C 语言入门必备的 18 个经典程序
-
输出 9*9 乘法口诀。共 9 行 9 列,i 控制行,j 控制列。
-
古典问题:有一对兔子,从出生后第 3 个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月兔子总数为多少?兔子的规律为数列
1,1,2,3,5,8,13,21...
-
判断 101-200 之间有多少个素数,并输出所有素数及素数的个数。
程序分析:判读素数的方法:用一个数分别去除 2 到这个数的算术平方根,如果能被整除,则证明此数不是素数,反之是素数。
#include "math.h" #include "stdio.h" const int MIN = 101; const int MAX = 200; int main() { int count = 0; for (int i = MIN; i <= MAX; i += 2) { int j; for (j = 2; j <= sqrt(i); j++) { if (i % j == 0) { break; } } if (j > sqrt(i)) { printf("%d ", i); count++; } } printf("\ncount=%d\n", count); return 0; }
-
一个数如果恰好等于它的因子之和,这个数就称为“完数”。假如 6=1+2+3。编程找出 1000 以内的所有完数。
#include "stdio.h" const int MIN = 3; const int MAX = 1000; int main() { int a[1024]; for (int i = MIN; i < MAX; i++) { int sum = 0; int j = 1; int count = 0; while (j < i) { if (i % j == 0) { sum = sum + j; a[count] = j; count++; } j++; } if (sum == i) { printf("%d=1", i); for (int k = 0; k < count; k++) { printf("+%d", a[k]); } printf("\n"); } count = 0; } return 0; }
-
下面程序的功能是将一个 4*4 的数组进行逆时针旋转 90 度后输出,要求原始数组的数据随机输入,新数组以 4 行 4 列的方式输出。
#include "stdio.h" const int N = 4; int main() { int src[N][N]; int dst[N][N]; printf("src:\n"); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf("%d", &src[i][j]); dst[N - 1 - j][i] = src[i][j]; } } printf("dst:\n"); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { printf("%6d", dst[i][j]); } printf("\n"); } return 0; }
-
编程打印直角杨辉三角形
#include "stdio.h" const int N = 6; int main() { int a[N][N]; for (int i = 0; i < N; i++) { a[i][0] = 1; a[i][i] = 1; } for (int i = 2; i < N; i++) { for (int j = 1; j <= i - 1; j++) { a[i][j] = a[i - 1][j] + a[i - 1][j - 1]; } } for (int i = 0; i < N; i++) { for (int j = 0; j <= i; j++) { printf("%4d", a[i][j]); } printf("\n"); } return 0; }
-
通过键盘输入 3 名学生 4 门课程的成绩,分别求每个学生的平均成绩和每门课程的平均成绩。
要求所有成绩放入一个 4 行 5 列的数组中,输入时同一人数据间用空格,不同人用回车,其中最后一列和最后一行分别放每个学生的平均成绩、每门课程的平均成绩及班级总平均分。
#include "stdio.h" const int STU_COUNT = 3; const int COURSE_COUNT = 4; int main() { float a[STU_COUNT + 1][COURSE_COUNT + 1]; for (int i = 0; i < STU_COUNT; i++) { for (int j = 0; j < COURSE_COUNT; j++) { scanf("%f", &a[i][j]); } } for (int i = 0; i < STU_COUNT; i++) { float sum = 0; for (int j = 0; j < COURSE_COUNT; j++) { sum += a[i][j]; a[i][COURSE_COUNT] = sum / COURSE_COUNT; } } for (int j = 0; j < COURSE_COUNT + 1; j++) { float sum = 0; for (int i = 0; i < STU_COUNT; i++) { sum += a[i][j]; a[STU_COUNT][j] = sum / STU_COUNT; } } for (int i = 0; i < STU_COUNT + 1; i++) { for (int j = 0; j < COURSE_COUNT + 1; j++) { printf("%6.2f", a[i][j]); } printf("\n"); } return 0; }
-
实现将输入的字符串反序输出,如输入
windows
输出swodniw
。 -
从字符数组
s
中删除存放在c
中的字符。 -
编写一个
void sort(int *x, int n)
实现将x
数组中的n
个数据从大到小排序。n
及数组元素在主函数中输入。将结果显示在屏幕上并输出到文件p9_1.out
中。#include "stdio.h" void sort(int* x, int n) { for (int i = 0; i < n - 1; i++) { int k = i; for (int j = i + 1; j < n; j++) { if (x[j] > x[k]) { k = j; } } if (k != i) { int temp = x[i]; x[i] = x[k]; x[k] = temp; } } } const int N = 10; int main() { FILE* fp = fopen("pg_1.out", "w"); int a[N]; for (int i = 0; i < N; i++) { scanf("%d", &a[i]); } sort(a, N); for (int i = 0; i < N; i++) { printf("%d ", a[i]); fprintf(fp, "%d ", a[i]); } fclose(fp); printf("\n"); return 0; }
-
已知数组
a
中的元素已经按由小到大顺序排列,以下程序的功能是将输入的一个数插入数组a
中,插入后,数组a
的元素仍然由小到大顺序排列。#include "stdio.h" int main() { int a[] = {0, 12, 17, 20, 25, 28, 30}; int len = sizeof(a) / sizeof(a[0]); int x; printf("x="); scanf("%d", &x); a[0] = x; int i = len - 1; while (a[i] > x) { a[i + 1] = a[i]; i--; } a[i + 1] = x; len++; for (int j = 1; j < len; j++) { printf("%8d ", a[j]); } printf("\n"); return 0; }
-
编写函数
replace(char* s, char c1, char c2)
实现将s
所指的字符串中所有字符c1
用c2
替换。字符串、字符
c1
和c2
均在主函数中输入,将原始字符串和替换后的字符串显示在屏幕上,并输出到文件p10_2.out
中。#include "stdio.h" void replace(char* s, char c1, char c2) { while (*s != '\0') { if (*s == c1) { *s = c2; } s++; } } int main() { char s[100]; char c1; char c2; scanf("%s %c %c", s, &c1, &c2); replace(s, c1, c2); FILE* fp = fopen("p10_2.out", "w"); printf("%s\n", s); fprintf(fp, "%s\n", s); return 0; }
-
在一个字符
s1
中查找一字串s2
,若存在则返回字串在主串中的起始位置,不存在则返回-1
。#include "stdio.h" #include "string.h" int search(char s1[], char s2[]) { int len = strlen(s2); int i = 0; while (s1[i] != '\0') { int j; for (j = 0; j < len; j++) { if (s1[i + j] != s2[j]) { break; } } if (j >= len) { return i; } else { i++; } } return -1; } int main() { char s1[] = "this is"; char s2[] = "is"; printf("%d\n", search(s1, s2)); return 0; }
-
用指针变量输出结构体数组元素。
#include "stdio.h" struct student { int num; char* name; char sex; int age; }; int main() { struct student stu[] = { {1001, "lihua", 'F', 18}, {1002, "linxing", 'M', 19}, {1003, "huangke", 'F', 19}, {1004, "fengshou", 'F', 19}, {1005, "Wangming", 'M', 18} }; int len = sizeof(stu) / sizeof(stu[0]); for (struct student* p = stu; p < stu + len; p++) { printf("%-10d %-10s %-10c %-10d\n", p->num, p->name, p->sex, p->age); } return 0; }
-
建立一个有三个节点的简单链表。
#include "stdio.h" struct student { int num; char* name; char sex; int age; struct student* next; }; int main() { struct student a = {1001, "lihua", 'F', 18}; struct student b = {1002, "linxing", 'M', 19}; struct student c = {1003, "huangke", 'F', 19}; struct student* head = &a; a.next = &b; b.next = &c; c.next = NULL; struct student* p = head; while (p != NULL) { printf("%-10d %-10s %-10c %-10d\n", p->num, p->name, p->sex, p->age); p = p->next; } return 0; }
-
输入一个字符串,判断其是否为回文。
回文字符串是指从左到右读和从右到左读完全相同的字符串。
-
冒泡排序,从小到大,排序后结果输出到屏幕及文件
myf2.out
。#include "stdio.h" #include "string.h" void sort(int a[], int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (a[j] > a[j + 1]) { int temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } } int main() { int a[] = {12, 45, 7, 8, 96, 4, 10, 48, 2, 46}; int len = sizeof(a) / sizeof(a[0]); sort(a, len); FILE* fp = fopen("myf2.out", "w"); for (int i = 0; i < len; i++) { printf("%-4d", a[i]); fprintf(fp, "%-4d", a[i]); } printf("\n"); fclose(fp); return 0; }
-
编写函数
count_pi
,利用公式计算pi
的 近似值,当某一项的值小于 10-5 时,认为达到精度要求。将结果显示在屏幕上并输出到文件
p7_3.out
中。#include "stdio.h" #include "string.h" double count_pi(double eps) { int m = 1; double temp = 1.0; double s = 0; while (temp >= eps) { s += temp; temp = temp * m / (2 * m + 1); m++; } return 2 * s; } int main() { double pi = count_pi(1e-5); FILE* fp = fopen("myf2.out", "w"); printf("%lf\n", pi); fprintf(fp, "%lf\n", pi); fclose(fp); return 0; }