编程c有趣的代码是什么
其他 10
-
编程语言C中有很多有趣的代码,以下是一些例子:
- "Hello, World!"程序:
这是学习任何编程语言时的经典起点。在C语言中,可以使用以下代码来打印出"Hello, World!":
#include <stdio.h> int main() { printf("Hello, World!"); return 0; }- 逆序输出数字:
这段代码可以逆序输出一个输入的整数。例如,输入12345,输出54321。
#include <stdio.h> void reverseNumber(int num) { while (num > 0) { int digit = num % 10; printf("%d", digit); num = num / 10; } } int main() { int number; printf("请输入一个整数:"); scanf("%d", &number); reverseNumber(number); return 0; }- 猜数字游戏:
这段代码是一个简单的猜数字游戏,计算机随机生成一个数字,玩家需要猜测该数字是多少,直到猜对为止。
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int numberToGuess, guess, attempts = 0; srand(time(0)); numberToGuess = rand() % 100 + 1; printf("猜一个1到100之间的数字。\n"); do { printf("请输入你的猜测:"); scanf("%d", &guess); attempts++; if (guess > numberToGuess) { printf("猜大了!\n"); } else if (guess < numberToGuess) { printf("猜小了!\n"); } else { printf("恭喜你,猜对了!你总共猜了%d次。\n", attempts); } } while (guess != numberToGuess); return 0; }- 字符串反转:
这段代码可以将一个输入的字符串反转输出。例如,输入"Hello",输出"olleH"。
#include <stdio.h> #include <string.h> void reverseString(char* str) { int length = strlen(str); for (int i = length - 1; i >= 0; i--) { printf("%c", str[i]); } } int main() { char string[100]; printf("请输入一个字符串:"); scanf("%s", string); reverseString(string); return 0; }以上只是一些C语言中的有趣代码示例,C语言的应用非常广泛,你可以根据自己的兴趣和需求编写更多有趣的代码。
1年前 - "Hello, World!"程序:
-
编程是一门创造性的艺术,有许多有趣的代码可以写。下面是一些有趣的C语言代码示例:
- "Hello World"倒序输出:
#include <stdio.h> int main() { char str[] = "Hello World"; int len = strlen(str); for (int i = len - 1; i >= 0; i--) { printf("%c", str[i]); } return 0; }这段代码将字符串"Hello World"倒序输出,结果为"dlroW olleH"。
- 打印杨辉三角形:
#include <stdio.h> int main() { int rows, coef = 1; printf("Enter number of rows: "); scanf("%d", &rows); for (int i = 0; i < rows; i++) { for (int space = 1; space <= rows - i; space++) printf(" "); for (int j = 0; j <= i; j++) { if (j == 0 || i == 0) coef = 1; else coef = coef * (i - j + 1) / j; printf("%4d", coef); } printf("\n"); } return 0; }这段代码可以打印出指定行数的杨辉三角形。
- 生成随机数猜谜游戏:
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int number, guess, attempts = 0; srand(time(0)); number = rand() % 100 + 1; printf("Guess the number (1-100)\n"); do { scanf("%d", &guess); attempts++; if (guess > number) { printf("Too high! Try again.\n"); } else if (guess < number) { printf("Too low! Try again.\n"); } else { printf("Congratulations! You guessed the number in %d attempts.\n", attempts); } } while (guess != number); return 0; }这段代码生成一个1到100之间的随机数,并让玩家猜测该数,直到猜对为止。
- 实现一个简单的计算器:
#include <stdio.h> int main() { char operator; double num1, num2; printf("Enter an operator (+, -, *, /): "); scanf("%c", &operator); printf("Enter two numbers: "); scanf("%lf %lf", &num1, &num2); switch (operator) { case '+': printf("%.2lf + %.2lf = %.2lf", num1, num2, num1 + num2); break; case '-': printf("%.2lf - %.2lf = %.2lf", num1, num2, num1 - num2); break; case '*': printf("%.2lf * %.2lf = %.2lf", num1, num2, num1 * num2); break; case '/': if (num2 != 0) printf("%.2lf / %.2lf = %.2lf", num1, num2, num1 / num2); else printf("Error! Division by zero is not allowed."); break; default: printf("Invalid operator!"); } return 0; }这段代码实现了一个简单的计算器,用户可以输入两个数字和一个运算符,程序将根据运算符执行相应的计算。
- 实现冒泡排序算法:
#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]); return 0; }这段代码实现了冒泡排序算法,将一个整数数组按升序排列,并打印排序后的结果。
这些例子只是C语言中一小部分有趣的代码示例,编程的世界非常广阔,任何有趣的想法都可以通过编程来实现。
1年前 -
编程是一门充满创造力和挑战性的艺术,有很多有趣的代码可以编写。下面是几个有趣的C语言代码示例:
- Hello World 反向输出
#include <stdio.h> #include <string.h> int main() { char str[] = "Hello, World!"; int len = strlen(str); for (int i = len - 1; i >= 0; i--) { printf("%c", str[i]); } return 0; }这个代码将字符串 "Hello, World!" 反向输出,结果为 "!dlroW ,olleH"。
- 九九乘法表
#include <stdio.h> int main() { for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { printf("%d * %d = %d\t", j, i, i*j); } printf("\n"); } return 0; }这个代码打印出一个九九乘法表,结果如下:
1 * 1 = 1 1 * 2 = 2 2 * 2 = 4 1 * 3 = 3 2 * 3 = 6 3 * 3 = 9 ...- 求斐波那契数列
#include <stdio.h> int fibonacci(int n) { if (n == 0) { return 0; } else if (n == 1) { return 1; } else { return fibonacci(n-1) + fibonacci(n-2); } } int main() { int n = 10; for (int i = 0; i < n; i++) { printf("%d ", fibonacci(i)); } return 0; }这个代码使用递归方法计算斐波那契数列的前n个数,结果为:0 1 1 2 3 5 8 13 21 34。
- 猜数字游戏
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int number, guess, attempts = 0; srand(time(0)); number = rand() % 100 + 1; printf("猜一个1到100之间的数。\n"); do { printf("你的猜测是:"); scanf("%d", &guess); attempts++; if (guess > number) { printf("太大了!\n"); } else if (guess < number) { printf("太小了!\n"); } else { printf("恭喜你,猜对了!\n"); printf("你共猜测了%d次。\n", attempts); } } while (guess != number); return 0; }这个代码是一个猜数字游戏,程序随机生成一个1到100之间的数字,玩家需要猜测该数字,程序会提示猜测的数字是太大还是太小,直到猜对为止,并统计猜测次数。
以上是一些有趣的C语言代码示例,希望能够激发你的创造力和兴趣。编程的乐趣在于不断尝试和创新,你也可以根据自己的兴趣和想法编写有趣的代码。
1年前