《程序设计基础(B)Ⅰ》实验3-while循环结构程序设计

 

 

A - 数列求和3

注意while循环的条件

 

   
#include
int main()
{
int n,sum;
scanf("%d",&n);
int i =1;
sum = 0;
while(i<=n){
    sum+=i;
    i++;
}
printf("%d\n",sum);
return 0;
}
 

数位数

#include
int main()
{
long int x;
    int n = 0;//n用来记录位数
    scanf("%ld",&x);
if(x == 0)//特殊情况单独判定
printf("%d\n",n);
else{
    while(x/1 != 0){
        n++;
        x /= 10;
    }
    printf("%d",n);
}
return 0;
}
C - N^3问题
#include
int main()
{
int n;
scanf("%d",&n);
int a;
a = n*n*n;
int b;
int sum = 0;
while(a/1 !=0){
    b = a%10;//用b存储a的各位数字
    sum += b*b*b;
    a = a/10;
}
printf("%d\n",sum);
return 0;
}
 

D - 小树快长高

A题的升级版

本题设置雷区

#include
int main()
{
int n,m,k,i;
int sum = 0;
scanf("%d %d %d",&n,&m,&k);
i = n;
while(i<=m){
    sum++;
    i = i+k;
}//大约在附近,我埋了个雷,很容易解决
printf("%d\n",sum);
return 0;
}

 

E - 偶数数位求和

The Updation of Problem A :

choose the even number to add up

so,need to use the “if ” framework

F - 小粉的难题

Althought it is the updation of Problem A

if the chosen number is equal to the one asked

the sum is added

 

 

G - A+B for Input-Output Practice (I)

多组输入 单组相加

 

#include
int main()
{
int a,b;
while(scanf("%d %d",&a,&b)!=EOF){
        //while(~scanf("%d %d",&a,&b))也可
    printf("%d\n",a+b);
}
return 0;
}

 

 

H - A+B for Input-Output Practice (III)

G题的Plus 在while内加入和判断语句 即 while(scanf'    && a!=0&&b!=0)

 

I - A+B for Input-Output Practice (VII)

G题的another plus 在printf行内多加个换行

K - 优越数

按照题设做就行

同样设置雷区

 

#include
int main()
{
int n;
scanf("%d",&n);
while(n--){
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
int flag = 0;//设定标记符,一方面省存储空间和读取时间,另一方面,方便计算
int ave;
ave = (a+b+c)/3;
if(ave1)printf("Yes\n");
else printf("No\n");
}
return 0;
}
N - 计算球体积
#include
#define PI 3.1415927
int main()
{
double r,v;
while(~scanf("%lf",&r)){
    v = 4*PI*r*r*r/3;
    printf("%.3lf\n",v);
}
return 0;
}

 

你可能感兴趣的:(《程序设计基础(B)Ⅰ》实验3-while循环结构程序设计)