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

 

 

A - 数列求和3

注意while循环的条件

 

  
#include<stdio.h>
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<stdio.h>
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<stdio.h>
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<stdio.h>
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<stdio.h>
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<stdio.h>
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(ave<a){
    falg++;
}
if(ave<b){
    flag++;
}
if(ave<c){
    flag++;
}
if(flag>1)printf("Yes\n");
else printf("No\n");
}
return 0;
}
N - 计算球体积
#include<stdio.h>
#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;
}

 

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FawkesDoris

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值