马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
以下是十个经典的C语言步调示例,这些步调涵盖了从根本到稍复杂的应用场景,得当初学者和有肯定根本的开发者学习和参考。
1. Hello, World!
这是每个初学者学习编程时的第一个步调,用于验证开发情况是否正确设置。- #include <stdio.h>
- int main() {
- printf("Hello, World!\n");
- return 0;
- }
复制代码 2. 求两个数的最大公约数
利用欧几里得算法盘算两个整数的最大公约数。- #include <stdio.h>
- int gcd(int a, int b) {
- while (b != 0) {
- int temp = b;
- b = a % b;
- a = temp;
- }
- return a;
- }
- int main() {
- int num1, num2;
- printf("Enter two integers: ");
- scanf("%d %d", &num1, &num2);
- printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2));
- return 0;
- }
复制代码 3. 素数判断
判断一个数是否为素数。- #include <stdio.h>
- #include <math.h>
- int isPrime(int num) {
- if (num <= 1) return 0;
- for (int i = 2; i <= sqrt(num); i++) {
- if (num % i == 0) return 0;
- }
- return 1;
- }
- int main() {
- int num;
- printf("Enter a number: ");
- scanf("%d", &num);
- if (isPrime(num)) {
- printf("%d is a prime number.\n", num);
- } else {
- printf("%d is not a prime number.\n", num);
- }
- return 0;
- }
复制代码 4. 冒泡排序
对数组举行冒泡排序。- #include <stdio.h>
- void bubbleSort(int arr[], int n) {
- for (int i = 0; i < n - 1; i++) {
- for (int j = 0; j < n - i - 1; j++) {
- if (arr[j] > arr[j + 1]) {
- int temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- }
- }
- }
- }
- int main() {
- int arr[] = {64, 34, 25, 12, 22, 11, 90};
- int n = sizeof(arr) / sizeof(arr[0]);
- bubbleSort(arr, n);
- printf("Sorted array: \n");
- for (int i = 0; i < n; i++) {
- printf("%d ", arr[i]);
- }
- printf("\n");
- return 0;
- }
复制代码 5. 斐波那契数列
天生斐波那契数列的前N项。- #include <stdio.h>
- void fibonacci(int n) {
- int a = 0, b = 1, c;
- printf("Fibonacci sequence: \n");
- for (int i = 0; i < n; i++) {
- printf("%d ", a);
- c = a + b;
- a = b;
- b = c;
- }
- }
- int main() {
- int n;
- printf("Enter the number of terms: ");
- scanf("%d", &n);
- fibonacci(n);
- return 0;
- }
复制代码 6. 字符串反转
反转一个字符串。- #include <stdio.h>
- #include <string.h>
- void reverseString(char str[]) {
- int len = strlen(str);
- for (int i = 0; i < len / 2; i++) {
- char temp = str[i];
- str[i] = str[len - i - 1];
- str[len - i - 1] = temp;
- }
- }
- int main() {
- char str[100];
- printf("Enter a string: ");
- scanf("%99s", str);
- reverseString(str);
- printf("Reversed string: %s\n", str);
- return 0;
- }
复制代码 7. 二分查找
在有序数组中查找一个元素。- #include <stdio.h>
- int binarySearch(int arr[], int l, int r, int x) {
- while (l <= r) {
- int m = l + (r - l) / 2;
- if (arr[m] == x) return m;
- if (arr[m] < x) l = m + 1;
- else r = m - 1;
- }
- return -1;
- }
- int main() {
- int arr[] = {2, 3, 4, 10, 40};
- int n = sizeof(arr) / sizeof(arr[0]);
- int x = 10;
- int result = binarySearch(arr, 0, n - 1, x);
- if (result == -1) {
- printf("Element is not present in array\n");
- } else {
- printf("Element is present at index %d\n", result);
- }
- return 0;
- }
复制代码 8. 矩阵乘法
盘算两个矩阵的乘积。- #include <stdio.h>
- void multiplyMatrices(int mat1[][10], int mat2[][10], int res[][10], int r1, int c1, int r2, int c2) {
- for (int i = 0; i < r1; i++) {
- for (int j = 0; j < c2; j++) {
- res[i][j] = 0;
- for (int k = 0; k < c1; k++) {
- res[i][j] += mat1[i][k] * mat2[k][j];
- }
- }
- }
- }
- int main() {
- int mat1[10][10], mat2[10][10], res[10][10];
- int r1, c1, r2, c2;
- printf("Enter rows and columns for first matrix: ");
- scanf("%d %d", &r1, &c1);
- printf("Enter rows and columns for second matrix: ");
- scanf("%d %d", &r2, &c2);
- if (c1 != r2) {
- printf("Matrix multiplication not possible.\n");
- return 0;
- }
- printf("Enter elements of first matrix:\n");
- for (int i = 0; i < r1; i++) {
- for (int j = 0; j < c1; j++) {
- scanf("%d", &mat1[i][j]);
- }
- }
- printf("Enter elements of second matrix:\n");
- for (int i = 0; i < r2; i++) {
- for (int j = 0; j < c2; j++) {
- scanf("%d", &mat2[i][j]);
- }
- }
- multiplyMatrices(mat1, mat2, res, r1, c1, r2, c2);
- printf("Resultant matrix:\n");
- for (int i = 0; i < r1; i++) {
- for (int j = 0; j < c2; j++) {
- printf("%d ", res[i][j]);
- }
- printf("\n");
- }
- return 0;
- }
复制代码 9. 链表反转
反转一个单链表。- #include <stdio.h>
- #include <stdlib.h>
- struct Node {
- int data;
- struct Node* next;
- };
- struct Node* newNode(int data) {
- struct Node* node = (struct Node*)malloc(sizeof(struct Node));
- node->data = data;
- node->next = NULL;
- return node;
- }
- void reverse(struct Node** head) {
- struct Node* prev = NULL;
- struct Node* current = *head;
- struct Node* next = NULL;
- while (current != NULL) {
- next = current->next;
- current->next = prev;
- prev = current;
- current = next;
- }
- *head = prev;
- }
- void printList(struct Node* node) {
- while (node != NULL) {
- printf("%d ", node->data);
- node = node->next;
- }
- printf("\n");
- }
- int main() {
- struct Node* head = newNode(1);
- head->next = newNode(2);
- head->next->next = newNode(3);
- head->next->next->next = newNode(4);
- head->next->next->next->next = newNode(5);
- printf("Original list: ");
- printList(head);
- reverse(&head);
- printf("Reversed list: ");
- printList(head);
- return 0;
- }
复制代码 10. 文件读写
读取文件内容并写入到另一个文件。- #include <stdio.h>
- int main() {
- FILE* sourceFile = fopen("source.txt", "r");
- FILE* targetFile = fopen("target.txt", "w");
复制代码 |