盡量縮短變數的定義范圍
四舍五入(把一個單位區間往左移動了0.5個單位的距離)
進行浮點數比較時,考慮浮點誤差#include<stdio.h> #include<math.h> int main() { for(int a = 1; a <= 9; a++) for(int b = 0; b <= 9; b++) { int n = a*1100 + b*11; // 盡量縮短變數的定義范圍 int m = floor(sqrt(n) + 0.5); // 四舍五入,把一個單位區間往左移動了0.5個單位的距離 if(m*m == n) // 進行浮點數比較時,考慮浮點誤差 printf("%d\n", n); } return 0; }
算數運算溢位
int32: -2147483648~2147483647
#include<stdio.h> int main() { int n, count = 0; scanf("%d", &n); while(n > 1) { if(n % 2 == 1) n = n*3+1; else n/=2; count++; // 計數器 } printf("%d\n", count); return 0; }
long long -2^63~2^63-1 cin/cout
#include<stdio.h> int main() { int n2, count = 0; scanf("%d", &n2); long long n = n2; while(n > 1) { if(n % 2 == 1) n = n*3+1; else n/=2; count++; // 計數器 } printf("%d\n", count); return 0; }
奇偶項
#include<stdio.h> int main() { double sum = 0; for(int i = 0; ; i++) { double term = 1.0 / (i*2+1); if(i % 2 == 0) // 奇偶項 sum += term; else sum -= term; if(term < 1e-6) break; } printf("%.6f", sum); return 0; }
末六位
#include<stdio.h> int main() { int n, S = 0; scanf("%d", &n); for(int i = 1; i <= n; i++) { int factorial = 1; for(int j = 1; j <= i; j++) factorial *= j; S += factorial; } printf("%d\n", S % 1000000); // 末六位 return 0; }要計算只包含+、-、*的整數運算式 / n的余數,可以在每步計算之后對n取余,結果不變 計時函式clock(),回傳程式目前為止運行的時間 / CLOCKS_PER_SEC以s為單位 避免輸入資料的時間影響測驗結果——管道 Windows: echo 20 | test.c Linux: echo 20 | ./test.c 程式效率低下!!!
#include<stdio.h> #include<time.h> int main() { const int mod = 1000000; int n, S = 0; scanf("%d", &n); for(int i = 1; i <= n; i++) { int factorial = 1; for(int j = 1; j <= i; j++) factorial = (factorial * j % mod); // 要計算只包含+、-、*的整數運算式 / n的余數,可以在每步計算之后對n取余,結果不變 S = (S + factorial) % mod; } printf("%d\n", S); printf("Time used = %.2f\n", (double)clock() / CLOCKS_PER_SEC); // 計時函式clock(),回傳程式目前為止運行的時間 / CLOCKS_PER_SEC以s為單位 return 0; }輸入的個數不確定 結束輸入:Windows: Enter-->Ctrl+Z-->Enter Linux: Ctrl+D
#include<stdio.h> int main() { int x, n = 0, min, max, s = 0; while(scanf("%d", &x) == 1) { // 輸入的個數不確定 s += x; if(x < min) min = x; if(x > max) max = x; n++; } printf("%d %d %.3f\n", min, max, (double)s/n); return 0; }
了解檔案讀寫的相關規定
檔案輸入輸出:輸入輸出重定向,不能同時讀寫檔案和標準輸入輸出
freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
檔案比較:Windows: fc
Linux: diff
#define LOCAL #include<stdio.h> #define INF 1000000000 int main() { #ifdef LOCAL // 條件編譯 freopen("data.in", "r", stdin); freopen("data.out", "w", stdout); #endif int x, n = 0, min = INF, max = -INF, s = 0; while(scanf("%d", &x) == 1) { s += x; if(x < min) min = x; if(x > max) max = x; /* printf("x = %d, min = %d, max = %d\n", x, min, max); */ n++; } printf("%d %d %.3f\n", min, max, (double)s/n); return 0; }
在編譯選項里定義LOCAL
檔案輸入輸出,禁止重定向,fopen可以反復打開并讀寫檔案
#include<stdio.h> #define INF 1000000000 int main() { FILE *fin, *fout; fin = fopen("data.in", "rb"); fout = fopen("data.out", "wb"); int x, n = 0, min = INF, max = -INF, s = 0; while(fscanf(fin, "%d", &x) == 1) { s += x; if(x < min) min = x; if(x > max) max = x; n++; } fprintf(fout, "%d %d %.3f\n", min, max, (double)s/n); fclose(fin); fclose(fout); return 0; }
改成標準輸入輸出,不要呼叫fopen和fclose
fin = stdin;
fout = stdout;
多組資料
魯棒性,在資料有瑕疵的情況下仍然給出正確的結果#include<stdio.h> #define INF 1000000000 int main() { int x, n = 0, min = INF, max = -INF, s = 0, kase = 0; // 當前資料編號 while(scanf("%d", &n) == 1 && n) { // 魯棒性,在資料有瑕疵的情況下仍然給出正確的結果 int s = 0; for(int i = 0; i < n; i++) { scanf("%d", &x); s += x; if(x < min) min = x; if(x > max) max = x; } if(kase) printf("\n"); printf("Case %d: %d %d %.3f\n", ++kase, min, max, (double)s/n); } return 0; }
編譯選項-Wall給出警告資訊
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/60069.html
標籤:其他
