1、经典C语言源代码经典C语言源代码1、(1)某年某月某日是星期几#includeint main() int year, month, day; while (scanf_s(%d%d%d, &year, &month, &day) != EOF) if (month = 1 | month = 2)/判断month是否为1或2 year-; month += 12; int c = year / 100; int y = year - c * 100; int week = (c / 4) - 2 * c + (y + y / 4) + (13 * (month + 1) / 5) + day
2、 - 1; while (week0) week += 7; week %= 7; switch (week) case 1:printf(Mondayn); break; case 2:printf(Tuesdayn); break; case 3:printf(Wednesdayn); break; case 4:printf(Thursdayn); break; case 5:printf(Fridayn); break; case 6:printf(Saturdayn); break; case 0:printf(Sundayn); break; return 0;1、(2)某年某月某
3、日是第几天(一维数组)#include stdio.hvoid main() int i, flag, year, month, day, dayth; int month_day = 0,31,28,31,30,31,30,31,31,30,31,30,31 ; printf(请输入年/月/日:n); scanf_s(%d/%d/%d, &year, &month, &day); dayth = day; flag = (year % 400 = 0) | (year % 4 = 0 & year % 100 != 0); if (flag) month_day2 = 29; for (i
4、= 1; i month; i+) dayth = dayth + month_dayi; printf(%d/%d/%d是第%d天n, year, month, day, dayth);2、30个数中找最小的数及其位置#include stdio.h# define SIZE 30void main() int i; float dataSIZE; int min; printf(请输入%d个浮点数:n,SIZE); for (i = 0; i SIZE; i+) /scanf_s(%f, &datai); datai = rand() % 30 + 1; printf(%f、, datai
5、); min = 0; for (i = 1; i SIZE; i+) if (datai datamin) min = i; printf(最小值是%5.2f,位置是%5dn, datamin, min);3、30个数从小到大排序(1)#include stdio.h# define SIZE 30void main() int i,j; float dataSIZE,temp; int min; printf(请输入%d个整型数:n,SIZE); for (i = 0; i SIZE; i+) scanf_s(%f, &datai); for (i = 0; i SIZE; i+) min
6、 = i; for (j = i + 1; j SIZE; j+) if (dataj datamin) min = j; temp = datamin; datamin = datai; datai = temp; printf(n排序后的结果是:n); for (i = 0; i SIZE; i+) printf(%5.2f, datai);(2)模块化程序(数组名作为函数参数)#include stdio.h# define SIZE 5void accept_array(float a, int size);void sort(float a, int size);void show_
7、array(float a, int size);void main() float scoreSIZE; accept_array(score, SIZE); printf(排序前:); show_array(score, SIZE); sort(score, SIZE); printf(排序后:); show_array(score, SIZE);void accept_array(float a, int size) int i; printf(请输入%d个分数:, size); for (i = 0; i size; i+) scanf_s(%f, &ai);void show_arr
8、ay(float a, int size) int i; for (i = 0; i size; i+) printf( %5.2f, ai); printf(n);void sort(float a,int size) int i, min, j; float temp; for (i = 0; i SIZE; i+) min = i; for (j = i + 1; j SIZE; j+) if (aj amin) min = j; temp = amin; amin = ai; ai = temp; 4、(1)指针加减:#include stdio.h#define SIZE 10voi
9、d main() int aSIZE = 1,2,3,4,5,6,7,8,9,10 ; int *pa, i; pa = &a0; /pa=a; printf(n); for (i = 0; i SIZE; i+) printf( %d, *pa); /printf( %d, *(pa+1); pa+; (2)指针比较:#include stdio.h#define SIZE 10void main() int aSIZE = 1,2,3,4,5,6,7,8,9,10 ; int *pa, i; int *qa; pa = qa = &a0; printf(请输入%d整型数:,SIZE); f
10、or (; pa qa + SIZE; pa+) scanf_s(%d, pa); for (pa-; qa = pa; pa-) printf( %d, *pa);5、两字符串相连:#include stdio.h#include string.hvoid str_cat(char str1, char str2);void main() int i, j; char str1160; char str280; printf(请输入第一个字符串:); gets(str1); printf(请输入第二个字符串:); gets(str2); str_cat(str1, str2); puts(s
11、tr1);void str_cat(char str1, char str2) int i, j; i = 0; while (str1i != 0) i+; j = 0; while (str2j != 0) str1i = str2j; i+; j+; str1i = 0;6、二维数组(a,b转置)#include stdio.hvoid main() int i, j, b23; int a32 = 1,2,3,4,5,6 ; for (i = 0; i 2; i+) for (j = 0; j 3; j+) bij = aji; printf(na:n); for (i = 0; i
12、3; i+) for (j = 0; j 2; j+) printf(%5d, aij); printf(n); printf(nb:n); for(i = 0; i 2; i+) for (j = 0; j 3; j+) printf(%5d, bij); printf(n); 7、输入一个二维数组并输出(指针)#include stdio.hvoid main() int x23; int i, j; for (i = 0; i 2; i+) for (j = 0; j 3; j+) scanf_s(%d, *(x + i) + j); putchar(n); for (i = 0; i
13、2; i+) for (j = 0; j 3; j+) printf(%d , *(*(x + i) + j); putchar(n); 8、冒泡法排序一个数组#include stdio.h#define size 10void maopao(int a);void main() int a10; int i; printf(请输入10个整数:n); for (i = 0; i 10; i+) scanf_s(%d, &ai); maopao(a);void maopao(int a) int i, j, temp; for (i = 0; i 9; i+) /进行9轮排序 for (j =
14、 0; j aj + 1) temp = aj; aj = aj + 1;/大的沉底,小的上浮 aj + 1 = temp; printf(排序结果:n); for (i = 0; i 10; i+) printf(%4d, ai);9、两数组A,B,要求AB,如A:4,7,9B:1,3,5,8,9变换后A:1,3,5B:4,7,8,9,9#include void ReArranger(int* A, int* B, int m, int n) /A和B是各有m个和n个整数的非降序数组,本算法将B数组元素逐个插入到A中,使A中各元素均不大于B中各元素,且两数组仍保持非降序排列。 int x,
15、 j, i; while (Am - 1B0) x = Am - 1; Am - 1 = B0; /交换Am-1和B0 j = 1; while (jn & Bj= 0 & Aix) Ai + 1 = Ai-; /寻找B0的插入位置 Ai + 1 = x; void main() /这里主要介绍算法思想,主函数就简单写了 int A3, B5, i; printf(输入第一个数组:); for (i = 0; i3; i+) scanf_s(%d, &Ai); printf(n输入第二个数组:); for (i = 0; i5; i+) scanf_s(%d, &Bi); ReArranger
16、(A, B, 3, 5); printf(n输出第一个数组:); for (i = 0; i3; i+) printf(%d , Ai); printf(nn); printf(输出第二个数组:); for (i = 0; i5; i+) printf(%d , Bi); printf(n);10、符合1+6+3=3+2+5=1+4+5有哪几组 A 1 B C 6 4D E F到 3 2 5#include void main() int a, b, c, d, e, f; for (a = 1; a = 6; a+) for (b = 1; b = 6; b+) if (b = a) con
17、tinue; for (c = 1; c = 6; c+) if (c = a) | (c = b) continue; for (d = 1; d = 6; d+) if (d = a) | (d = b) | (d = c) continue; for (e = 1; e = 6; e+) if (e = a) | (e = b) | (e = c) | (e = d) continue; f = 21 - (a + b + c + d + e); if (a + b + d = d + e + f) & (a + b + d = a + c + f) printf( %dn, a); p
18、rintf(%d %dn, b, c); printf(%d %d %dn, d, e, f); 11、输入一串字符,升序排序,折半查找其中一字符#include void *sortString(char unsort, int length) for (int i = 0; i length; i+) for (int j = i + 1; j unsortj) int temp = unsortj; unsortj = unsorti; unsorti = temp; void main() char s150; gets(s1); char value; scanf_s(%c, &va
19、lue); printf(s1:%sn, s1); printf(value:%cn, value); int length = strlen(s1); printf(length:%dn, length); sortString(s1, length); printf(s1:%sn, s1); int start = 0, end = length - 1; int mid = (end + start) / 2; while (start s1mid) start = mid+1; else end = mid-1; if (mid) printf(该字符在已知字符串中,即在第%d个n,m
20、id); else printf(该字符不在已知字符串中n);12、100-300和500-700直接素数,m不被2到根号m直接任一整除#includeint isprime(int n) if (n2) return 0; for (int i = 2; in / 2; i+) if (n%i = 0) return 0; return 1;void main() int i, k = 0; for (i = 100; i = 300; i+) if (isprime(i) printf(%3d , i); k+; if (k % 10 = 0) printf(n); for (i = 50
21、0; i = 700; i+) if (isprime(i) printf(%3d , i); k+; if (k % 10 = 0) printf(n); printf(n);13、判断一个数是否是素数#include#includevoid main() int m, i, k; printf(请输入一个整数:); scanf_s(%d, &m); k = (int)sqrt(m); for (i = 2; i k) printf(%d 是素数。n, m); else printf(%d 不是素数。n, m);14、一个数是否含有数字5#include #include bool is5N
22、um(int num) int temp = num % 10; while (temp != 5 & num 10) num = num / 10; temp = num % 10; if (temp != 5) return false; else return true;void main() int num; printf(输入一个数:n); scanf_s(%d, &num); if (is5Num(num) printf(含5n); else printf(不含5n);15、一个排好序的数组,插入一个数#include void main() int a11 = 1,2,3,4,5
23、,6,7,8,9,10 ; int num; printf(插入前数组为:n); for (int i = 0; i a9) a10 = num; else for (int i = 0; i 10;i+) if (num = i; j-) aj + 1 = aj; ai = num; break; printf(插入后数组为:n); for (int i = 0; i = 10; i+) printf(%3d, ai); printf(n);16、牛顿迭代法:#include #include double func(double x) /函数 return x*x*x + 2.0*x*x
24、+ 3.0*x + 4.0;double func1(double x) /导函数 return 3 * x*x + 4 * x + 3;void Newton(double x0,double precision)/迭代次数 double x1; int k; if (func1(x0) = 0.0) /若通过初值,函数返回为0 printf(迭代过程中倒数为0!n); return; x1 = x0 - func(x0) / func1(x0);/进行牛顿迭代计算 while (!(func1(x1 - x0) precision | fabs(func(x1) precision) x0 = x1;/准备下一次迭代 if (func1(x0) = 0.0)/若通过初值,函数返回值为0 printf(迭代过程中倒数为0!n); x1 = x0 - fu
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1