经典C语言源代码.doc
《经典C语言源代码.doc》由会员分享,可在线阅读,更多相关《经典C语言源代码.doc(37页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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或2year-;month += 12;int c = year / 100;int y = year - c * 100;int week = (c / 4) - 2 * c + (y + y / 4) + (13
2、* (month + 1) / 5) + day - 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;retur
3、n 0;1、(2)某年某月某日是第几天(一维数组)#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;f
4、or (i = 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 = i;for (j
6、 = 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_array(float a, int s
7、ize);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_array(float a, int size) int i;f
8、or (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 10void main() int aSIZE = 1,2,3,4,5,6,7,8,9,10
9、 ;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);for (; pa qa + SIZE; pa+)scanf_s(%d, pa);for (pa-; qa
10、= 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(str1);void str_cat(char str1, char str2) int i, j;i = 0;while (st
11、r1i != 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 3; i+) for (j = 0; j 2; j+)printf(%5d, aij);printf(n);printf(nb:n);for(i = 0
12、; 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 2; i+)for (j = 0; j 3; j+)printf(%d , *(*(x + i) + j);putchar(n);8、冒泡法排序一个数组#include stdi
13、o.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 = 0; j aj + 1)temp = aj;aj = aj + 1;/大的沉底,小的上浮aj + 1 = temp;printf(排序结果:n);for (i = 0; i 10; i+)print
14、f(%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, j, i;while (Am - 1B0)x = Am - 1;Am - 1 = B0; /交换Am-1和B0j = 1;while (jn & Bj= 0 & Aix)Ai + 1 = Ai-; /寻找B0的
15、插入位置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(A, B, 3, 5);printf(n输出第一个数组:);for (i = 0; i3; i+)printf(%d , Ai);printf(nn);printf(输出第二个数组:);for (i = 0; i5; i+)printf(
16、%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)continue;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;
17、 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);printf(%d %dn, b, c);printf(%d %d %dn, d, e, f);11、输入一串字符,升序排序,折半查找其中一字符#include void *sortString(char unsort, int length) for (int i = 0; i
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 经典 语言 源代码
限制150内