您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > while循环结构例题
引子#includestdio.hmain(){inti=1;for(i=1;i=10000;i++)printf(“%d\t”,i);}题型1输入输出多个数据eg1、输出1~10000之间所有的整数#includestdio.hmain(){inti=1;while(i=1000){printf(“%d\t”,i);i++;}}拓展:1、换成所有的奇数2、换成所有的偶数题型2有限个数连加和连乘eg2.1、求1+2+3+4+………+100的值#includestdio.hmain(){inti=1,s=0;while(i=100){s=s+i;i++;}printf(“%d\n”,s);}拓展:1、求1+2+3+4+………+n的值2、求12+22+32+………+n2的值3、求1+1/2+1/3+………+1/n的值eg2.2、求n!的值#includestdio.hmain(){inti=1,n,p=1;scanf(“%d”,&n);while(i=n){p=p*i;i++;}printf(“%d\n”,p);}拓展:求1!+2!+3!+………+n!的值#includestdio.hmain(){inti=1,n,p=1,s;scanf(“%d”,&n);while(i=n){p=p*i;s=s+p;i++;}printf(“%d\n”,s);}题型3无限个数连加eg3、求1-1/3+1/5-1/7+………的近似值,要求精度要达到10-4#includestdio.h#includemath.hmain(){floatn=1,s=0,f=1,t=1;while(fabs(t)=1e-4){t=f/(2*n-1);s=s+t;f=-f;n++;}printf(“%f\n”,s);}拓展:求1-1/2+1/4-1/6+………的近似值,要求精度要达到10-4题型4统计eg4.1、输入20个数,统计其中正数、负数和零的个数。#includestdio.hmain(){inti=1,n,p,z;floatx;p=n=z=0;while(i=20){scanf(“%f”,&x);if(x0)p++;elseif(x0)n++;elsez++;i++;}printf(“%d\t%d\t%d\n”,p,n,z);}拓展:统计各类字符的个数eg4.2个位为6且能被3整除的五位数有多少?方法1#includestdio.hmain(){longi=10000,c=0;while(i=99999){if((i%3==0)&&(i%10==6))c++;i++;}printf(“%d\n”,c);}方法2#includestdio.hmain(){longi=10006,c=0;while(i=99999){if(i%3==0)c++;i=i+10;}printf(“%d\n”,c);}题型5数列eg5输出fibo数列的第20位数字#includestdio.hmain(){intf1=1,f2=1,f3,i=3;while(i=20){f3=f1+f2;f1=f2;f2=f3;i++;}printf(“%d\n”,f3);}拓展:输出fibo数列前20位数字#includestdio.hmain(){intf1=1,f2=1,f3,i=3;printf(“%d\t%d\t”,f1,f2);while(i=20){f3=f1+f2;f1=f2;f2=f3;printf(“%d\t”,f3);i++;}}题型6数据的逆序输出eg6任意给定一个正整数,个位数字逆序输出。#includestdio.hmain(){longx,t;scanf(“%ld”,&x);while(x!=0){t=x%10;x=x/10;printf(“%d”,t);}}题型7公约数与公倍数eg7任意输入两个正整数,求其最大公约数和最小公倍数。#includestdio.hmain(){intm,n,a,b,r,t;scanf(“%d%d”,&m,&n);if(mn){a=m;b=n;}else{a=n;b=m;}while(b!=0){r=a%b;a=b;b=r;}printf(“zuidagongyushushi:%d\n”,a);printf(“zuixiaogongbeishushi:%d\n”,m*n/a);}题型8素数问题eg8从键盘上任意输入一个正整数,判断其是否为素数。#includestdio.hmain(){intx,i=2;scanf(“%d”,&x);while(x%i!=0)i++;if(x==i)printf(“shi!”);elseprintf(“fou!”);}题型9高次方程的根eg9.1用二分迭代法求解方程y=2x3-4x2+3x-6=0在(-10,10)之间的根,要求精度10-5#includestdio.h#includemath.hmain(){floatx1=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;elsex2=x;x=(x1+x2)/2;y=2*x*x*x-4*x*x+3*x-6;}printf(therootis%f\n,x);}eg9.2用牛顿迭代法求解方程2x3+4x2-7x-6=0在x=1.5附近的根,要求精度10-5#includestdio.h#includemath.hmain(){floatx,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(therootis%f\n,x);}牛顿迭代公式:xn+1=xn-f(xn)/f’(xn)do-while循环结构举例#includestdio.hmain(){inti=1,s=0;do{s=s+i;i++;}while(i=100);printf(“%d\n”,s);}for循环结构举例f1#includestdio.hmain(){inti=1,s=0;for(i=1;i=100;i++)s=s+i;printf(“%d\n”,s);}f2#includestdio.hmain(){inti,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#includestdio.hmain(){inti;floata,max;scanf(%f,&a);max=a;for(i=1;i=9;i++){scanf(%f,&a);if(maxa)max=a;}printf(%f\n,max);}f4#includestdio.hmain(){inti,s=1;for(i=9;i=1;i--)s=2*(s+1);printf(%d\n,s);}#includestdio.hmain(){intx,n=0,s=0;while(n10){scanf(%d,&x);if(x0)break;s+=x;n++;}printf(s=%d\n,s);}#includestdio.hmain(){intx,n=0,s=0;while(n10){scanf(%d,&x);if(x0)continue;s+=x;n++;}printf(s=%d\n,s);}#includestdio.hmain(){intx,n=0,s=0;while(n10){scanf(%d,&x);n++;if(x0)continue;s+=x;}printf(s=%d\n,s);}#includestdio.hmain(){inti=2,m;scanf(%d,&m);while(m%i!=0)i++;if(i==m)printf(%dshisushu!\n,m);elseprintf(%dbushisushu!\n,m);}#includestdio.hmain(){inti,m;scanf(%d,&m);for(i=2;m%i!=0;i++);if(i==m)printf(%dshisushu!\n,m);elseprintf(%dbushisushu!\n,m);}#includestdio.hmain(){inti,m;scanf(%d,&m);for(i=2;i=m;i++)if(m%i==0)break;if(i==m)printf(%dshisushu!\n,m);elseprintf(%dbushisushu!\n,m);}#includestdio.h#includemath.hmain(){inti,m,s;scanf(%d,&m);s=sqrt(m);for(i=2;i=s;i++)if(m%i==0)break;if(i==s+1)printf(%dshisushu!\n,m);elseprintf(%dbushisushu!\n,m);}#includestdio.h#includemath.hmain(){inti,j;for(i=100;i=200;i++){for(j=2;j=i;j++)if(i%j==0)break;if(j==i)printf(%-10d,i);}}#includestdio.h#includemath.hmain(){inti,j,s;for(i=100;i=200;i++){s=sqrt(i);for(j=2;j=s;j++)if(i%j==0)break;if(j==s+1)printf(%-10d,i);}}#includeinclude.hmain(){inti,j,s;for(i=2;i=10000;i++){s=0;for(j=1;ji;j++)if(i%j==0)s+=j;if(i==s)printf(%6d\n,s);}}#includestdio.hmain(){inti,j,k;for(i=0;i=35;i++)for(j=0;j=35;j++)if((i+j==35)&&(2*i+4*j==94))printf(\ni=%-10dj=%-10d,i,j);}#includestdio.hmain(){inti,j,k;for(i=0;i=19;i++)for(j=0;j=33;j++)for(k=0;k=100;k++)if((i+j+k==100)&&(5*i+3*j+k/3==300))printf(\ni=%-10dj=%-10dk=%-10d,i,j,k);}#includestdio.hmain(){inti,j,k;for(i=0;i=19;i++)for(j=0;j=33;j++){k=100-i-j;if(15*i+9*j+k==300)printf(\ni=%-10dj=%-10dk=%-10d,i,j,k);}}#includestdio.hmain(){inti,j,k;for(i=0;i=19;i++)for(j=0;j=33;j++){k=100-i-j;if(5*i+3*j+k/3==100)printf(\ni=%-10dj=%-10dk=%-10d,i,j,k);}}#includestdio.hmain(){intm,n,k;for(m=1;m=9;m++){for(n=1;n=m;n++)printf(%d*%d=%-5d,n,m,n*m);printf(\n);}}#includestdio.hmain(){inti;for(i=1;i=5;i++)printf(********\n);}#includestdio.hmain(){
本文标题:while循环结构例题
链接地址:https://www.777doc.com/doc-5129792 .html