收錄 100 道經典 C 語言演算法題目,涵蓋基礎語法、數學運算、陣列操作、字串處理、遞迴、排序搜尋、位元運算、圖形繪製等核心領域。本文呈現前 45 題完整解析與程式碼,適合面試複習、教學參考與技能鞏固。
long f1=1, f2=1;
for (int i=1; i<=20; ++i) {
printf("%12ld %12ld", f1, f2);
if (i%2==0) puts("");
f1 += f2; f2 += f1;
}
for (int m=101; m<=200; ++m) {
int k = sqrt(m+1), leap=1;
for (int i=2; i<=k; ++i)
if (m%i==0) { leap=0; break; }
if (leap) printf("%-4d", m);
}
for (int n=100; n<1000; ++n) {
int a=n/100, b=n/10%10, c=n%10;
if (a*a*a+b*b*b+c*c*c == n) printf("%-5d", n);
}
for (int i=2; i<=n; ++i)
while (n!=i) {
if (n%i==0) { printf("%d*", i); n/=i; }
else break;
}
printf("%d", n);
grade = score>=90 ? 'A' : (score>=60 ? 'B' : 'C');
while (b) { int t=a%b; a=b; b=t; }
printf("GCD=%d, LCM=%d", a, n1*n2/a);
while ((c=getchar())!='\n') {
if (isalpha(c)) ++letter;
else if (c==' ') ++space;
else if (isdigit(c)) ++digit;
else ++other;
}
while (cnt<=n) { tn=tn*10+a; sn+=tn; ++cnt; }
for (int j=2; j<1000; ++j) {
int s=j;
for (int i=1; i
float h=100, sum=100;
for (int i=2; i<=10; ++i) { h/=2; sum+=2*h; }
int n=1;
for (int i=1; i<10; ++i) n=(n+1)*2;
// 牛頓法求 2x^3-4x^2+3x-6=0
float x=1.5;
do { x0=x; x=x0-(2*x0*x0*x0-4*x0*x0+3*x0-6)/(6*x0*x0-8*x0+3); }
while (fabs(x-x0)>=EPS);
for (int i=0; i<=3; ++i) { spaces(2-i); stars(2*i+1); }
for (int i=0; i<=2; ++i) { spaces(i); stars(4-2*i); }
wan=x/10000; qian=x%10000/1000; shi=x%100/10; ge=x%10;
if (ge==wan && shi==qian) puts("迴文");
switch(getch()) { case 'S': if(getch()=='a') puts("Saturday"); ... }
textbackground, textcolor, gotoxy, clrscr (需 conio.h,Windows 專用)。
for (int i=2; i
for (i=0; i
for (i=0; i<3; ++i) sum += a[i][i];
for (i=0; i<10; ++i) if (a[i]>num) { shift_right(i); a[i]=num; break; }
for (i=0; i
展示區域靜態變數保持值、自動變數生命週期、外部變數跨檔案連結、暫存器變數加速迴圈。
#define SQ(x) ((x)*(x))
#define exchange(a,b) { int t=a; a=b; b=t; }
#define LAG > // 運算子巨集
示範 #ifdef MAX 切換編譯邏輯、跨檔案巨集共享。
b = a & 3; // 取低 2 位
b = a | 3; // 設低 2 位
b = a ^ 3; // 翻轉低 2 位
b = ~a; // 位元取反
d = (a>>4) & ~(~0<<4); // 取 4~7 位
BGI 圖形庫示範:同心圓、網格線、同心矩形 (需 graphics.h,Turbo C 時代遺產)。
stdbool.h、inttypes.h、// 註解、變數就近宣告。後續將陸續發布第 46-100 題,涵蓋遞迴、動態規劃、圖演算法、資料結構實作等進階主題。