五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

C語言練習

2022-10-16 22:41 作者:沐笙--ms  | 我要投稿


[例5.10] 輸入5個成績數(shù)據(jù)求平均成績。對非法成績(不在[0,100]之間)數(shù)據(jù)要求重新輸入。?


main()

{

float x,s=0;

int i=1;

while(i<=5)

{ printf("輸入第%d個成績:",i);

scanf("%f",&x);

if(x<0 || x>100)continue;

s+=x;

i++;

}

printf("平均成績?yōu)?%5.1f\n",s/5);

}

[例5.11]? 用雙重循環(huán)改寫例5.3求1!+2!+……+5!之和的程序


main()

{ int i,j,p,s=0;

for(i=1;i<=5;i++) /* 外循環(huán) */

{ p=1;

for(j=1;j<=i;j++) /* 內(nèi)循環(huán) */

p=p*i;

s=s+p;

}

printf("Sum=%d\n",s);

}

[例5.12] 打印所示的三角形圖案。

? ? ? ? ? ? ?*

? ? ? ? ? ? ***

? ? ? ? ? ?*****

? ? ? ? ? *******

? ? ? ? ?*********


main()

{ int i,j;

for(i=1;i<=5;i++)

{ for(j=1;j<=i;j++)? printf(" ");

for(j=1;j<=i;j++)? printf("*");

printf("\n");

}

}

[例5.13] 人口增長問題。按年2%的增長速度,現(xiàn)有12億人,多少年后人口將達到或超過14億。


main()

{

int n=0;

float m=12;

do{

m=m*1.02;

n++;

}while(m<14);

printf("%d年后人口將超過14億\n",n);

}

[例5.14]? 驗證素數(shù)。


main()

{ int m,n;

printf("輸入一個正整數(shù):");

scanf("%d",&m);

for(n=2;n<=m-1;n++) /* n<=m-1可以寫成n<m */

? ?if(m%n==0)break;

if(n<m)

? ?printf("%d不是素數(shù)\n",m);

else

? ?printf("%d是素數(shù)\n",m);

}


更高效的算法:


#include <math.h>

main()

{ int m,n;

printf("輸入一個正整數(shù):");

scanf("%d",&m);

for(n=2;n<=sqrt(m);n++)

if(m%n==0)break;

if(n<=sqrt(m))

? ?printf("%d不是素數(shù)\n",m);

else

? ?printf("%d是素數(shù)\n",m);

}

[例5.15] 打印100~200之間的全部素數(shù),每行10數(shù)據(jù)。


#include <math.h>

main()

{ int m,n,k,i=0;

for(m=100;m<=200;m++)

{ k=sqrt(m);

for(n=2;n<=k;n++)

? ?if(m%n==0)break;

if(n>k)

{? printf("%4d",m);

? ?i++;

? ?if(i%10==0)printf("\n");

}

}

printf("\n");

}

[例5.16] 百錢買百雞。


? ?這是《算經(jīng)》中的一題:

? ? ?雞翁一值錢五,

? ? ?雞母一值錢三,

? ? ?雞雛三值錢一。

? ? ?百錢買百雞,問雞翁、母、雛各幾何?


main()

{ int cocks,hens,chicks;

printf("Cocks\tHens\tChicks\n");

for(cocks=1;cocks<=18;cocks++)

for(hens=1;hens<=32;hens++)

{ chicks=100-cocks-hens;

if(5.0*cocks+3.0*hens+chicks/3.0==100)

printf("%d\t%d\t%d\n",cocks,hens,chicks);

}

}


C語言練習的評論 (共 條)

分享到微博請遵守國家法律
子长县| 天台县| 镇赉县| 清水县| 阳高县| 师宗县| 大竹县| 乌鲁木齐县| 楚雄市| 洛隆县| 三江| 衡阳县| 兴安盟| 六安市| 景东| 太白县| 新安县| 夏津县| 沈阳市| 丹凤县| 花莲市| 乐至县| 万荣县| 罗定市| 台安县| 江油市| 扎兰屯市| 梓潼县| 定边县| 桐乡市| 遵义市| 新巴尔虎左旗| 响水县| 万宁市| 哈巴河县| 丰原市| 长宁区| 信宜市| 雅安市| 兴义市| 新巴尔虎左旗|