7-1 sdut-C语言实验-A+B for Input-Output Practice (不确定次数循环)

Too easy?! Of course! I specially designed the problem for acm beginners.

You must have found that some problems have the same titles with this one,
yes, all these problems were designed for the same aim.

The input will consist of a series of pairs of integers a and b, separated by
a space, one pair of integers per line.

For each pair of input integers a and b you should output the sum of a and b
in one line, and with one line of output for each line in input.

1 5 10 20 1 5 10 20

6 30

#include <stdio.h> int main() { int a, b; while ((scanf("%d %d", &a, &b) !=
EOF)) printf("%d\n", a + b); return 0; }
7-2 sdut-C语言实验-偶数数位求和

6768

20 #include<stdio.h> int main() { int n;//输入整数n scanf("%d", &n); int a = 0;
int sum = 0; while (n > 0) { a = n % 10;//保留了个位数 n /= 10;//保留了除了个位数以外的数 if (a %
2 == 0) sum += a;//判断是否为偶数 } printf("%d", sum); return 0; }
7-3 sdut-C语言实验-小树快长高

n cm，每天长高k cm，他想知道多少天小树可以长到m cm。

100 200 5

20 #include <stdio.h> int main() { int n;//现在高度 int k;//每天长高的长度； int m;//期望高度
int i;//需要天数； scanf("%d %d %d",&n,&m,&k); while (n<m){ n=n+k; i++; }
printf("%d\n",i); return 0; }
7-4 sdut-C语言实验-数位数

1234567

7 #include <stdio.h> int main() { int n,i=0; scanf("%d",&n); while (n>0){
n/=10; i++; } printf("%d",i); return 0; }
7-5 sdut-C语言实验—— 数列求和

2

3 #include <stdio.h> int main() { int n; int i=0; int sum = 0; scanf("%d",
&n);//输入整数 while (i<=n) { sum += i; i++; } printf("%d", sum); return 0; }
7-6 sdut-C语言实验-N^3问题

3

351 #include <stdio.h> int main() { int n = 0;//输入一个整数 int sum = 0;//求和
scanf("%d", &n); int N; N = n * n * n; //计算出n的三次方；例如是3的三次方结果是27，再求各位数字的立方之和 int
a;//定义个位数 while (N >0) { a = N % 10;//个位数读取 sum += a * a * a;//对个位数求和； N /=
10;//留取个位数，这个N>0的话又会进入这个循环了 } printf("%d", sum); return 0; }
7-7 sdut-C语言实验-虎子的难题

2122345 2

3 #include <stdio.h> int main() { int n, m = 0;//输入整数n例如22434； scanf("%d %d",
&n, &m); int i=0;//定义次数； int a = 0; while (n > 0) { a=n % 10;//保留个位； n /=
10;//保留除个位之外的数 if (a == m) { i++; } } printf("%d", i); return 0; }
7-8 sdut-C语言实验- A+B for Input-Output Practice (II)

Input contains multiple test cases. Each test case contains a pair of integers
a and b, one pair of integers per line. A test case containing 0 0 terminates
the input and this test case is not to be processed.

For each pair of input integers a and b you should output the sum of a and b
in one line, and with one line of output for each line in input.

1 5 10 20 0 0

6 30 #include <stdio.h> int main() { int a, b = 0; while ((scanf("%d %d", &a,
&b) != EOF) && ((a != 0)) || (b != 0)) { printf("%d\n", a + b); } return 0; }
法二：
#include <stdio.h> int main() { int a,b; while ((scanf("%d
%d",&a,&b)!=EOF)&&((a!=0)||(b!=0))) printf("%d\n",a+b); }
7-9 sdut-C语言实验-A+B for Input-Output Practice (III)

The input will consist of a series of pairs of integers a and b, separated by
a space, one pair of integers per line.

For each pair of input integers a and b you should output the sum of a and b,
and followed by a blank line.

1 5 10 20

6 30 #include <stdio.h> int main() { int a,b; while (scanf("%d
%d",&a,&b)!=EOF){ printf("%d\n\n",a+b); } return 0; }
7-10 3n+1

3

7 #include <stdio.h> int main() { int n = 0; scanf("%d", &n);//输入n; int i =
0;//定义次数； while (n > 1) { if (n % 2 == 1) //n为奇数的表达方式 { n = 3 * n + 1; i +=
1;//注意这种求和的表达 } else if (n == 1)break; else if (n % 2 == 0) { n = n / 2; i +=
1; } } printf("%d", i); return 0; }
7-11 Average Plus

A number rounds to two decimal places, which is the average of the serial.

Using C, the printf for this case is:

printf("%.2f\n", average);

##输入例子
1.0 2.1 3.2 4.3 5.4 6.5
##输出例子
3.75 #include <stdio.h> int main() { double n;int i=0; double average,sum=0;
while (scanf("%lf",&n)!=EOF){ sum+=n;//求和 i++; average=sum/i; }
printf("%.2f",average); return 0; }
7-12 求总金额

3 5.6 8.4 4.5

18.50 #include <stdio.h> int main() { int n;//总件数 scanf("%d",
&n);//输入件数，注意件数不参与到循环求和之中； double x = 0; double sum = 0;//x代表每件价格，sum代表求和 while
(n--) { scanf("%lf", &x); sum += x; } printf("%.2f", sum); return 0; }

#include <stdio.h> int main() { int n;double p,sum=0; scanf("%d",&n);
while(scanf("%lf",&p)!=EOF){ sum+=p; } printf("%.2f",sum); }

7-13 判断4和7的倍数

30 21 5 16 9

2 #include <stdio.h> int main() { int n; int i = 0; while (scanf("%d", &n) !=
EOF) { if ((n % 4) == 0 || (n % 7) == 0 ) i++; } printf("%d", i); return 0; }
7-14 循环结构 —— 中国古代著名算题。趣味题目：物不知其数。

x，五五数之剩y，七七数剩z 。 x,y,z 的值可从键盘输入，请求出对应的最小 N 值并输出。

2 3 2

23

1 1 3

31 #include <stdio.h> int main() { int x,y,z; scanf("%d %d %d",&x,&y,&z); int
i=0; do {i++;} while (!(i%3==x&&i%5==y&&i%7==z)); /*有一个
!（a）,当a为真的时候，整体为假，循环结束，输出结果 所以这个题来说，就相当于满足后面那3个条件时，while判定为假，

7-15 N个数求和

，要求分子小于分母，且它们没有公因子。如果结果的整数部分为0，则只输出分数部分。

5 2/5 4/15 1/30 -2/60 8/3

3 1/3

2 4/3 2/3

2

3 1/3 -1/6 1/8

7/24 #include<stdio.h> #include <math.h> int M(int a,int a1) { int c, m, n; m
= fmax(a, a1); n = fmin(a, a1); while (n != 0) { c = m % n; m = n; n = c; }
return m; }//辗转相除法，返回最大公因数 int main(void) { int n, sum = 0; int a,
b;//a表示分子b表示分母 scanf("%d", &n); for (int i = 1; i <= n; ++i) {//n次，每次读一个分数 int
a1, b1; scanf("%d/%d", &a1, &b1); sum += a1 / b1; a1 = a1 % b1;//提出整数部分 if (i
== 1) { a = a1, b = b1;//第一次时初始化 } else { int m = b * b1 / M(b, b1);
//通过最大公因数找到最小公倍数 a = a * (m / b) + a1 * (m / b1); b = m; sum += a / b; a = a %
b; //通分计算 } while (1) { int l = M(a, b); if (l == 1) { break; } a /= l; b /= l;
}//while循环约分分子分母使其最简，每次都要操作避免数据溢出 } if (sum == 0) { if (a == 0) {
printf("0\n"); } else { printf("%d/%d", a, b); } } else { printf("%d", sum); if
(a == 0) { printf("\n"); } else { printf(" %d/%d", a, b); } }//最后判断并输出 return
0; }

7-16 念数字

0: ling 1: yi 2: er 3: san 4: si 5: wu 6: liu 7: qi 8: ba 9: jiu

yi er san si。

-600

fu liu ling ling #include <stdio.h> int count(int x); int main() { int n;
if(scanf("%d",&n)){};//输入要读的整数 int c; if(n<0) { printf("fu "); n=-n;
}//当输入的整数为负数时，取负数的相反数 c=count(n); for(int i=0;c>0;i++)//循环的控制条件为c>0,之前写成了n>0 {
int m; m=n/c;//取出该整数的第一位数字 n%=c; switch (m) { case 0: printf("ling"); break;
case 1: printf("yi"); break; case 2: printf("er"); break; case 3:
printf("san"); break; case 4: printf("si"); break; case 5: printf("wu"); break;
case 6: printf("liu"); break; case 7: printf("qi"); break; case 8:
printf("ba"); break; case 9: printf("jiu"); } if(c>9)//判断点，控制最后输出没有空格 {
printf(" "); } c/=10; } return 0; } int count(int x)//例如-400，则返回值为100 { int
cnt=1; while(x>9) { x/=10; cnt*=10; } return cnt; }

#include<stdio.h> int main() { int x; scanf("%d",&x); if (x<0){ printf("fu
");//fu后面是空格不是换行，要注意否则格式老是不对。 x=-x; } int mask =1; int t=x; while (t>9){ t/=10;
1:printf("yi");break; case 2:printf("er");break; case 3:printf("san");break;
case 4:printf("si");break; case 5:printf("wu");break; case
6:printf("liu");break; case 7:printf("qi");break; case 8:printf("ba");break;
7-17 最大公约数和最小公倍数

511 292

73 2044 /* 题目：输入两个正整数 m 和 n，求其最大公约数和最小公倍数。 程序分析： 最大公约数：

#include<stdio.h> int main() { int m,n; int gongyue,gongbei; scanf("%d
%d",&m,&n); //求最大公约数 int flag; if(m>n){flag = n;}else flag =
m;//找出m.n中小的那一个作为循环次数 for(int x= 1;x<=flag;x++) { if(m%x==0 &&n%x==0) { gongyue
= x; } } gongbei = m*n/gongyue; printf("%d %d",gongyue,gongbei); return 0; }

#include<stdio.h> int main() { int a,b,x,y; scanf("%d %d",&a,&b); int c,min;
if(a>b){ min=b; }else{ min=a; } for(c=1;c<=min;c++){ if(a%c==0&&b%c==0){ x=c; }
}//最大公约数=（a*b）/最小公倍数。 y=(a*b)/x; printf("%d %d\n",x,y); return 0; } #include
<stdio.h> int main() { int a,b,gongyue,gongbei; scanf("%d %d",&a,&b); int
min,i=0; if(a>b)min=b; else min=a; while(i<=min) { i++; if(a%i==0&&b%i==0)
gongyue=i; } gongbei=(a*b)/gongyue; printf("%d %d",gongyue,gongbei); }

7-18 求奇数和

8 7 4 3 70 5 6 101 -1

116 #include <stdio.h> int main() { int n,sum; scanf("%d",&n); while(n>0) {
if(n%2!=0) { sum += n; }//也不需要用break scanf("%d ",&n);//在while的循环里，用于实现输入一系列的数 }
printf("%d",sum); return 0; }

#include <stdio.h> int main() { int n,sum;
while(scanf("%d",&n)&&n>0)//注意这种表达方式 { if(n%2==1){ sum+=n; } }
printf("%d",sum); }
7-19 猜数字游戏

big”），还是小了（“Too
small”），相等表示猜到了。如果猜到，则结束程序。程序还要求统计猜的次数，如果1次猜出该数，提示“Bingo!”；如果3次以内猜到该数，则提示“Lucky
You!”；如果超过3次但是在N（>3）次以内（包括第N次）猜到该数，则提示“Good Guess!”；如果超过N次都没有猜到，则提示“Game
Over”，并结束程序。如果在到达N次之前，用户输入了一个负数，也输出“Game Over”，并结束程序。

58 4 70 50 56 58 60 -2 #include <stdio.h> int main(){ // randNum：随机数
guessNum：用户猜测数 N：次数 flag：标志 int randNum, guessNum, N, flag = 0; scanf("%d %d",
&randNum, &N); for( int i=1; i<=N; i++ ){ //循环输入N次结束 scanf("%d", &guessNum);
if( guessNum < 0 ) // 输入不合法 break; if( guessNum > randNum ) // 输入太大 printf("Too
big\n"); else if( guessNum < randNum ) // 输入太小 printf("Too small\n"); else if(
guessNum == randNum ){ // 相等 if( i == 1 ) // 1次成功 printf("Bingo!\n"); else if(
i <= 3 ) // 3次以内成功 printf("Lucky You!\n"); else if( i > 3 ) // 3次以上成功
printf("Good Guess!\n"); flag = 1; break; } } // 判断是否在N次内猜对 if( flag == 0 )
printf("Game Over\n"); return 0; }

#include <stdio.h> int main() { int i,a, b, N; scanf("%d %d", &a, &N); for(i =
1; i <= N; i++) { scanf("%d", &b); if (b <= 0) { printf("Game Over"); return 0;
} else if (b < a) { printf("Too small\n"); } else if (b > a) { printf("Too
big\n"); } else if (a == b && i == 1) { printf("Bingo!"); return 0; } else if
(a == b && i <= 3&&i!=1) { printf("Lucky You!"); return 0; } else if(a == b &&
i > 3) { printf("Good Guess!"); return 0; } else if(i == N && a != b) {
printf("Game Over"); return 0; } } printf("Game Over"); }
7-20 sdut-C语言实验- 计算球体积

#define PI 3.1415927

1 1.5

4.189 14.137 #include<stdio.h> #define pl 3.1415927 //常量定义 int main() { double
r,s; while(scanf("%lf",&r)!=EOF){ s = (4 / 3.0) * pl * r * r * r;
printf("%.3lf\n", s); } return 0; }

GitHub

Gitee