while循环结构例题Word文档格式.docx

上传人:b****3 文档编号:13647625 上传时间:2022-10-12 格式:DOCX 页数:18 大小:17.19KB
下载 相关 举报
while循环结构例题Word文档格式.docx_第1页
第1页 / 共18页
while循环结构例题Word文档格式.docx_第2页
第2页 / 共18页
while循环结构例题Word文档格式.docx_第3页
第3页 / 共18页
while循环结构例题Word文档格式.docx_第4页
第4页 / 共18页
while循环结构例题Word文档格式.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

while循环结构例题Word文档格式.docx

《while循环结构例题Word文档格式.docx》由会员分享,可在线阅读,更多相关《while循环结构例题Word文档格式.docx(18页珍藏版)》请在冰豆网上搜索。

while循环结构例题Word文档格式.docx

=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!

的值

{inti=1,n,p=1;

scanf(“%d〞,&

n);

=n)

{p=p*i;

printf(“%d\n〞,p);

求1!

+2!

+3!

+………+n!

{inti=1,n,p=1,s;

s=s+p;

题型3无限个数连加

eg3、求1-1/3+1/5-1/7+………的近似值,要求精度要达到10-4

math.h"

{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个数,统计其中正数、负数和零的个数。

{inti=1,n,p,z;

floatx;

p=n=z=0;

=20)

{scanf(“%f〞,&

x);

if(x>

0)

p++;

else

if(x<

n++;

else

z++;

printf(“%d\t%d\t%d\n〞,p,n,z);

统计各类字符的个数

eg4.2个位为6且能被3整除的五位数有多少?

方法1

{longi=10000,c=0;

=99999)

{

if((i%3==0)&

&

(i%10==6))

c++;

printf(“%d\n〞,c);

方法2

{longi=10006,c=0;

if(i%3==0)

i=i+10;

题型5数列

eg5输出fibo数列的第20位数字

{intf1=1,f2=1,f3,i=3;

{f3=f1+f2;

f1=f2;

f2=f3;

printf(“%d\n〞,f3);

输出fibo数列前20位数字

printf(“%d\t%d\t〞,f1,f2);

printf(“%d\t〞,f3);

题型6数据的逆序输出

eg6任意给定一个正整数,个位数字逆序输出。

{longx,t;

scanf(“%ld〞,&

while(x!

=0)

{t=x%10;

x=x/10;

printf(“%d〞,t);

}

题型7公约数与公倍数

eg7任意输入两个正整数,求其最大公约数和最小公倍数。

{intm,n,a,b,r,t;

scanf(“%d%d〞,&

m,&

if(m>

n){a=m;

b=n;

else{a=n;

b=m;

while(b!

{r=a%b;

a=b;

b=r;

printf(“zuidagongyushushi:

%d\n〞,a);

printf(“zuixiaogongbeishushi:

%d\n〞,m*n/a);

题型8素数问题

eg8从键盘上任意输入一个正整数,判断其是否为素数。

{intx,i=2;

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

{

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*y1>

0)

x1=x;

x2=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

floatx,x0,y,y1;

x=;

while(fabs(x-x0)>

{x0=x;

y=2*x0*x0*x0+4*x0*x0-7*x0-6;

y1=6*x0*x0+8*x0-7;

x=x0-y/y1;

牛顿迭代公式:

xn+1=xn-f(xn)/f’(xn)

do-while循环结构举例

do

}while(i<

=100);

for循环结构举例

f1

=100;

s=s+i;

printf(“%d\n〞,s);

f2

main()

inti,f1,f2,f3;

f1=1;

f2=1;

%d,%d"

f1,f2);

for(i=3;

=20;

f3=f1+f2;

f2=f3;

printf("

%d"

f3);

f3

inti;

floata,max;

scanf("

%f"

&

a);

max=a;

for(i=1;

=9;

{scanf("

if(max<

a)

max=a;

%f\n"

max);

f4

inti,s=1;

for(i=9;

=1;

i--)

s=2*(s+1);

%d\n"

s);

intx,n=0,s=0;

while(n<

10)

{

%d"

if(x<

0)break;

s+=x;

}

s=%d\n"

while(n<

0)continue;

inti=2,m;

scanf("

m);

while(m%i!

if(i==m)

%dshisushu!

\n"

m);

else

%dbushisushu!

inti,m;

for(i=2;

m%i!

=0;

i++);

if(i==m)

else

{inti,m;

for(i=2;

i<

=m;

i++)

if(m%i==0)break;

printf("

{inti,m,s;

s=sqrt(m);

=s;

if(i==s+1)

%dbushis

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工作范文 > 行政公文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1