引言
C语言作为一种历史悠久且功能强大的编程语言,在计算机科学和软件工程领域有着广泛的应用。学习C语言不仅能够帮助程序员掌握编程的基础,还能够培养逻辑思维和问题解决能力。本文将带您探索C语言编程中的100个趣味实例,旨在激发学习者的兴趣,提高编程技能。
第1章 趣味算法入门实例
1.1 程序1:计算阶乘
程序分析:阶乘是数学中的一个基本概念,用于计算一个正整数的阶乘。例如,5的阶乘(5!)等于5×4×3×2×1。
程序源代码:
#include <stdio.h>
long factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Factorial of %d is %ld\n", num, factorial(num));
return 0;
}
1.2 程序2:计算斐波那契数列
程序分析:斐波那契数列是一个著名的数列,其中每个数字是前两个数字的和。例如,数列的前几个数字是0, 1, 1, 2, 3, 5, 8, 13等。
程序源代码:
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Fibonacci number at position %d is %d\n", n, fibonacci(n));
return 0;
}
第2章 趣味数学实例
2.1 程序3:计算三角形的面积
程序分析:根据海伦公式,可以计算三角形的面积,其中a、b、c是三角形的三边,s是半周长。
程序源代码:
#include <stdio.h>
#include <math.h>
double calculate_area(double a, double b, double c) {
double s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
int main() {
double a, b, c;
printf("Enter the sides of the triangle: ");
scanf("%lf %lf %lf", &a, &b, &c);
printf("Area of the triangle is %lf\n", calculate_area(a, b, c));
return 0;
}
2.2 程序4:计算素数
程序分析:素数是只能被1和自身整除的大于1的自然数。例如,2, 3, 5, 7, 11等。
程序源代码:
#include <stdio.h>
#include <stdbool.h>
bool is_prime(int n) {
if (n <= 1)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (is_prime(num))
printf("%d is a prime number\n", num);
else
printf("%d is not a prime number\n", num);
return 0;
}
第3章 趣味整数实例
3.1 程序5:整数反转
程序分析:整数反转是指将一个整数的各个数字的位置颠倒,例如,1234反转后变为4321。
程序源代码:
#include <stdio.h>
int reverse_int(int n) {
int reversed = 0;
while (n != 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
return reversed;
}
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
printf("Reversed integer is %d\n", reverse_int(num));
return 0;
}
3.2 程序6:判断闰年
程序分析:闰年是指能被4整除但不能被100整除的年份,或者能被400整除的年份。
程序源代码:
#include <stdio.h>
int is_leap_year(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (is_leap_year(year))
printf("%d is a leap year\n", year);
else
printf("%d is not a leap year\n", year);
return 0;
}
第4章 趣味分数实例
4.1 程序7:计算最大公约数
程序分析:最大公约数(GCD)是两个或多个整数共有的最大的正整数因子。
程序源代码:
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
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;
}
4.2 程序8:计算最小公倍数
程序分析:最小公倍数(LCM)是两个或多个整数共有的最小的正整数倍数。
程序源代码:
#include <stdio.h>
int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
int main() {
int num1, num2;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
printf("LCM of %d and %d is %d\n", num1, num2, lcm(num1, num2));
return 0;
}
第5章 趣味素数实例
5.1 程序9:判断素数序列
程序分析:判断一个序列中的所有数字是否都是素数。
程序源代码:
#include <stdio.h>
#include <stdbool.h>
bool is_prime_in_sequence(int n) {
if (n <= 1)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (is_prime_in_sequence(num))
printf("%d is a prime number\n", num);
else
printf("%d is not a prime number\n", num);
return 0;
}
5.2 程序10:寻找素数对
程序分析:寻找一对素数,它们的和等于一个指定的值。
程序源代码:
#include <stdio.h>
#include <stdbool.h>
bool is_prime(int n) {
if (n <= 1)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
int main() {
int target_sum, num1, num2;
printf("Enter a target sum: ");
scanf("%d", &target_sum);
for (num1 = 2; num1 <= target_sum / 2; num1++) {
num2 = target_sum - num1;
if (is_prime(num1) && is_prime(num2)) {
printf("Prime pair: %d + %d = %d\n", num1, num2, target_sum);
break;
}
}
return 0;
}
第6章 趣味逻辑推理实例
6.1 程序11:逻辑谜题
程序分析:解决一个逻辑谜题,例如找出三个数字,它们满足特定的逻辑条件。
程序源代码:
#include <stdio.h>
int main() {
int a, b, c;
// 这里可以添加逻辑谜题的代码
return 0;
}
6.2 程序12:密码破解
程序分析:编写一个程序来破解一个简单的密码,例如通过尝试所有可能的组合。
程序源代码:
#include <stdio.h>
int main() {
char password[100];
printf("Enter the password: ");
scanf("%s", password);
// 这里可以添加密码破解的代码
return 0;
}
第7章 趣味游戏实例
7.1 程序13:猜数字游戏
程序分析:编写一个猜数字游戏,用户尝试猜测一个随机生成的数字。
程序源代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int number, guess;
srand(time(NULL));
number = rand() % 100 + 1;
printf("Guess the number (1-100): ");
scanf("%d", &guess);
while (guess != number) {
if (guess < number)
printf("Higher...\n");
else
printf("Lower...\n");
printf("Guess the number (1-100): ");
scanf("%d", &guess);
}
printf("Congratulations! You guessed the number %d\n", number);
return 0;
}
7.2 程序14:接龙游戏
程序分析:编写一个接龙游戏,用户需要根据前一个单词的最后一个字母来猜测下一个单词。
程序源代码:
#include <stdio.h>
#include <string.h>
int main() {
char word[100], prev_word[100];
printf("Enter the first word: ");
scanf("%s", word);
strcpy(prev_word, word);
while (1) {
printf("Enter the next word (or 'exit' to quit): ");
scanf("%s", word);
if (strcmp(word, "exit") == 0)
break;
if (strlen(word) > 1 && word[0] == prev_word[strlen(prev_word) - 1])
strcpy(prev_word, word);
else
printf("Invalid word. Try again.\n");
}
return 0;
}
第8章 趣味数组实例
8.1 程序15:数组排序
程序分析:编写一个程序来对数组进行排序,例如使用冒泡排序或选择排序算法。
程序源代码:
#include <stdio.h>
void bubble_sort(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]);
bubble_sort(arr, n);
printf("Sorted array: \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
8.2 程序16:数组查找
程序分析:编写一个程序来在一个已排序的数组中查找一个特定的元素,例如使用二分查找算法。
程序源代码:
#include <stdio.h>
int binary_search(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 = binary_search(arr, 0, n - 1, x);
if (result == -1)
printf("Element is not present in array");
else
printf("Element is present at index %d", result);
return 0;
}
第9章 趣味函数递归实例
9.1 程序17:计算阶乘(递归)
程序分析:使用递归函数计算阶乘。
程序源代码:
#include <stdio.h>
long factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Factorial of %d is %ld\n", num, factorial(num));
return 0;
}
9.2 程序18:计算斐波那契数列(递归)
程序分析:使用递归函数计算斐波那契数列。
程序源代码:
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Fibonacci number at position %d is %d\n", n, fibonacci(n));
return 0;
}
第10章 定理与猜想实例
10.1 程序19:勾股定理验证
程序分析:验证勾股定理,即在一个直角三角形中,直角边的平方和等于斜边的平方。
程序源代码:
#include <stdio.h>
#include <math.h>
int is_pythagorean_triple(int a, int b, int c) {
return (a * a + b * b == c * c) || (a * a + c * c == b * b) || (b * b + c * c == a * a);
}
int main() {
int a, b, c;
printf("Enter the sides of a triangle: ");
scanf("%d %d %d", &a, &b, &c);
if (is_pythagorean_triple(a, b, c))
printf("The triangle is a right triangle\n");
else
printf("The triangle is not a right triangle\n");
return 0;
}
10.2 程序20:素数猜想验证
程序分析:验证一个素数猜想,例如梅森素数猜想。
程序源代码:
#include <stdio.h>
#include <stdbool.h>
bool is_prime(int n) {
if (n <= 1)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
int main() {
int p;
printf("Enter a prime number: ");
scanf("%d", &p);
if (is_prime(p))
printf("%d is a prime number\n", p);
else
printf("%d is not a prime number\n", p);
return 0;
}
第11章 趣味图形实例
11.1 程序21:绘制余弦曲线
程序分析:使用字符在控制台上绘制余弦曲线。
程序源代码:
#include <stdio.h>
#include <math.h>
void draw_cosine_curve(int width, int height) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
double angle = (x * M_PI) / width;
double value = sin(angle);
int pos = (int)(value * (height / 2));
printf("%c", (pos == y) ? '*' : ' ');
}
printf("\n");
}
}
int main() {
int width = 80, height = 25;
draw_cosine_curve(width, height);
return 0;
}
11.2 程序22:绘制正弦曲线
程序分析:使用字符在控制台上绘制正弦曲线。
程序源代码:
#include <stdio.h>
#include <math.h>
void draw_sine_curve(int width, int height) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
double angle = (x * M_PI) / width;
double value = cos(angle);
int pos = (int)(value * (height / 2));
printf("%c", (pos == y) ? '*' : ' ');
}
printf("\n");
}
}
int main() {
int width = 80, height = 25;
draw_sine_curve(width, height);
return 0;
}
第12章 综合性趣味实例
12.1 程序23:狼追兔子模拟
程序分析:模拟狼追兔子的场景,根据速度差异和移动规则来决定狼是否能够