经典C语言程序100例(word文档版)

上传人:woxinch****an2018 文档编号:38980588 上传时间:2018-05-10 格式:DOC 页数:118 大小:357.72KB
返回 下载 相关 举报
经典C语言程序100例(word文档版)_第1页
第1页 / 共118页
经典C语言程序100例(word文档版)_第2页
第2页 / 共118页
经典C语言程序100例(word文档版)_第3页
第3页 / 共118页
经典C语言程序100例(word文档版)_第4页
第4页 / 共118页
经典C语言程序100例(word文档版)_第5页
第5页 / 共118页
点击查看更多>>
资源描述

《经典C语言程序100例(word文档版)》由会员分享,可在线阅读,更多相关《经典C语言程序100例(word文档版)(118页珍藏版)》请在金锄头文库上搜索。

1、【程序 1】 题目:有 1、2、3、4 个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 1.程序分析:可填在百位、十位、个位的数字都是 1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。 2.程序源代码: main() int i,j,k; printf(“n“); for(i=1;i2)/*如果是闰年且月份大于 2,总天数应该加一天*/ sum+; printf(“It is the %dth day.“,sum); =【程序 5】 题目:输入三个整数 x,y,z,请把这三个数由小到大输出。 1.程序分析:我们想办法把最小的数放到 x 上,先将 x 与 y 进行比较,如

2、果 xy 则将 x 与 y 的值进行交换,然后再用 x 与 z 进行比较,如果 xz 则将 x 与 z 的值进行交换,这样能使 x 最小。 2.程序源代码: main() int x,y,z,t; scanf(“%d%d%d“, if (xy) t=x;x=y;y=t; /*交换 x,y 的值*/ if(xz) t=z;z=x;x=t;/*交换 x,z 的值*/ if(yz) t=y;y=z;z=t;/*交换 z,y 的值*/ printf(“small to big: %d %d %dn“,x,y,z); =【程序 6】 题目:用*号输出字母 C 的图案。 1.程序分析:可先用*号在纸上写出

3、字母 C,再分行输出。 2.程序源代码: #include “stdio.h“ main() printf(“Hello C-world!n“); printf(“ *n“); printf(“ *n“); printf(“ * n“); printf(“ *n“); =【程序 7】 题目:输出特殊图案,请在 c 环境中运行,看一看,Very Beautiful! 1.程序分析:字符共有 256 个。不同字符,图形不一样。 2.程序源代码: #include “stdio.h“ main() char a=176,b=219; printf(“%c%c%c%c%cn“,b,a,a,a,b);

4、printf(“%c%c%c%c%cn“,a,b,a,b,a); printf(“%c%c%c%c%cn“,a,a,b,a,a); printf(“%c%c%c%c%cn“,a,b,a,b,a); printf(“%c%c%c%c%cn“,b,a,a,a,b); =【程序 8】 题目:输出 9*9 口诀。 1.程序分析:分行与列考虑,共 9 行 9 列,i 控制行,j 控制列。 2.程序源代码: #include “stdio.h“ main() int i,j,result; printf(“n“); for (i=1;ik,但 n 能被 k 整除,则应打印出 k 的值,并用 n 除以k 的

5、商,作为新的正整数你 n, 重复执行第一步。 (3)如果 n 不能被 k 整除,则用 k+1 作为 k 的值,重复执行第一步。 2.程序源代码: /* zheng int is divided yinshu*/ main() int n,i; printf(“nplease input a number:n“); scanf(“%d“, printf(“%d=“,n); for(i=2;i=90 分的同学用 A 表示,60-89 分之间的用 B 表示, 60 分以下的用 C 表示。 1.程序分析:(ab)?a:b 这是条件运算符的基本例子。 2.程序源代码: main() int score;

6、 char grade; printf(“please input a scoren“); scanf(“%d“, grade=score=90?A:(score=60?B:C); printf(“%d belongs to %c“,score,grade); = 【程序 16】 题目:输入两个正整数 m 和 n,求其最大公约数和最小公倍数。1. temp=num1; num1=num2; num2=temp; a=num1;b=num2; 2. 程序分析:利用辗除法。 2.程序源代码: main() int a,b,num1,num2,temp; printf(“please input t

7、wo numbers:n“); scanf(“%d,%d“, if(num1=a/*第一天的桃子数是第 2 天桃子数加 1 后的 2 倍*/ x2=x1; day-; printf(“the total is %dn“,x1); = 【程序 22】 题目:两个乒乓球队进行比赛,各出三人。甲队为 a,b,c 三人,乙队为 x,y,z 三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a 说他不和 x 比,c 说他不和 x,z 比,请编程序找出三队赛手的名单。 1.程序分析:判断素数的方法:用一个数分别去除 2 到 sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。 2.程序源代

8、码: main() char i,j,k;/*i 是 a 的对手,j 是 b 的对手,k 是 c 的对手*/ for(i=x;i void main() char letter; printf(“please input the first letter of somedayn“); while (letter=getch()!=Y)/*当所按字母为 Y 时才结束*/ switch (letter) case S:printf(“please input second lettern“); if(letter=getch()=a) printf(“saturdayn“); else if (l

9、etter=getch()=u) printf(“sundayn“); else printf(“data errorn“); break; case F:printf(“fridayn“);break; case M:printf(“mondayn“);break; case T:printf(“please input second lettern“); if(letter=getch()=u) printf(“tuesdayn“); else if (letter=getch()=h) printf(“thursdayn“); else printf(“data errorn“); br

10、eak; case W:printf(“wednesdayn“);break; default: printf(“data errorn“); = 【程序 32】 题目:Press any key to change color, do you want to try it. Please hurry up! 1.程序分析: 2.程序源代码: #include void main(void) int color; for (color = 0; color void main(void) clrscr();/*清屏函数*/ textbackground(2); gotoxy(1, 5);/*定

11、位函数*/ cprintf(“Output at row 5 column 1n“); textbackground(3); gotoxy(20, 10); cprintf(“Output at row 10 column 20n“); = 【程序 34】 题目:练习函数调用 1. 程序分析: 2.程序源代码: #include void hello_world(void) printf(“Hello, world!n“); void three_hellos(void) int counter; for (counter = 1; counter void main(void) int co

12、lor; for (color = 1; color #include “math.h“ #define N 101 main() int i,j,line,aN; for(i=2;iaj) min=j; tem=ai; ai=amin; amin=tem; /*output data*/ printf(“After sorted n“); for(i=0;iend) a10=number; else for(i=0;inumber) temp1=ai; ai=number; for(j=i+1;j4; c=(0=RIGHT) dx1=-dx1; if(y1=BOTTOM) dy1=-dy1;

13、 if(x2=RIGHT) dx2=-dx2; if(y2=BOTTOM) dy2=-dy2; if(+countLINES) setcolor(color); color=(color=MAXCOLOR)?0:+color; closegraph(); 【程序 61】 题目:打印出杨辉三角形(要求打印出 10 行如下图) 1.程序分析: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 2.程序源代码: main() int i,j; int a1010; printf(“n“); for(i=0;in2) swap(pointer1,pointer2); if(n1n3) swap(pointer1,pointer3); if(n2n3) swap(pointer2,pointer3); printf(“the sorted numbers are:%d,%d,%dn“,n1,n2,n3); swap(p1,p2) int *p1,*p2; int p; p=*p1;*p1=*p2;*p2=p; = 【程序 67】 题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。 1.程序分析:谭浩强的书中答案有问题。 2.程序源代码: main() int number10; input(numbe

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号