个人整理的C例题

上传人:痛*** 文档编号:202075529 上传时间:2023-04-21 格式:PDF 页数:58 大小:4.16MB
收藏 版权申诉 举报 下载
个人整理的C例题_第1页
第1页 / 共58页
个人整理的C例题_第2页
第2页 / 共58页
个人整理的C例题_第3页
第3页 / 共58页
资源描述:

《个人整理的C例题》由会员分享,可在线阅读,更多相关《个人整理的C例题(58页珍藏版)》请在装配图网上搜索。

1、【程序1】题目:输入三个整数x,y,z,请把这三个数由小到大输出。1 .程序分析:我们想办法把最小的数放到x 上,先将x 与y 进行比较,如果x y 则将x 与y 的值进行交换,然后再用x 与z 进行比较,如果x z 则将x 与z 的值进行交换,这样能使x 最小。2.程序源代码:main()(int x,y,z,t;scanf(%d%d%d,&x,&y,&z);if(xy)t=x;x=y;y=t;/*交换x,y 的值*/if(x z)t=z;z=x;x=t;/*交换 x,z 的值*/if(yz)t=y;y=z;z=t;/*交换 z,y 的值*/printf(nsmall to big:%d%d

2、%dn,x,y,z);【程序2】题目:用*号输出字母C 的图案。1.程序分析:可先用 号在纸上写出字母C,再分行输出。2.程序源代码:#include stdio.hmain()printf(HHello C-world!nM);printf(H*nH);printf(H*nH);printf(M*nM);printf(H*n);【程序3】题目:输出特殊图案,请在c 环境中运行,看 看,Very Beautiful!1.程序分析:字符共有256个。不同字符,图形不一样。2.程序源代码:#include stdio.hmain()char a=176,b=219;printf(%c%c%c%c%

3、cn,b,a,a,a,b);printf(M%c%c%c%c%cnM,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);【程序4】题目:输出9*9 口诀。1.程序分析:分行与列考虑,共 9 行 9 列,i 控制行,j 控制列。2.程序源代码:#include stdio.hmain()(int i,j,result;printf(n);for(i=1 ;i 10;i+)for(j=1;j10;j+)(result=i*j;printf(%d*%

4、d=%-3d,i,j,result);/*-3d 表示左对齐,占3 位*/)printf(n);/*每一行后换行*/)【程序5】题目:要求输出国际象根棋盘。1.程序分析:用 i 控制行,j 来控制列,根据i+j的和的变化来控制输出黑方格,还是白方格。2.程序源代码:#include stdio.hmain()(int i,j;for(i=0;i8;i+)(for(j=0;j8;j+)if(i+j)%2=0)printf(%c%c,219,219);elseprintf();printf(n);)【程序10题目:打印楼梯,同时在楼梯上方打印两个笑脸。1 .程序分析:用 i 控制行,j 来控制列,

5、j 根据i 的变化来控制输出黑方格的个数。2.程序源代码:#include stdio.hmain()(int i,j;printf(11n);/*输出两个笑脸*/for(i=1 ;i 11;i+)(for(j=1;j=i;j+)printf(%c%c,219,219);printf(n);)经典C 语言程序设计100例 11-20【程序11题目:古典问题:有对兔子,从出生后第3 个月起每个月都生对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?1.程序分析:兔子的规律为数列1,1,2,3,5,8,13,21.2.程序源代码:main()(long f1

6、,f2;int i;f 1 =f2=1 jfor(i=1 ;i=20;i+)printf(%12ld%12ld,f1,f2);if(i%2=0)printf(n);/*控制输出,每行四个*/f1=f1+f2;/*前两个月加起来赋值给第三个月*/f2=f1+f2;/*前两个月加起来赋值给第三个月*/【程序12题目:判断101-200之间有多少个素数,并输出所有素数。1.程序分析:判断素数的方法:用一个数分别去除2 到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。2.程序源代码:#include math.hmain()(int m,i,k,h=0,leap=1;printf(

7、n);for(m=101;m=200;m+)k=sqrt(m+1);for(i=2;i=k;i+)if(m%i=O)leap=O;break;if(leap)printf(H%-4d,m);h+;if(h%10=0)printf(Hn);)leap=1;printf(nThe total is%d,h);【程序13题目:打印出所有的“水仙花数”,所谓“水仙花数”是指个三位数,其各位数字立方和等于该数本身。例如:153是一个“水仙花数”,因为153=1 的三次方+5 的三次方+3 的三次方。1.程序分析:利用fo r循环控制100-999个数,每个数分解出个位,十位,百位。2.程序源代码:mai

8、n()int i,j,k,n;printf(water flowernumber is:);for(n=100;n 1000;n+)(i=n/100;/*分解出百位*/j=n/10%10;/*分解出十位*/k=n%10;/*分解出个位*/if(i*100+j*10+k=i*i*i+j*j*j+k*k*k)(printf(%-5d,n);)printf(n);【程序14题目:将个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。程序分析:对 n 进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。(2)

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

10、core;char grade;printf(Mplease input a scoren);scanf(%d,&score);grade=score=90?,A:(score=60?B:C);printf(H%d belongs to%c,score,grade);【程序16题目:输入两个正整数m和n,求其最大公约数和最小公倍数。1.程序分析:利用辗除法。2.程序源代码:main()int a,b,num1,num2,ternp;printf(nplease input two numbers:n);scanf(%d,%d,&num 1 ,&num2);if(num 1 =宣&cv=z|c=

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

12、0,tn=0;printf(Mplease input a and nn);scanf(H%d,%d,&a,&n);printf(a=%d,n=%dn,a,n);while(count=n)tn=tn+a;sn=sn+tn;a=a*10;+count;)printf(na+aa+.=%ldn,sn);【程 序19题目:个数如果恰好等于它的因子之和,这个数就称为“完数”。例 如6=1+2+3.编程找出1000以内的所有完数。1.程序分析:请参照程序v-上页程序14.2.程序源代码:main()static int k10;int i,j,n,s;for(j=2;j1000;j+)(n=-1;S=

13、j;for(i=1;ij;i+)if(j%i)=O)n+;s=s-i;kn=i;)if(s=O)printf(M%d is a wanshu,j);for(i=0;in;i+)printf(%d,ki);printf(M%dnkn);)【程序20题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第 1 0 次落地时,共经过多少米?第 1 0 次反弹多高?1.程序分析:见下面注释2.程序源代码:main()(float sn=1 00.0,hn=sn/2;int n;for(n=2;n 0)x1=(x2+1)*2;/*第一天的桃子数是第2 天桃子数加1 后的2 倍*/

14、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.程序源代码:main()char i,j,k;/*i 是 a 的对手,j 是 b 的对手,k 是c 的对手*/for(i=x;i=z;i+)for(j=x,;j=z;j+)(if(i!=j)

15、for(k=x;k=z;k+)if(i!=k&j!=k)if(i!=x&k!=x&k!=z)printf(order is a-%ctb-%ctc-%cn,i,j,k);)【程序23题口:打印出如下图案(菱形)1.程序分析:先把图形分成两部分来看待,前四行个规律,后 三 行 个规律,利用双重fo r循环,第一层控制行,第二层控制列。2.程序源代码:main()(int i,j,k;for(i=0;i=3;i+)(for(j=0;j=2-i;j+)p rin tff);for(k=0;k=2*i;k+)printff*);printf(n);for(i=0;i=2;i+)for(j=0;j=i;

16、j+)printf(M);for(k=0;k=4-2*i;k+)printf(*,);printf(nn);)【程序24题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13求出这个数列的前2 0 项之和。1 .程序分析:请抓住分子与分母的变化规律。2.程序源代码:main()int n,t,number=20;float a=2,b=1 ,s=0;for(n=1 ;n=number;n+)s=s+a/b;t=a;a=a+b;b=t;/*这部分是程序的关键,请读者猜猜t 的作用*/)printf(sum is%9.6fn,s);)【程序25题目:求 1 +2!+3!+.+20

17、!的和1.程序分析:此程序只是把累加变成了累乘。2.程序源代码:main()(float n,s=0,t=1;for(n=1;n=20;n+)t*=n;s+=t;)printf(1+2!+3!.+20!=%en,s);)【程序26题目:利用递归方法求5!。1.程序分析:递归公式:fn=fn_1*4!2 程序源代码:#include stdio.hmain()int i;int fact();for(i=0;i5;i+)printf(,40:%d!=%dn,i,fact(i);)int fact(j)int j;int sum;if(j=O)sum=1;elsesum=j*fact(j-1);r

18、eturn sum;)【程序27题目:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。1.程序分析:2.程序源代码:#include stdio.hmain()int i=5;void palin(int n);printf(,40:M);palin(i);printf(HnM);)void palin(n)int n;char next;if(n=1)next=getchar();printf(nnO:M);putchar(next);elsenext=getchar();palin(n-1);putchar(next);)【程序28题目:有 5 个人坐在 起,问第五个人多少岁

19、?他说比第4 个人大2 岁。问第4 个人岁数,他说比第3 个人大2 岁。问第三个人,又说比第2 人大两岁。问第2 个人,说比第一个人大两岁。最后问 第 个 人,他说是1 0 岁。请问第五个人多大?1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道第四人的岁数,依次类推,推 到 第 人(1 0 岁),再往回推。2.程序源代码:age(n)int n;int c;if(n=1)c=10;else c=age(n-1)+2;return(c);)main()printf(,%d,age(5);)【程序29题目:给一个不多于5 位的正整数,要求:一、求它是几位数,二

20、、逆序打印出各位数字。1.程序分析:学会分解出每位数,如下解释:(这里是种简单的算法,师专数002班赵鑫提供)2.程序源代码:m ain()long a,b,c,d,e,x;scanf(%ldM,&x);a=x/10000;/*分解出万位*/b=x%10000/1000;/*分解出千位*/c=x%1 000/100;/*分解出百位*/d=x%100/10;/*分解出十位*/e=x%10;/*分解出个位*/if(a!=0)printf(there are 5,%ld%Id%ld%ld%ldn,e,d,c,b,a);else if(b!=0)printf(there are 4,%ld%ld%Id

21、%ldn,e,d,c,b);else if(c!=0)printf(there are 3,%ld%Id%ldn,e,d,c);else if(d!=0)printf(there are 2,%Id%ldn,e,d);else if(e!=0)printf(there are 1 ,%ldnM,e);【程序30题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。1.程序分析:同29例2.程序源代码:main()long ge,shi,qian,wan,x;scanf(%ldH,&x);wan=x/1 0000;qian=x%10000/1000;shi=

22、x%100/10;ge=x%10;if(ge=wan&shi=qian)/*个位等于万位并且十位等于千位*/printf(this number is a huiwennH);elseprintf(this number is not a huiwennH);)【程序31题目:请输入星期几的第一个字母来判断一卜.是星期几,如果第一个字母一样,则继续判断第二个字母。1.程序分析:用情况语句比较好,如果第个字母样,则判断用情况语句或if语句判断第二个字母。2.程序源代码:#include void main()char letter;printf(nplease input the first l

23、etter of somedayn,f);while(letter=getch()!=Y)/*当所按字母为 丫时才结束*/switch(letter)case,S,:printf(Mplease input second lettern);if(letter=getch()=(a)printf(saturdayn);else if(letter=getch()=u)printf(nsundayn);else printf(data errorn);break;case F:printf(fridayn);break;case MprintfCmondayVn);break;case T:pri

24、ntf(please input second lettern);if(letter=getch()=u)printf(tuesdaynn);else if(letter=getch()=*h)printf(HthursdaynH);else printf(data errornM);break;case WprintfCwednesdayXn);break;default:printf(ndata errorn);)【程序32题目:Press any key to change color,do you want to try it.Please hurry up!1.程序分析:2.程序源代

25、码:#include void main(void)int color;for(color=0;color 8;color+)textbackground(color);/*设置文本的背景颜色*/cprintf(This is color%drn,color);cprintf(nPress any key to continuern);getch();/*输入字符看不见*/)【程序33题目:学习gotoxy()与clrscr()函数1.程序分析:2.程序源代码:#include void main(void)clrscr();/*清屏函数*/textbackground(2);gotoxy(1

26、,5);/*定位函数*/cprintf(MOutput at row 5 column 1nM);textbackground(3);gotoxy(20,10);cprintf(HOutput at row 10 column 20nM);【程序34题目:练习函数调用1.程序分析:2.程序源代码:#include void hello_world(void)(printf(Hello,world!nH);)void three_hellos(void)(int counter;for(counter=1;counter=3;counter+)hello_world();/*调用此函数*/)vo

27、id main(void)(three_hellos();/*调用此函数*/【程序35题目:文本颜色设置1.程序分析:2.程序源代码:#include void main(void)(int color;for(color=1;color 16;color+)textcolor(color);/*设置文本颜色*/cprintf(This is color%drnM,color);)textcolor(128+15);cprintf(This is blinkingrn);【程序36题目:求100之内的素数1.程序分析:2.程序源代码:#include#include math.h#define

28、 N 101main()int i,j,line,aN;for(i=2;iN;i+)ai=i;for(i=2;isqrt(N);i+)for(j=i+1 ;j N;j+)(if(ai!=O&aj!=O)if(aj%ai=O)aj=O;printf(HnH);for(i=2,line=0;i N;i+)if(ai!=O)printf(%5d,ai);line+;if(line=10)printf(HnM);line=0;)【程序37题目:对 10个数进行排序1.程序分析:可以利用选择法,即从后9 个比较过程中,选择个最小的与第个元素交换,下次类推,即用第二个元素与后8 个进行比较,并进行交换。2

29、.程序源代码:#define N 10main()int i,j,min,tem,aN;/*input data*/printf(please input ten num:n);for(i=0;iN;i+)printf(na%d=M,i);scanf(%d,&ai);printf(MnH);for(i=0;iN;i+)printf(M%5d,ai);printf(nH);/*sort ten num*/for(i=0;i N-1;i+)min=i;for(j=i+1;jaj)min=j;tem=a i;ai=am in;amin=tem;)/*output data*/printf(HAfte

30、r sorted n);for(i=0;iN;i+)printf(n%5d,ai);【程序38题目:求 个3*3 矩阵对角线元素之和1.程序分析:利用双重fo r循环控制输入二维数组,再 将 累 加 后 输 出。2 程序源代码:main()float a33,sum=0;int i,j;printf(please input rectangle element:n);for(i=0;i3;i+)for(j=0;j3;j+)scanf(%f,&aij);for(i=0;i3;i+)sum=sum+aii;printf(duijiaoxian he is%6.2f,sum);)【程序39题IR:有

31、一个已经排好序的数组。现输入一个数,耍求按原来的规律将它插入数组中。1.程序分析:首先判断此数是否大于最后个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。2.程序源代码:main()int a11=1,4,6,9,13,16,19,28,40,100;int tempi,temp2,number,end,i,j;printf(original array is:n);for(i=0;iend)a10=number;else for(i=0;inumber)tempi=a i;ai=number;for(j=i+1;j11;j+)tem p2=aj;aj=temp1;

32、tempi=temp2;break;)for(i=0;i11;i+)printf(,%6d,ai);【程序40题IR:将一个数组逆序输出。1 .程序分析:用第一个与最后一个交换。2.程序源代码:#define N 5main()int aN=9,6,5,4,1 J.ternp;printf(Mn original array:n);for(i=0;iN;i+)printf(N%4d,ai);for(i=0;iN/2;i+)temp=a i;ai=aN-i-1;aN-i-1=temp;)printf(Hn sorted array:n);for(i=0;i N;i+)printf(%4d,ai)

33、;)【程序41题目:学习static定义静态变量的用法1.程序分析:2.程序源代码:#include stdio.h,varfunc()int var=0;static int static_var=0;printf(40:var equal%d n,var);printf(H40:static var equal%d nH,static_var);printf(Hnn);var+;static_var+;)void main()int i;for(i=0;i3;i+)varfunc();【程序42题目:学习使用aut。定义变量的用法1.程序分析:2.程序源代码:#include stdio.

34、hmain()int i,num;num=2;for(i=0;i3;i+)printf(H40:The num equal%d nn,num);num+;auto int num=1;printf(n40:The internal block num equal%d n,num);num+;)【程序43题目:学习使用static的另一用法。1.程序分析:2.程序源代码:#include stdio.hmain()int i,num;num=2;for(i=0;i3;i+)printf(M40:The num equal%d n,num);num+;static int num=1;printf

35、(40:The internal block num equal%dn,num);num+;)【程序44题目:学习使用external的用法。1.程序分析:2.程序源代码:#include stdio.hint a,b,c;void add()int a;a=3;c=a+b;)void main()a=b=4;add();printf(The value of c is equal to%dnn,c);【程序45题目:学习使用register定义变量的方法。1.程序分析:2.程序源代码:void main()register int i;int tmp=0;for(i=1 ;i);scanf(

36、%cT,&num);printf(n40:The square for this number is%d n,SQ(num);if(num=50)again=TRUE;elseagain=FALSE;)【程序47题目:宏#define命令练习(2)1.程序分析:2.程序源代码:#include stdio.h1#define exchange(a,b)/*宏定义中允许包含两道衣裳命令的情形,此时必须在最右边加上int t;t=a;a=b;b=t;)void main(void)(int x=10;int y=20;printf(Hx=%d;y=%dn,x,y);exchange(x,y);pr

37、intf(nx=%d;y=%dnn,x,y);【程序48题目:宏#define命令练习(3)1.程序分析:2.程序源代码:#define LAG#define SMA y)?x:y#define MINIMUM(x,y)(xy)?y:xvoid main()int a=10,b=20;#ifdef MAXprintf(4O:The larger one is%dn,MAXIMUM(a,b);#elseprintf(40:The lower one is%dn,MINIMUM(a,b);#endif#ifndef MINprintf(H40:The lower one is%dn,MINIMUM

38、(a,b);#elseprintf(4O:The larger one is%dn,MAXIMUM(a,b);#endif#undef MAX#ifdef MAXprintf(4O:The larger one is%dn,MAXIMUM(a,b);#elseprintf(40:The lower one is%dn,MINIMUM(a,b);#endif#define Ml N#ifndef MINprintf(40:The lower one is%dn,MINIMUM(a,b);#elseprintf(40:The larger one is%dn,MAXIMUM(a,b);#endif

39、【程序50题目:#include的应用练习1.程序分析:2 程序源代码:test.h文件如下:#define LAG#define SMA 4;c=(Ov v 4);d=b&c;printf(%on%on,a,d);【程序55题目:学习使用按位取反1.程序分析:-0=1;-1=0;2.程序源代码:#include stdio.h1main()int a,b;a=234;b=a;printf(40:The as 1 complement(decimal)is%d n,b);a=a;printf(n40:The as 1 complement(hexidecimal)is%x nsa);【程序56

40、题目:画图,学用circle画圆形。1.程序分析:2.程序源代码:/*circle*/#include graphics.hMmain()int driver,mode,i;float j=1 ,k=1;driver=VGA;mode=VGAHI;initgraph(&driver,&mode,M M);set bkcolor(YELLOW);for(i=0;i=25;i+)(setcolor(8);circle(310,250,k);k=k+j;j=j+0.3;【程序57题目:画图,学用line画直线。1.程序分析:2.程序源代码:#include graphics.hnmain()int

41、driver,modej;float x0,y0,y1,x1;float j=12,k;driver=VGA;mode=VGAHI;initgraph(&driver,&mode,H);setbkcolor(GREEN);x0=263;y0=263;y1=275;x1=275;for(i=0;i=1 8;i+)setcolor(5);line(x0,y0,x0,y1);x0=x0-5;y0=y0-5;x1=x1+5;y1=y1+5;j=j+1O;)x0=263;y1=275;y0=263;for(i=0;i=20;i+)setcolor(5);line(x0,y0,x0,y1);x0=x0+5

42、;yO=yO+5;yi=yi-5;【程序58题目:画图,学用rectangle画方形。1 .程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。2.程序源代码:#include graphics.hMmain()int x0,y0,y1,x1,driver,mode,i;driver=VGA;mode=VGAHI;initgraph(&driver,&mode,M);setbkcolor(YELLOW);x0=263;y0=263;y1=275;x1=275;for(i=0;i=18;i+)setcolor(1);rectangle(xO,yO,x1,y1);x0=x

43、0-5;y0=y0-5;x1=x1+5;y1=y1+5;)settextstyle(DEFAULT_F0NT,H0RIZ_DIR,2);outtextxy(1 50,40,How beautiful it is!);line(130,60,480,60);setcolor(2);circle(269,269,137);【程序59题目:画图,综合例子。1.程序分析:2.程序源代码:#define PAI 3.1415926#define B 0.809#include graphics.hH#include math.hmain()(int i,j,k,x0,y0,x,y,driver,mode

44、;float a;driver=CGA;mode=CG ACO;initgraph(&driver,&mode,“);setcolor(3);setbkcolor(GREEN);x0=150;y0=100;circle(x0,y0,10);circle(x0,y0,20);circle(x0,y0,50);for(i=0;i16;i+)a=(2*PAI/16)*i;x=ceil(x0+48*cos(a);y=ceil(y0+48*sin(a)*B);setcolor(2);line(x0,y0,x,y);setcolor(3);circle(x0,y0,60);/*Make 0 time no

45、rmal size letters*/settextstyle(DEFAULT_FONT,HORIZ_DIR,0);outtextxy(10,170,press a key);getch();setfillstyle(HATCH_FI LL,YELLOW);floodfill(202,100,WHITE);getch();for(k=0;k=500;k+)setcolor(3);for(i=0;i=16;i+)a=(2*PAI/16)*i+(2*PAI/180)*k;x=ceil(x0+48*cos(a);y=ceil(yO+48+sin(a)*B);setcolor(2);line(xO,y

46、O,x,y);)for(j=1 ;j=50;j+)a=(2*PAI/16)*i+(2*PAI/180)*k-1;x=ceil(x0+48*cos(a);y=ceil(y0+48*sin(a)*B);line(xO,yO,x,y);)restorecrtmode();)【程序60题目:画图,综合例子。1.程序分析:2.程序源代码:#include graphics.hn#define LEFT 0#define TOP 0#define RIGHT 639#define BOTTOM 479#define LINES 400#define MAXCOLOR 15main()int driver,

47、mode,error;int x1,y1;int x2,y2;int dx1,dy1,dx2,dy2,i=1;int count=0;int color=0;driver=VGA;mode=VGAHI;initgraph(&driver,&mode,H);x1=x2=y1=y2=10;dx1=dy1=2;dx2=dy2=3;while(!kbhit()(Ijne(x1,y1,x2,y2);x1+=dx1;y1+=dy1;x2+=dx2;y2+dy2;if(x1=RIGHT)dx1=-dx1;if(y1=BOTTOM)dy1=-dy1;if(x2=RIGHT)dx2=-dx2;if(y2=BOT

48、TOM)dy2=-dy2;if(+count LINES)setcolor(color);color=(color=MAXCOLOR)?0:+color;)closegraph();)【程序61题目:打印出杨辉三角形(要求打印出10行如下图)1.程序分析:11 11 2 11 3 3 11 4 6 4 11 5 10 10 5 12.程序源代码:main()int i,j;int a1010;printf(Hn);for(i=0;i 10;i+)ai0=1;aii=1;for(i=2;i 10;i+)for(j=1;ji;j+)aij=ai-1j-1+ai-1j;for(i=0;i10;i+)

49、for(j=0;j=i;j+)printf(,%5d,aij);printf(nnM);)【程序62题目:学习putpixel画点。1.程序分析:2.程序源代码:#include stdio.h#include graphics.hnmain()(int i,j,driver=VGA,mode=VGAHI;initgraph(&driver,&mode,n);set bkcolor(YELLOW);for(i=50;i=230;i+=20)for(j=50;j=230;j+)putpixel(i,j,1);for(j=50;j=230;j+=20)for(i=50;i=230;i+)putpi

50、xel(i,j,1);【程序63题目:画椭圆ellipse1.程序分析:2.程序源代码:#include stdio.h#include graphics.hn#include conio.hmain()int x=360,y=160,driver=VGA,mode=VGAHI;int num=20,i;int top,bottom;initgraph(&driver,&mode,M);top=y-30;bottom=y-30;for(i=0;inum;i+)(ellipse(250,250,0,360,top,bottom);top-=5;bottom+=5;)getch();)【程序64题

51、目:利用 ellipse and rectangle 画图。1.程序分析:2.程序源代码:#include stdio.hn#include graphics.h#include conio.h*main()(int driver=VGA,mode=VGAHI;int i,num=15,top=50;int left=20,right=50;initgraph(&driver,&mode,H);for(i=0;inum;i+)(ellipse(250,250,0,360,right,left);ellipse(250,250,0,360,20,top);rectangle(20-2*i,20-

52、2*i,10*(i+2),10*(i+2);right+=5;left+=5;top+=10;)getch();【程序65题目:一个最优美的图案。1.程序分析:2.程序源代码:#include graphics.hH#include math.h#include dos.h#include conio.h,#include stdlib.h#include stdio.h#include stdarg.h#define MAX PTS 15#define PI 3.1415926struct PTS int x,y;);double AspectRatio=0.85;void LineToDe

53、mo(void)struct viewporttype vp;struct PTS pointsMAXPTS;int i,j,h,w,xcenter,ycenter;int radius,angle,step;double rads;printf(MoveTo/LineTo Demonstration);getviewsettings(&vp);h=vp.bottom-vp.top;w=vp.right-vp.left;xcenter=w/2;/*Determine the center of circle*/ycenter=h/2;radius=(h-30)/(AspectRatio*2);

54、step=360/MAXPTS;/*Determine#of increments*/angle=0;/*Begin at zero degrees*/for(i=0;iMAXPTS;+i)/*Determine circle intercepts*/rads=(double)angle*PI/180.0;/*Convert angle to radians*/pointsi.x=xcenter+(int)(cos(rads)*radius);pointsi.y=ycenter-(int)(sin(rads)*radius*Aspect Ratio);angle+=step;/*Move to

55、 next increment*/)circle(xcenter,ycenter,radius);/*Draw bounding circle*/for(i=0;i MAXPTS;+i)/*Draw the cords to the circle*/for(j=i;jn2)swap(pointer1,pointer2);if(n1 n3)swap(pointer1,pointers);if(n2n3)swap(pointer2,pointers);printf(Mthe sorted numbers are:%d,%d,%dn,n1,n2,n3);)swap(p1,p2)int*p1,*p2;

56、int p;p=*p1;*p1=*p2;*p2=p;【程序67题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。1.程序分析:谭浩强的书中答案有问题。2.程序源代码:main()int number10;input(number);max_min(number);output(number);)input(number)int number10;int i;for(i=0;i9;i+)scanf(%d,n,&numberi);scanf(%d,&number9);max_min(array)int array10;int*max,*min,k,l;int*p,*arr

57、_end;arr_end=array+10;max=min=array;for(p=array+1 ;p*max)max=p;else if(*p*min)min=p;k=*max;l=*min;*p=array0;array0=l;l=*p;*p=array9;array9=k;k=*p;return;)output(array)int array 10;int*p;for(p=array;p array+9;p+)printf(,%d,*p);printf(M%dn,array9);)【程序68题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数1.程序分析:2.

58、程序源代码:main()int number20printf(the total numbers is:);scanf(%dn,&n);printf(nback m:H);scanf(%dn,&m);for(i=0;i n-1;i+)scanf(%d,&numbnri);scanf(%d”,&numbern-1);move(number,n,m);for(i=0;i array;p-)*P=*(P-1);*array=array_end;m-;if(m0)move(array,n,m);【程序69题目:有n个人围成圈,顺序排号。从第 个人开始报数(从1到3报数),圈子,问最后留下的是原来第几号

59、的那位。1 .程序分析:2.程序源代码:#define nmax 50main()int i,k,m,n,numnmax,*p;printf(please input the total of numbers:);scanf(%d,&n);p=num;for(i=0;in;i+)*(P+i)=i+1;i=0;k=0;m=0;while(m n-1)if(*(p+i)!=0)k+;if(k=3)*(P+i)=0;k=0;m+;)i+;if(i=n)i=0;)while(*p=0)p+;printf(u%d is leftn,*p);)凡报到3的人退出【程序70题目:写一个函数,求一个字符串的长度

60、,在main函数中输入字符串,并输出其长度。1.程序分析:2.程序源代码:main()(int len;char*str20;printf(nplease input a string:n);scanf(%su,str);len=length(str);printf(nthe string has%d characters.Jen);)length(p)char*p;int n;n=0;while(*p!=O,)n+;P+;)return n;)题目:编写input。和output。函数输入,输 出5个学生的数据记录。1.程序分析:2.程序源代码:#define N 5struct stude

61、nt char num6;char name8;int score4;stuN;input(stu)struct student stu;int i.j;for(i=0;i N;i+)printf(n please input%d of%dn,i+1 ,N);printf(num:);scanf(%sH,stui.num);printf(name:M);scan f(%sM,st u i.n am e);for(j=0;j3;j+)printf(score%d.,j+1);scanf(n%d,&stui.scorej);)printf(n);)print(stu)struct student

62、stu;int i,j;printf(nnNo.Name Sco1 Sco2 Sco3nH);for(i=0;iN;i+)printf(H%-6s%-1 OsH,stui.num,stui.name);for(j=0;jn);for(i=0;i data=num;ptr-next=(link)malloc(sizeof(node);if(i=4)ptr-next=NULL;else ptr=ptr-next;)ptr=head;while(ptr!=NULL)printf(The value is=%dnH,ptr-data);ptr=ptr-next;)【程序73题IR:反向输出一个链表。1

63、.程序分析:2.程序源代码:/*reverse output a list*/#include stdlib.h#include stdio.hstruct list int data;struct list*next;);typedef struct list node;typedef node*link;void main()link ptr,head,tail;int num,i;tail=(link)malloc(sizeof(node);tail-next=NULL;ptr=tail;printf(nplease input 5 data=nM);for(i=0;idata=num;

64、head=(link)malloc(sizeof(node);head-next=ptr;ptr=head;)ptr=ptr-next;while(ptr!=NULL)printf(The value is=%dn,ptr-data);ptr=ptr-next;)【程序74题目:连接两个链表。1.程序分析:2.程序源代码:#include stdlib.h#include stdio.hstruct list int data;struct list*next;);typedef struct list node;typedef node*link;link delete_node(link

65、pointerjink tmp)if(tmp=NULL)/*delete first node*/return pointer-next;else if(tmp-next-next=NULL)/*delete last node*/tmp-next=NULL;else/*delete the other node*/tmp-next=tmp-next-next;return pointer;)void selection_sort(link pointer,int num)link tmp,btmp;int i,min;for(i=0;idata;btmp=NULL;while(tmp-nex

66、t)if(min tmp-next-data)min=tmp-next-data;btmp=tmp;)tmp=tmp-next;)printf(40:%dn,min);pointer=delet e_node(pointer,btmp);)link create_list(int array,int num)link tmp1,tmp2,pointer;int i;pointer=(link)malloc(sizeof(node);pointer-data=array0;tmp1=pointer;for(i=1 ;i next=NULL;tmp2-data=arrayi;tmp1-next=tmp2;tmp1=tmp1-next;)return pointer;)link concatenate(link pointeri Jink pointer2)link tmp;tmp=pointeri;while(tmp-next)tmp=tmp-next;tmp-next=pointer2;return pointeri;)void main(void)int ar门口=3,12,8,9,1

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!