本文地址:http://www.cnblogs.com/archimedes/p/programming-abstractions-in-c-1.html,轉載請注明源地址。
1、溫度轉換:
#include<stdio.h> int main() { double F,C; while(~scanf("%f", &C)) printf("%lf\n", 9 * C * 1.0 / 5 + 32); return 0; }
2、長度轉換:
#include<stdio.h> int main() { double YCI, YC, M; while(~scanf("%f", &M)){ printf("YCI:%lf\n", 1.0 / 0.0254); printf("YC:%lf\n", 1.0 / 12); } return 0; }
3、計算1+2+3+……+100
#include<stdio.h> int main() { int sum = 0; for(int i = 1; i <= 100; i++) sum += i; printf("%d\n", sum); return 0; }
4、計算序列值
#include<stdio.h> int main() { int N, i, sum; scanf("%d", &N); sum = 0; i = 1; while(i <= N) sum += 2 * (i++) - 1; printf("%d\n", sum); return 0; }
5、按照指定格式輸入一個整數序列中的最大值
#include<stdio.h> int main() { int max, n, i; printf("This program finds the largest integer in a list.\n"); printf("Enter 0 to sigal the end of the list.\n"); printf("? "); max = -1; while(scanf("%d", &n) && n) { if(max < n) max = n; printf("? "); } printf("The largest value is %d\n", max); return 0; }
6、反轉打印輸入的整數
#include<stdio.h> void print(int N) { printf("The reverse number is "); while(N) { printf("%d", N % 10); N /= 10; } printf("\n"); } int main() { int N; printf("This program reverses the digits in an integer.\n"); printf("Enter a positive integer: "); scanf("%d", &N); print(N); return 0; }
7、尋找完全數
#include<stdio.h> #include<math.h> #include<stdbool.h> bool IsPerfect(int n) { int m, sum, t; sum = 1; m = (int)sqrt(n); for(int i = 2; i <= m; i++){ if(n % i == 0) { if(i * i == n) { sum += m; } else { sum += (i + n / i); } } } if(sum == n) return true; else return false; } int main() { for(int i = 2; i <= 9999; i++) { if(IsPerfect(i)) printf("%d\n", i); } return 0; }
8、按照指定格式分解質因數
#include<stdio.h> #include<math.h> #include<stdbool.h> #define N 100000 int isprime[N]; bool prime(int n) { int i; for(i = 2; i * i <= n; i++) { if(n % i == 0) return false; } return true; } void init() { int i, k; k = 0; for(i = 2; i < 100000; i++) { if(prime(i)) isprime[k++] = i; } } void solve() { int i, n, flag, k, t; init(); printf("Enter number to be factored: "); while(~scanf("%d", &n)) { k = flag = 0; if(prime(n)) { printf("%d\n", n); } else { t = n; while(isprime[k] < n) { while(t % isprime[k] == 0) { if(!flag) { flag = 1; printf("%d", isprime[k]); } else { printf(" * %d", isprime[k]); } t /= isprime[k]; } k++; } printf("\n"); } printf("Enter number to be factored: "); } } int main() { solve(); return 0; }
9、浮點數按照規則轉化為整數
#include<stdio.h> #include<math.h> #include<stdbool.h> void Round(float n) { int m; m = floor(n); if(n > 0) { if(n - m < 0.5) { printf("%d\n", m); } else { printf("%d\n", m + 1); } } else { if(n - 0.5 > m) { printf("%d\n", m + 1); } else { printf("%d\n", m); } } } int main() { float n; while(~scanf("%f", &n)) Round(n); return 0; }
10、利用萊布利茲公式計算PI
#include<stdio.h> #include<math.h> #include<stdbool.h> double count(int n) { int t, i; double ans = 0.0; t = 1; for(i = 1; i <= n; i++) { ans += t * 1.0 / (2 * i - 1); t *= -1; } return 4 * ans; } int main() { printf("%f\n", count(10000)); return 0; }
11、通過扇形的面積近似計算PI
#include<stdio.h> #include<math.h> #include<stdbool.h> #define N 100 void solve() { int i; double w, r, ans, x; r = 2.0; w = r / N; ans = 0.0; for(i = 1; i <= N; i++) { x = w * (i - 0.5); ans += sqrt(r * r - x * x) * w; } printf("%f\n", ans); } int main() { solve(); return 0; }