首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

經典 C 語言算法 100 例:從費波納契到數值運算完整實作

     編輯:關於C語言

經典算法題目與解法

1. 兔子繁殖問題(費波納契數列)

規律:1, 1, 2, 3, 5, 8, 13, 21...

#include <stdio.h>
int main() {
    long f1 = 1, f2 = 1;
    for (int i = 1; i <= 20; i++) {
        printf("%12ld %12ld", f1, f2);
        if (i % 2 == 0) printf("\n");
        f1 = f1 + f2;
        f2 = f1 + f2;
    }
    return 0;
}

2. 判斷素數(101-200)

判斷邏輯:試除法至 sqrt(n)

#include <stdio.h>
#include <math.h>
int main() {
    int m, i, k, h = 0, leap = 1;
    for (m = 101; m <= 200; m++) {
        k = sqrt(m + 1);
        for (i = 2; i <= k; i++)
            if (m % i == 0) { leap = 0; break; }
        if (leap) { printf("%-4d", m); if (++h % 10 == 0) printf("\n"); }
        leap = 1;
    }
    printf("\n總數: %d", h);
    return 0;
}

3. 水仙花數(三位數自戀數)

各位數字立方和等於自身:153 = 1³+5³+3³

#include <stdio.h>
int main() {
    for (int n = 100; n < 1000; n++) {
        int i = n / 100, j = n / 10 % 10, k = n % 10;
        if (n == i*i*i + j*j*j + k*k*k) printf("%-5d", n);
    }
    return 0;
}

4. 質因數分解

#include <stdio.h>
int main() {
    int n, i;
    printf("輸入正整數: "); scanf("%d", &n);
    printf("%d=", n);
    for (i = 2; i <= n; i++) {
        while (n != i) {
            if (n % i == 0) { printf("%d*", i); n /= i; }
            else break;
        }
    }
    printf("%d", n);
    return 0;
}

5. 最大公約數與最小公倍數(輾轉相除法)

#include <stdio.h>
int main() {
    int a, b, num1, num2, temp;
    scanf("%d,%d", &num1, &num2);
    if (num1 < num2) { temp = num1; num1 = num2; num2 = temp; }
    a = num1; b = num2;
    while (b != 0) { temp = a % b; a = b; b = temp; }
    printf("GCD: %d\nLCM: %d\n", a, num1 * num2 / a);
    return 0;
}

6. 字元統計:字母、空格、數字、其他

#include <stdio.h>
int main() {
    char c; int letters=0, space=0, digit=0, others=0;
    while ((c = getchar()) != '\n') {
        if ((c>='a'&&c<='z')||(c>='A'&&c<='Z')) letters++;
        else if (c==' ') space++;
        else if (c>='0'&&c<='9') digit++;
        else others++;
    }
    printf("字母=%d 空格=%d 數字=%d 其他=%d\n", letters, space, digit, others);
    return 0;
}

7. 猴子吃桃問題(逆向推導)

#include <stdio.h>
int main() {
    int n = 1;
    for (int i = 1; i < 10; i++) n = (n + 1) * 2;
    printf("第一天共摘 %d 個桃\n", n);
    return 0;
}

8. 數值方法:牛頓迭代法求平方根與方程根

#include <stdio.h>
#include <math.h>
#define EPS 1.0E-6

// 平方根
float sqrt_newton(float a) {
    float x0 = a/2, x1 = (x0 + a/x0)/2;
    while (fabs(x1 - x0) >= EPS) { x0 = x1; x1 = (x0 + a/x0)/2; }
    return x1;
}

// 方程 2x³-4x²+3x-6=0
float solve_cubic() {
    float x0 = 1.5, x1;
    x1 = x0 - (2*x0*x0*x0 - 4*x0*x0 + 3*x0 - 6) / (6*x0*x0 - 8*x0 + 3);
    while (fabs(x1 - x0) >= EPS) { x0 = x1; x1 = x0 - (2*x0*x0*x0 - 4*x0*x0 + 3*x0 - 6) / (6*x0*x0 - 8*x0 + 3); }
    return x1;
}

9. 圖形列印:菱形、矩陣對角線和、陣列操作

包含雙重迴圈控制列印菱形、3x3 矩陣對角線求和、已排序陣列插入元素、陣列逆序輸出等經典練習。

10. 位元運算實戰

  • & 取特定位、清零
  • | 設定特定位
  • ^ 翻轉特定位、交換變數
  • ~ 取補數
  • 移位運算 << >> 配合遮罩提取位段

11. 圖形程式設計(BGI 圖形庫)

繪製同心圓、放射線、巢狀矩形等圖形,展示 graphics.h 基礎用法。

這 100 例涵蓋基礎語法、演算法邏輯、數值計算、資料結構操作、位元運算與圖形程式設計,是 C 語言學習者的經典練習庫。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved