C语言基本算法程序(共109页).doc

上传人:飞****2 文档编号:15161549 上传时间:2022-05-11 格式:DOC 页数:109 大小:427.50KB
返回 下载 相关 举报
C语言基本算法程序(共109页).doc_第1页
第1页 / 共109页
C语言基本算法程序(共109页).doc_第2页
第2页 / 共109页
点击查看更多>>
资源描述

《C语言基本算法程序(共109页).doc》由会员分享,可在线阅读,更多相关《C语言基本算法程序(共109页).doc(109页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、精选优质文档-倾情为你奉上(_)母鸡 荣誉作品以下一百例C语言基本程序算法出自C语言基本算法程序一百例.pdf的word形式,并经过本人亲自在vc6.0 上编写运行完美!第一章 最简单的问题与算法1.绘制余弦曲线源程序:#include#includevoid main()double y;int x,m;for(y=1;y-1;y-=0.1)/y为列方向m=acos(y)*10;/y对应的弧度mfor(x=1;xm;x+) printf( );printf(*);for(;x62-m;x+) printf( );printf(*n);运行结果:* * * * * * * * * * * *

2、* * * * * * * * * * * * * * * * * * * * * * * * * * * * *Press any key to continue2.绘制余弦曲线和直线源代码:#include#includevoid main()double y;int x,m,n,yy;for(yy=0;yy=20;yy+)y=0.1*yy;/y:屏幕行方向坐标m=acos(1-y)*10;/m:cos(x)曲线上y点对应的屏幕列坐标n=45*(y-1)+31;for(x=0;x=62;x+)if(x=m&x=n)printf(+);else if(x=n)printf(+);else i

3、f(x=m|x=62-m) printf(*);elseprintf( );printf(n);运行结果:* * * * * *+ * * + * * + * * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * * * *3.绘制圆源代码:#include#includevoid main()double y;int x,m;for(y=10;y=-10;y-)/圆的半径为10m=2.5*sqrt(100-y*y);/行y对应的列坐标m。2.5是屏幕纵横比调节系数,/因为屏幕的行距大于列距,不进

4、行调节的话就显示为椭圆for (x=1;x30-m;x+)printf( );printf(*);for(;x30+m;x+)printf( );printf(*n);运行结果: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *Press any key to continue4.歌星大奖赛源代码:#include#includevoid main()int integer,i,max,min,sum;max=-32768;min=32767;/-32768+32767为C语言的整型数

5、的最值sum=0;for(i=1;imax) max=integer;if(integermin) min=integer;printf(Canceled max score:%dn Cancele min score:%dn,max,min);printf(Average score:%dn,(sum-max-min)/8);运行结果:input number 1=90input number 2=91input number 3=93input number 4=90input number 5=94input number 6=97input number 7=99input numbe

6、r 8=95input number 9=92input number 10=97Canceled max score:99 Cancele min score:90Average score:93Press any key to continue5.求最大约数源代码:#include#includevoid main()long i;int j;printf(plese input number:);scanf(%ld,&i);for(j=999;j=100;j-)if(i%j=0)printf(The max factor with 3 digits in %ld is:%d.n,i,j)

7、;break;运行结果:plese input number:The max factor with 3 digits in is:777.Press any key to continue6.高次方数的尾数源代码:#include#includevoid main()int i,x,y,last=1;printf(input X and Y(X*Y):);scanf(%d*%d,&x,&y);for(i=1;i=y;i+)last=last*x%1000;printf(The last 3 digits of %d*%d is:%dn,x,y,last%1000);运行结果:input X

8、and Y(X*Y):13*13The last 3 digits of 13*13 is:253Press any key to continue7.阶乘尾数零的个数源代码:#include#includevoid main()int a,count=0;for(a=5;a=100;a+=5)count+;if(!(a%25) count+;printf(The number of 0 in the end of 100! is:%d.n,count);运行结果:The number of 0 in the end of 100! is:24.Press any key to continu

9、e8.借书方案知多少源代码:#include#includevoid main()int a,b,c,count=0;printf(There are different methods for XM to distribute books to 3 readers:n);for(a=1;a=5;a+)for(b=1;b=5;b+)for(c=1;a!=b&c=5;c+)if(c!=a&c!=b)printf(+count%5?%6d:%d,%d,%d:%6d:%d,%d,%dn,count,a,b,c);printf(n);运行结果:There are different methods f

10、or XM to distribute books to 3 readers: 1:1,2,3 2:1,2,4 3:1,2,5 4:1,3,2 5:1,3,4 6:1,3,5 7:1,4,2 8:1,4,3 9:1,4,5 10:1,5,2 11:1,5,3 12:1,5,4 13:2,1,3 14:2,1,4 15:2,1,5 16:2,3,1 17:2,3,4 18:2,3,5 19:2,4,1 20:2,4,3 21:2,4,5 22:2,5,1 23:2,5,3 24:2,5,4 25:3,1,2 26:3,1,4 27:3,1,5 28:3,2,1 29:3,2,4 30:3,2,5

11、 31:3,4,1 32:3,4,2 33:3,4,5 34:3,5,1 35:3,5,2 36:3,5,4 37:4,1,2 38:4,1,3 39:4,1,5 40:4,2,1 41:4,2,3 42:4,2,5 43:4,3,1 44:4,3,2 45:4,3,5 46:4,5,1 47:4,5,2 48:4,5,3 49:5,1,2 50:5,1,3 51:5,1,4 52:5,2,1 53:5,2,3 54:5,2,4 55:5,3,1 56:5,3,2 57:5,3,4 58:5,4,1 59:5,4,2 60:5,4,3Press any key to continue9.杨辉三

12、角形源代码:#include#includeint e(int x,int y);void main()int i,j,n=30;printf(N=);while(n27)scanf(%d,&n);for(i=0;i=n;i+)for(j=0;jn-i;j+) printf( );for(j=1;ji+2;j+) printf(%6d,e(i,j);printf(n);int e(int x,int y)int z;if(y=1)|(y=x+1) return(1);z=e(x-1,y-1)+e(x-1,y);return(z);运行结果:N=12 1 1 1 1 2 1 1 3 3 1 1

13、4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 1 10 45 120 210 252 210 120 45 10 1 1 11 55 165 330 462 462 330 165 55 11 1 1 12 66 220 495 792 924 792 495 220 66 12 1Press any key to continue10.数制转换源代码:#include#includevoid printb(int x,i

14、nt n);void main()int x;printf(input number:);scanf(%d,&x);printf(number of decimal form:%dn,x);printf(its binary form:);printb(x,sizeof(int)*4);/x:整数sizeof(int):int型在内存中所占的字节数,/sizeof(int)*4:int型对应的数printf(n);void printb(int x,int n)if(n0)putchar(0+(unsigned)(x&(1(n-1);/输出第n位printb(x,n-1);/递归调用,输出x的

15、后n-1位运行结果:input number:128number of decimal form:128 its binary form:00000Press any key to continue第二章 生活中的数学问题11. 打渔还是晒网源代码:#include#includestruct dateint year;int month;int day;int days(struct date day);void main()struct date today,term;int yearday,year,day;printf(enter year/month/day:);scanf(%d/%

16、d/%d,&today.year,&today.month,&today.day);term.day=31;term.month=12;for(yearday=0,year=1990;year0&day4)printf(He was fishing at that day.n);else printf(He was sleeping at that day.n);int days(struct date day)static int day_tab213=0,31,28,31,30,31,30,31,31,30,31,30,31,0,31,29,31,30,31,30,31,31,30,31,

17、30,31,;/二维数组形式的天数表作为参数int i,lp;lp=day.year%4=0&day.year%100!=0|day.year%400=0;for(i=1;iday.month;i+)day.day+=day_tablpi;return(day.day);运行结果:enter year/month/day:1993/10/25He was sleeping at that day.Press any key to continue12. 抓交通肇事犯源代码:#include#includevoid main()int i,j,k,c;for(i=1;i=9;i+)for(j=0

18、;j=9;j+)if(i!=j)k=i*1000+i*100+j*10+j;for(c=31;c*ck;c+);if(c*c=k)printf(Lorry-NO. is %d.n,k);运行结果:Lorry-NO. is 7744.Press any key to continue13. 该该存多少钱源代码:#include#includevoid main()int i;float total=0;for(i=0;i5;i+)total=(total+1000.0)/(1+0.0063*12);printf(He must save %.2f at first.n,total);运行结果:H

19、e must save 4039.44 at first.Press any key to continue14. 怎样存钱利最大源代码:#include#includevoid main()int i8,i5,i3,i2,i1,n8,n5,n3,n2,n1;float max=0,term;for(i8=0;i83;i8+)for(i5=0;i5=(20-i8*8)/5;i5+)for(i3=0;i3=(20-8*i8-5*i5)/3;i3+)for(i2=0;i2=(20-8*i8-5*i5-3*i3)/2;i2+)i1=20-8*i8-5*i5-3*i3-2*i2;term=2000.0

20、*pow(double)(1+0.0063*12),(double)i1)*pow(double)(1+2*0.0066*12),(double)i2)*pow(double)(1+3*0.0069*12),(double)i3)*pow(double)(1+5*0.0075*12),(double)i5)*pow(double)(1+8*0.0084*12),(double)i8);if(maxterm)max=term;n1=i1;n2=i2;n3=i3;n5=i5;n8=i8;printf(For max profit,he should so save his moneyin a ba

21、nk:n);printf(made fixed deposit for 8 year:%d timesn,n8);printf(made fixed deposit for 5 year:%d timesn,n5);printf(made fixed deposit for 3 year:%d timesn,n3);printf(made fixed deposit for 2 year:%d timesn,n2);printf(made fixed deposit for 1 year:%d timesn,n1);printf(total:%0.2fn,max);运行结果:For max p

22、rofit,he should so save his moneyin a bank: made fixed deposit for 8 year:0 times made fixed deposit for 5 year:4 times made fixed deposit for 3 year:0 times made fixed deposit for 2 year:0 times made fixed deposit for 1 year:0 times total:8841.01Press any key to continue15. 捕鱼和分鱼源代码:#include#includ

23、evoid main()int n,i,x,flag=1;for(n=6;flag;n+)/采用试探的方法,将n逐步放大for(x=n,i=1&flag;i=5;i+)if(x-1)%5=0) x=4*(x-1)/5;else flag=0;if(flag) break;else flag=1;printf(Total number of fish catched=%dn,n);运行结果:Total number of fish catched=3121Press any key to continue16. 出售金鱼源代码:#include#includevoid main()int i,j

24、,n=0,x;for(i=23;n=0;i+=2)/23为试探初值,n为标识变量for(j=1,x=i;j=11;j+)if(x+1)%(j+1)=0)x-=(x+1)/(j+1);else x=0;break;if(j=5&x=11)printf(There are %d fishes at first.n,i);n=1;运行结果:There are 59 fishes at first.Press any key to continue17. 平分七筐鱼源代码:#include#includeint a33,count=0;void main()int i,j,k,m,n,flag;pri

25、ntf(It exists possible distribution plans:n);for(i=0;i=3;i+)a00=i;for(j=i;j=7-i&j3) continue;if(a20a10) break;for(k=1;k=5;k+=2)a01=k;for(m=1;m7-k;m+=2)a11=m;a21=7-k-m;for(flag=1,n=0;flag&n3;n+)if(an0+an17&an0*2+an1=7)an2=7-an0-an1;else flag=0;if(flag)printf(No. %dFull basket Semi-basket Emptyn,+coun

26、t);for(n=0;n3;n+)printf(fisher %c:%d%d%dn,A+n,an0,an1,an2);运行结果:It exists possible distribution plans:No. 1 Full basket Semi-basket Empty fisher A: 1 5 1 fisher B: 3 1 3 fisher C: 3 1 3No. 2 Full basket Semi-basket Empty fisher A: 2 3 2 fisher B: 2 3 2 fisher C: 3 1 3Press any key to continue第三章 整数趣

27、题18. 有限5位数源代码:#include#includevoid main()long i;int count=0;for(i=1000;i=9999;i+)if(!(i*10+6)%3)count+;printf(count=%dn,count);运行结果:count=3000Press any key to continue19. 8除不尽的自然数源代码:#include#includevoid main()int i;for(i=0;i+)if(i*8+7)*8+1)*8+1=(2*i*17)+15)*17+4)printf(The required number is:%dn,(34*i+15)*17+4);break;运行结果:The required number is:1993Press any key to continue20. 一个奇异的三位数源代码:#include#includemat

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

当前位置:首页 > 教育专区 > 教案示例

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

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com