入门到醒目,C语言十大经典步调

[复制链接]
发表于 2025-9-24 16:17:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
以下是十个经典的C语言步调示例,这些步调涵盖了从根本到稍复杂的应用场景,得当初学者和有肯定根本的开发者学习和参考。
1. Hello, World!

这是每个初学者学习编程时的第一个步调,用于验证开发情况是否正确设置。
  1. #include <stdio.h>
  2. int main() {
  3.     printf("Hello, World!\n");
  4.     return 0;
  5. }
复制代码
2. 求两个数的最大公约数

利用欧几里得算法盘算两个整数的最大公约数。
  1. #include <stdio.h>
  2. int gcd(int a, int b) {
  3.     while (b != 0) {
  4.         int temp = b;
  5.         b = a % b;
  6.         a = temp;
  7.     }
  8.     return a;
  9. }
  10. int main() {
  11.     int num1, num2;
  12.     printf("Enter two integers: ");
  13.     scanf("%d %d", &num1, &num2);
  14.     printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2));
  15.     return 0;
  16. }
复制代码
3. 素数判断

判断一个数是否为素数。
  1. #include <stdio.h>
  2. #include <math.h>
  3. int isPrime(int num) {
  4.     if (num <= 1) return 0;
  5.     for (int i = 2; i <= sqrt(num); i++) {
  6.         if (num % i == 0) return 0;
  7.     }
  8.     return 1;
  9. }
  10. int main() {
  11.     int num;
  12.     printf("Enter a number: ");
  13.     scanf("%d", &num);
  14.     if (isPrime(num)) {
  15.         printf("%d is a prime number.\n", num);
  16.     } else {
  17.         printf("%d is not a prime number.\n", num);
  18.     }
  19.     return 0;
  20. }
复制代码
4. 冒泡排序

对数组举行冒泡排序。
  1. #include <stdio.h>
  2. void bubbleSort(int arr[], int n) {
  3.     for (int i = 0; i < n - 1; i++) {
  4.         for (int j = 0; j < n - i - 1; j++) {
  5.             if (arr[j] > arr[j + 1]) {
  6.                 int temp = arr[j];
  7.                 arr[j] = arr[j + 1];
  8.                 arr[j + 1] = temp;
  9.             }
  10.         }
  11.     }
  12. }
  13. int main() {
  14.     int arr[] = {64, 34, 25, 12, 22, 11, 90};
  15.     int n = sizeof(arr) / sizeof(arr[0]);
  16.     bubbleSort(arr, n);
  17.     printf("Sorted array: \n");
  18.     for (int i = 0; i < n; i++) {
  19.         printf("%d ", arr[i]);
  20.     }
  21.     printf("\n");
  22.     return 0;
  23. }
复制代码
5. 斐波那契数列

天生斐波那契数列的前N项。
  1. #include <stdio.h>
  2. void fibonacci(int n) {
  3.     int a = 0, b = 1, c;
  4.     printf("Fibonacci sequence: \n");
  5.     for (int i = 0; i < n; i++) {
  6.         printf("%d ", a);
  7.         c = a + b;
  8.         a = b;
  9.         b = c;
  10.     }
  11. }
  12. int main() {
  13.     int n;
  14.     printf("Enter the number of terms: ");
  15.     scanf("%d", &n);
  16.     fibonacci(n);
  17.     return 0;
  18. }
复制代码
6. 字符串反转

反转一个字符串。
  1. #include <stdio.h>
  2. #include <string.h>
  3. void reverseString(char str[]) {
  4.     int len = strlen(str);
  5.     for (int i = 0; i < len / 2; i++) {
  6.         char temp = str[i];
  7.         str[i] = str[len - i - 1];
  8.         str[len - i - 1] = temp;
  9.     }
  10. }
  11. int main() {
  12.     char str[100];
  13.     printf("Enter a string: ");
  14.     scanf("%99s", str);
  15.     reverseString(str);
  16.     printf("Reversed string: %s\n", str);
  17.     return 0;
  18. }
复制代码
7. 二分查找

在有序数组中查找一个元素。
  1. #include <stdio.h>
  2. int binarySearch(int arr[], int l, int r, int x) {
  3.     while (l <= r) {
  4.         int m = l + (r - l) / 2;
  5.         if (arr[m] == x) return m;
  6.         if (arr[m] < x) l = m + 1;
  7.         else r = m - 1;
  8.     }
  9.     return -1;
  10. }
  11. int main() {
  12.     int arr[] = {2, 3, 4, 10, 40};
  13.     int n = sizeof(arr) / sizeof(arr[0]);
  14.     int x = 10;
  15.     int result = binarySearch(arr, 0, n - 1, x);
  16.     if (result == -1) {
  17.         printf("Element is not present in array\n");
  18.     } else {
  19.         printf("Element is present at index %d\n", result);
  20.     }
  21.     return 0;
  22. }
复制代码
8. 矩阵乘法

盘算两个矩阵的乘积。
  1. #include <stdio.h>
  2. void multiplyMatrices(int mat1[][10], int mat2[][10], int res[][10], int r1, int c1, int r2, int c2) {
  3.     for (int i = 0; i < r1; i++) {
  4.         for (int j = 0; j < c2; j++) {
  5.             res[i][j] = 0;
  6.             for (int k = 0; k < c1; k++) {
  7.                 res[i][j] += mat1[i][k] * mat2[k][j];
  8.             }
  9.         }
  10.     }
  11. }
  12. int main() {
  13.     int mat1[10][10], mat2[10][10], res[10][10];
  14.     int r1, c1, r2, c2;
  15.     printf("Enter rows and columns for first matrix: ");
  16.     scanf("%d %d", &r1, &c1);
  17.     printf("Enter rows and columns for second matrix: ");
  18.     scanf("%d %d", &r2, &c2);
  19.     if (c1 != r2) {
  20.         printf("Matrix multiplication not possible.\n");
  21.         return 0;
  22.     }
  23.     printf("Enter elements of first matrix:\n");
  24.     for (int i = 0; i < r1; i++) {
  25.         for (int j = 0; j < c1; j++) {
  26.             scanf("%d", &mat1[i][j]);
  27.         }
  28.     }
  29.     printf("Enter elements of second matrix:\n");
  30.     for (int i = 0; i < r2; i++) {
  31.         for (int j = 0; j < c2; j++) {
  32.             scanf("%d", &mat2[i][j]);
  33.         }
  34.     }
  35.     multiplyMatrices(mat1, mat2, res, r1, c1, r2, c2);
  36.     printf("Resultant matrix:\n");
  37.     for (int i = 0; i < r1; i++) {
  38.         for (int j = 0; j < c2; j++) {
  39.             printf("%d ", res[i][j]);
  40.         }
  41.         printf("\n");
  42.     }
  43.     return 0;
  44. }
复制代码
9. 链表反转

反转一个单链表。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct Node {
  4.     int data;
  5.     struct Node* next;
  6. };
  7. struct Node* newNode(int data) {
  8.     struct Node* node = (struct Node*)malloc(sizeof(struct Node));
  9.     node->data = data;
  10.     node->next = NULL;
  11.     return node;
  12. }
  13. void reverse(struct Node** head) {
  14.     struct Node* prev = NULL;
  15.     struct Node* current = *head;
  16.     struct Node* next = NULL;
  17.     while (current != NULL) {
  18.         next = current->next;
  19.         current->next = prev;
  20.         prev = current;
  21.         current = next;
  22.     }
  23.     *head = prev;
  24. }
  25. void printList(struct Node* node) {
  26.     while (node != NULL) {
  27.         printf("%d ", node->data);
  28.         node = node->next;
  29.     }
  30.     printf("\n");
  31. }
  32. int main() {
  33.     struct Node* head = newNode(1);
  34.     head->next = newNode(2);
  35.     head->next->next = newNode(3);
  36.     head->next->next->next = newNode(4);
  37.     head->next->next->next->next = newNode(5);
  38.     printf("Original list: ");
  39.     printList(head);
  40.     reverse(&head);
  41.     printf("Reversed list: ");
  42.     printList(head);
  43.     return 0;
  44. }
复制代码
10. 文件读写

读取文件内容并写入到另一个文件。
  1. #include <stdio.h>
  2. int main() {
  3.     FILE* sourceFile = fopen("source.txt", "r");
  4.     FILE* targetFile = fopen("target.txt", "w");
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表