本文地址:,转载请注明源地址。
2、按照规定求圆柱的表面积和体积
#include#include #include #define PI 3.1415926void input(double *r, double *h){ double a, b; printf("请输入半径和高度:"); scanf("%lf %lf", &a, &b); *r = a; *h = b;}void count(double r, double h, double *s, double *v){ *s = (2 * PI * r) * h; *v = PI * r * r * h;}void output(double s, double v){ printf("圆柱的表面积:%lf\n", s); printf("圆柱的体积:%lf\n", v);}int main(){ double r, h, s, v; input(&r, &h); count(r, h, &s, &v); output(s, v); return 0;}
3、按照规则计算平均值
#include#include #include #define MaxJudges 100double Mean(double array[], int n){ int i; double total, max, min; max = min = array[0]; total = 0.0; for(i = 0; i < n; i++) { total += array[i]; if(max < array[i]) max = array[i]; if(min > array[i]) min = array[i]; } return ((total - min - max) / n);}void ReadScore(double scores[], int n){ int i; for(i = 0; i < n; i++) scanf("%lf", &scores[i]);}int main(){ int n; double array[MaxJudges]; scanf("%d", &n); ReadScore(array, n); printf("平均值为:%lf\n", Mean(array, n)); return 0;}
4、判断一个数列是否升序
#include#include #include bool IsSorted(int *a, int n){ int i; for(i = 1; i <= n - 1; i++) { if(a[i] < a[i-1]) return false; } return true;}int main(){ int n, array[1000]; scanf("%d", &n); for(int i = 0; i < n; i++) scanf("%d", &array[i]); if(IsSorted(array, n)) printf("TRUE\n"); else printf("FALSE\n"); return 0;}
5、埃拉托斯特尼筛生成2~1000之间的素数
#include#include #include #define N 1000000bool isprime[N];void initPrime(){ int n, i, j; int k; for(k = 0; k < N; k++) isprime[k] = true; n = (int)sqrt(N); for(i = 2; i <= n; i++) { if(isprime[i]) { j = i * i; while(j <= n) { isprime[j] = false; j += i; } } }}int main(){ int i = 2; initPrime(); while(1) { if(i > 1000) break; if(isprime[i]) printf("%d\n", i); i++; } return 0;}
6、按照格式输出一组数据的柱状图
#include#include #include void printStar(int n){ for(int i = 1; i <= n; i++) putchar('*'); printf("\n");}int main(){ int a[11], b[1000]; int n, t, i; scanf("%d", &n); for(i = 0; i < 11; i++) a[i] = 0; for(i =0; i < n; i++) { scanf("%d", &b[i]); t = b[i] / 10; switch(t) { case 0: a[0]++; break; case 1: a[1]++; break; case 2: a[2]++; break; case 3: a[3]++; break; case 4: a[4]++; break; case 5: a[5]++; break; case 6: a[6]++; break; case 7: a[7]++; break; case 8: a[8]++; break; case 9: a[9]++; break; case 10: a[10]++; break; default: break; } } for(i = 0; i < 11; i++) { printf("%3d: ", i * 10); printStar(a[i]); } return 0;}
8、扫描数组,出去数组中含0的元素,返回0的数目
#include#include #include void swap(int *a, int *b){ int t; t = *a; *a = *b; *b = t;}int RemoveZeroElements(int a[], int n){ int num, i, j; num = 0; if(a[0]) num++; for(i = 0; i < n; i++) { if(!a[i] && i < n - 1) { for(j = i + 1; j < n; j++) { if(a[j]) { num++; swap(&a[i], &a[j]); break; } } } } return n - num;}void printArray(int *a, int n){ int i; for(i = 0; i < n; i++) printf("%d ", a[i]); printf("\n");}int main(){ int a[] = { 65,0,95,0,0,79,82,0,84,94,86,90,0}; int n, zeronum; n = sizeof(a) / sizeof(a[0]); printArray(a, n); zeronum = RemoveZeroElements(a, n); printArray(a, n - zeronum); printf("zreo numbers: %d\n", zeronum); return 0;}
10、找出一组输入数字中的最小值与最大值
#include#include #include void solve(){ double n, min, max; printf("Enter the elements of the array, one per line.\n"); printf("use -1 to sigal the end of the list.\n"); printf("? "); scanf("%lf", &n); max = min = n; printf("? "); while(~scanf("%lf", &n) && (n != -1)) { if(n < min) min = n; if(max < n) max = n; printf("? "); } printf("The range of value is %lf-%lf\n", min, max);}int main(){ solve(); return 0;}
11、按要求动态分配一个整型数组并赋值
#include#include #include #include int *indexArray(int n){ int *p = (int *)malloc(n * sizeof(int)); for(int i = 0; i < n; i++) p[i] = i; return p;}int main(){ int *ip, n; scanf("%d", &n); ip = indexArray(n); for(int i = 0; i < n; i++) printf("%d ", ip[i]); printf("\n"); free(ip); return 0;}