经典c++程序100例(完整版)资料

上传人:w****i 文档编号:107691780 上传时间:2019-10-20 格式:PDF 页数:87 大小:376.65KB
返回 下载 相关 举报
经典c++程序100例(完整版)资料_第1页
第1页 / 共87页
经典c++程序100例(完整版)资料_第2页
第2页 / 共87页
经典c++程序100例(完整版)资料_第3页
第3页 / 共87页
经典c++程序100例(完整版)资料_第4页
第4页 / 共87页
经典c++程序100例(完整版)资料_第5页
第5页 / 共87页
点击查看更多>>
资源描述

《经典c++程序100例(完整版)资料》由会员分享,可在线阅读,更多相关《经典c++程序100例(完整版)资料(87页珍藏版)》请在金锄头文库上搜索。

1、经典经典 c c 程序程序 100100 例例 【程序 1】 题目:有 1、2、3、4 个数字,能组成多少个互不相同且无重复数字的三位数? 都是多少? 1.程序分析:可填在百位、十位、个位的数字都是 1、2、3、4。组成所有的排 列后再去 掉不满足条件的排列。 2.程序源代码: main() int i,j,k; printf(“n“); for(i=1;iy 则将 x 与 y 的值进行交换, 然后再用 x 与 z 进行比较,如果 xz 则将 x 与 z 的值进行交换,这 样能使 x 最小。 2.程序源代码: main() int x,y,z,t; scanf(“%d%d%d“, if (xy

2、) 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.程序分析:可先用*号在纸上写出字母 C,再分行输出。 2.程序源代码: #include “stdio.h“ main() printf(“Hello C-world!n“); printf(“ *n“); printf(“ *n“); printf(“ * n“);

3、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); 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“

4、,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;ib)?a:b 这是条件运算符的基本例子。 2.程序源代码: main() int score; char grade; printf(“please input a scoren“); scanf(“%d“, grade=score=90?A:(score=60?B:C); printf(“%d belo

5、ngs to %c“,score,grade); = 【程序 16】 题目:输入两个正整数 m 和 n,求其最大公约数和最小公倍数。 1.程序分析:利用辗除法。 2.程序源代码: main() int a,b,num1,num2,temp; printf(“please input two numbers:n“); scanf(“%d,%d“, if(num1 temp=num1; num1=num2; num2=temp; a=num1;b=num2; while(b!=0)/*利用辗除法,直到 b 为 0 为止*/ temp=a%b; a=b; b=temp; printf(“gongyu

6、eshu:%dn“,a); printf(“gongbeishu:%dn“,num1*num2/a); = 【程序 17】 题目: 输入一行字符, 分别统计出其中英文字母、 空格、 数字和其它字符的个数。 1.程序分析:利用 while 语句,条件为输入的字符不为n. 2.程序源代码: #include “stdio.h“ main() char c; int letters=0,space=0,digit=0,others=0; printf(“please input some charactersn“); while(c=getchar()!=n) if(c=a else others+

7、; printf(“all in all:char=%d space=%d digit=%d others=%dn“,letters, space,digit,others); = 【程序 18】 题目:求 s=a+aa+aaa+aaaa+aa.a 的值,其中 a 是一个数字。例如 2+22+222+2222+22222(此时 共有 5 个数相加),几个数相加有键盘控制。 1.程序分析:关键是计算出每一项的值。 2.程序源代码: main() int a,n,count=1; long int sn=0,tn=0; printf(“please input a and nn“); scanf(

8、“%d,%d“, printf(“a=%d,n=%dn“,a,n); while(count=50) again=TRUE; else again=FALSE; = 【程序 47】 题目:宏#define 命令练习(2) 1.程序分析: 2.程序源代码: #include “stdio.h“ #define exchange(a,b) /*宏定义中允许包含两道衣裳命令的情形,此时必 须在最右边加上“*/ int t; t=a; a=b; b=t; void main(void) int x=10; int y=20; printf(“x=%d; y=%dn“,x,y); exchange(x,

9、y); printf(“x=%d; y=%dn“,x,y); = 【程序 48】 题目:宏#define 命令练习(3) 1.程序分析: 2.程序源代码: #define LAG #define SMA y)?x:y #define MINIMUM(x,y) (xy)?y:x void main() int a=10,b=20; #ifdef MAX printf(“40: The larger one is %dn“,MAXIMUM(a,b); #else printf(“40: The lower one is %dn“,MINIMUM(a,b); #endif #ifndef MIN p

10、rintf(“40: The lower one is %dn“,MINIMUM(a,b); #else printf(“40: The larger one is %dn“,MAXIMUM(a,b); #endif #undef MAX #ifdef MAX printf(“40: The larger one is %dn“,MAXIMUM(a,b); #else printf(“40: The lower one is %dn“,MINIMUM(a,b); #endif #define MIN #ifndef MIN printf(“40: The lower one is %dn“,M

11、INIMUM(a,b); #else printf(“40: The larger one is %dn“,MAXIMUM(a,b); #endif = 【程序 50】 题目:#include 的应用练习 1.程序分析: 2.程序源代码: test.h 文件如下: #define LAG #define SMA 4; c=(0LINES) setcolor(color); color=(color=MAXCOLOR)?0:+color; closegraph(); = 【程序 61】 题目:打印出杨辉三角形(要求打印出 10 行如下图) 1.程序分析: 1 1 1 1 2 1 1 3 3 1

12、1 4 6 4 1 1 5 10 10 5 1 2.程序源代码: main() int i,j; int a1010; printf(“n“); for(i=0;in3) 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】 题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出 数组。

13、 1.程序分析:谭浩强的书中答案有问题。 2.程序源代码: main() int number10; input(number); max_min(number); output(number); input(number) int number10; int i; for(i=0;i0) move(array,n,m); = 【程序 69】 题目:有 n 个人围成一圈,顺序排号。从第一个人开始报数(从 1 到 3 报数), 凡报到 3 的人退出 圈子,问最后留下的是原来第几号的那位。 1. 程序分析: 2.程序源代码: #define nmax 50 main() int i,k,m,n,numnmax,*p; printf(“please input the total of numbers:“); scanf(“%d“, p=num; for(i=0;inext=(link)malloc(sizeof(node); if(i=4) ptr-next=NULL; else ptr=ptr-next; pt

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

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