本文地址:http://www.cnblogs.com/archimedes/p/programming-abstractions-in-c-2.html,轉載請注明源地址。
2、按照規定求圓柱的表面積和體積
#include<stdio.h> #include<math.h> #include<stdbool.h> #define PI 3.1415926 void 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<stdio.h> #include<math.h> #include<stdbool.h> #define MaxJudges 100 double 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<stdio.h> #include<math.h> #include<stdbool.h> 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<stdio.h> #include<stdbool.h> #include<math.h> #define N 1000000 bool 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<stdio.h> #include<stdbool.h> #include<math.h> 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<stdio.h> #include<stdbool.h> #include<math.h> 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<stdio.h> #include<stdbool.h> #include<math.h> 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<stdio.h> #include<stdbool.h> #include<math.h> #include<stdlib.h> 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; }