《程式設計基礎(B)Ⅰ》實驗3-while迴圈結構程式設計
阿新 • • 發佈:2018-12-13
注意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; }
#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;
}
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; }
The Updation of Problem A : choose the even number to add up so,need to use the “if ” framework |
Althought it is the updation of Problem A if the chosen number is equal to the one asked the sum is added |
#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; }
按照題設做就行 同樣設定雷區 |
#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;
}
#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;
}