while循环结构例题

上传人:101****457 文档编号:88684870 上传时间:2019-05-06 格式:DOC 页数:36 大小:111KB
返回 下载 相关 举报
while循环结构例题_第1页
第1页 / 共36页
while循环结构例题_第2页
第2页 / 共36页
while循环结构例题_第3页
第3页 / 共36页
while循环结构例题_第4页
第4页 / 共36页
while循环结构例题_第5页
第5页 / 共36页
点击查看更多>>
资源描述

《while循环结构例题》由会员分享,可在线阅读,更多相关《while循环结构例题(36页珍藏版)》请在金锄头文库上搜索。

1、引子#include stdio.hmain() int i=1; for(i=1;i=10000;i+) printf(“%dt”,i);题型1 输入输出多个数据eg1、输出110000之间所有的整数#include stdio.hmain() int i=1; while(i=1000) printf(“%dt”,i); i+;拓展:1、换成所有的奇数 2、换成所有的偶数题型2 有限个数连加和连乘eg2.1、求1+2+3+4+100的值#include stdio.hmain() int i=1,s=0; while(i=100) s=s+i;i+;printf(“%dn”,s);拓展:1

2、、求1+2+3+4+n的值2、求12+22+32+n2的值 3、求1+1/2+1/3+1/n的值eg2.2、求n!的值#include stdio.hmain() int i=1,n,p=1; scanf(“%d”,&n); while(i=n) p=p*i;i+;printf(“%dn”,p);拓展:求1!+2!+3!+n!的值#include stdio.hmain() int i=1,n,p=1,s; scanf(“%d”,&n); while(i=1e-4) t=f/(2*n-1); s=s+t;f=-f;n+;printf(“%fn”,s);拓展:求1-1/2+1/4-1/6+的近似

3、值,要求精度要达到10-4题型4 统计eg4.1、输入20个数,统计其中正数、负数和零的个数。#include stdio.hmain() int i=1,n,p,z; float x; p=n=z=0; while(i0)p+; else if(x0) n+;else z+; i+;printf(“%dt%dt %dn”,p,n,z);拓展:统计各类字符的个数eg4.2 个位为6且能被3整除的五位数有多少?方法1#include stdio.hmain() long i=10000,c=0; while(i=99999) if(i%3=0)& (i%10=6)c+;i+;printf(“%d

4、 n”,c);方法2#include stdio.hmain() long i=10006,c=0; while(i=99999) if(i%3=0)c+;i=i+10;printf(“%d n”,c);题型5 数列eg5 输出fibo数列的第20位数字#include stdio.hmain() int f1=1,f2=1, f3,i=3; while(i=20) f3=f1+f2;f1=f2;f2=f3;i+;printf(“%d n”,f3);拓展:输出fibo数列前20位数字#include stdio.hmain() int f1=1,f2=1, f3,i=3;printf(“%d

5、t%d t”,f1,f2); while(in) a=m;b=n; else a=n;b=m; while(b!=0) r=a%b;a=b;b=r;printf(“zuida gongyushu shi:%dn”,a);printf(“zuixiao gongbeishu shi:%dn”,m*n/a);题型8 素数问题eg8 从键盘上任意输入一个正整数,判断其是否为素数。#include stdio.hmain() int x,i=2; scanf(“%d”,&x); while(x%i!=0) i+; if(x=i) printf(“shi!”);else printf(“fou!”);题

6、型9 高次方程的根eg9.1 用二分迭代法求解方程y=2x3-4x2+3x-6=0在(-10,10)之间的根,要求精度10-5#include stdio.h#include math.hmain()float x1=10,x2=-10,x, y ,y1;x=(x1+x2)/2;y=2*x*x*x-4*x*x+3*x-6;while(fabs(y)1e-5)y1=2*x1*x1*x1-4*x1*x1+3*x1-6; if(y*y10) x1=x; else x2=x; x=(x1+x2)/2; y=2*x*x*x-4*x*x+3*x-6; printf(the root is %fn,x);e

7、g9.2 用牛顿迭代法求解方程2x3+ 4x2-7x-6=0在x=1.5附近的根,要求精度10-5#include stdio.h#include math.hmain()float x,x0, y ,y1;x=1.5;while(fabs(x-x0)1e-5) x0=x;y=2*x0*x0*x0+4*x0*x0-7*x0-6;y1=6*x0*x0+8*x0-7;x=x0-y/y1;printf(the root is %fn,x);牛顿迭代公式:xn+1=xn-f(xn)/f(xn)do-while循环结构举例#include stdio.hmain() int i=1,s=0; do s=

8、s+i;i+; while(i=100);printf(“%dn”,s);for循环结构举例f1#include stdio.hmain() int i=1,s=0; for(i=1;i=100;i+) s=s+i; printf(“%dn”,s);f2#include stdio.hmain( )int i,f1,f2,f3;f1=1;f2=1;printf(%d,%d,f1,f2);for(i=3;i=20;i+) f3=f1+f2; f1=f2; f2=f3; printf(,%d,f3);f3#include stdio.hmain( )int i;float a,max;scanf(

9、%f ,&a);max=a;for(i=1;i=9;i+) scanf(%f ,&a); if(maxa) max=a;printf(%fn,max);f4#include stdio.hmain( )int i,s=1;for(i=9;i=1;i-)s=2*(s+1);printf(%dn,s);#include stdio.hmain()int x,n=0,s=0; while (n10) scanf(%d,&x); if (x0) break; s+=x; n+; printf(s=%dn,s); #include stdio.hmain( )int x,n=0,s=0;while (n

10、10) scanf(%d,&x); if (x0) continue; s+=x; n+; printf(s=%dn,s);#include stdio.hmain( )int x,n=0,s=0;while (n10) scanf(%d,&x);n+; if (x0) continue; s+=x; printf(s=%dn,s);#include stdio.hmain()int i=2,m; scanf(%d,&m);while(m%i!=0)i+;if(i=m) printf(%d shi sushu!n,m); else printf(%d bu shi sushu!n,m);#include stdio.hmain()int i,m;scanf(%d,&m);for(i=2;m%i!=0;i+) ;if(i=m) printf(%d shi sushu!n,m);else printf(%d bu shi sushu!n,m);#include stdio.hmain( ) int i,m; scanf(%d,&m); for (i=2; i=m; i+) if (m%i=0) break; if(i=m) printf(%d shi s

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

当前位置:首页 > 中学教育 > 其它中学文档

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