C语言编程练习---2021级山东理工大学ACM实验三题解

发布于:2022-12-15 ⋅ 阅读:(1942) ⋅ 点赞:(1)

A - 数列求和3

Description

正整数序列是指从1开始的序列,例如{1,2,3,4,......}

给定一个整数 n,现在请你求出正整数序列 1 - n 的和。

Input

输入一个整数 n 。(1 <= n <= 1000)

Output

输出一个整数,即为正确答案。

Sample

Input 

2

Output 

3
#include<stdio.h>
int main()
{
	int n = 0;//自己终端输入一个正整数n
	int sum = 0;//用sum来代表最后的和
	int i = 0;//用i来代表次数
	scanf("%d", &n);//使之能够终端输入
	while (i<=n) {
		sum += i;
		i++;
	 }
	printf("%d", sum);
	return 0;
}

B - 数位数

Description

给定一个正整数 n ,请你求出它的位数。

Input

单组输入,输入一个整数 n 。(1<= n <= 2147483647)

Output

输出一行,包含一个整数,即为 n 的位数。

Sample

Input 

1234567

Output 

7
#include<stdio.h>
int main()
{
	int n;//输入一个正整数
	int i = 0;//记次数
	scanf("%d", &n);
	while (n > 0) {
		i++; n /= 10;
	}
	printf("%d", i);
	return 0;
}

C - N^3问题

Description

输入一个正整数N,求出N^3的各位数字的立方和。

Input

 输入N的值。N<=1024

Output

 问题描述中所要求的数值。

Sample

Input 

3

Output 

351

Hint

#include<stdio.h>
int main()
{
 int n =0;//初始化例如n=3
 scanf("%d", &n);//输入整数
 n = n * n * n;//三次方计算此时n=27
 int sum=0;//结果储存
 while (n > 0) {
  int a = n % 10;//a就是7了
  sum += a * a * a;//此时这个sum=7*7*7是343
  n/= 10;//n就是2了,这个n=2符合n>0会再次循环
  //再对2%10还是2然后再做三次方处理加和
 }
 printf("%d", sum);
 return 0;
}

D - 小树快长高

Description

小明在植树节种了一棵小树,小明非常关心小树,每天都给小树浇水,盼望着小树快快长高。他知道小树现在有 n cm,每天长高k cm,他想知道多少天小树可以长到m cm。

Input

输入三个整数  n, m, k。 ( 0 <= n<= 10000, 0 <= m <= 10000,0 <= k <= 10000)

Output

输出一个整数,即需要的天数。

Sample

Input 

100 200 5

Output 

20
#include<stdio.h>
int main()
{
	int a = 0;//现在长度
	int c = 0;//每天长高的长度
	int b= 0;//最后期望的高度
	int i = 0;//定义天数
	scanf("%d %d %d", &a, &b, &c);
	while (a < b) {
		i++;
		a = a + c;
	}
	printf("%d", i);
	return 0;
}

E - 偶数数位求和

Description

给定一个整数,请求出这个整数所有数位中是偶数的数位的和。例如,对于12436546,那么答案就是 2 + 4 + 6 + 4 + 6 。

Input

输入一个数 n 。 (0 <= n <= 2147483647)

Output

输出 n 的所有偶数数位的和。

Sample

Input 

6768

Output 

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;
 }


F - 小粉的难题

Description

小粉和哈士奇是好朋友,一天,哈士奇去找小粉玩,但小粉还没做完功课。粉妈妈说只有做完功课才能出门,这可急坏了小粉,于是小粉让哈士奇和他一块做功课。其中有一道题是这样的:

给出一个正整数 n 和数字 m ( m 取值范围[0,9]中的一个数字),求 m 在 n 中出现的次数。

比如 n = 2122345 , m = 2,答案就是  3 ,因为 2 在 2122345 中出现了三次。

哈士奇的数学不好,为了尽快做完功课,他找到了会编程的你,请你编写程序解决这个问题。

Input

输入只有一行,包含两个空格分开的整数 n 和 m 。(0 <= m <= 9,1 <= n <= 2147483647)

Output

输出一个数字,表示 m 在 n 中 出现的次数。

Sample

Input 

2122345 2

Output 

3
#include<stdio.h>
int main()
{
	int a = 0; int b = 0;//a代表一串数字
	//b代表某个数字
	scanf("%d %d", &a, &b);
	int i = 0;//次数即个数
	int n = 0;
	while (a > 0) {
		n = a % 10;//保留个位
		a /= 10;//保留前面的除个位以外的数
		if (n == b)i++;
	}
	printf("%d", i);
	return 0;
}

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

Description

Your task is to Calculate a + b.

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

Input

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

Output

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.

Sample

Input 

1 5
10 20

Output 

6
//这是一个求两数之和的题目,输入多对用空格分开的两个数a b,输出a+b的和,每一对数据的和占一行。编写
//代码时需要注意的是,由于没有指出有多少对输入数据,因此我们可以编写如下代码:多组输入

#include<stdio.h>
int main()
{
	int a, b;
	//输入结束时函数返回值为EOF,即没有数据输入时退出循环
	while (scanf("%d %d", &a, &b) != EOF){
	printf("%d\n", a + b); }
	return 0;
}

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

Description

Your task is to Calculate a + b.

Input

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.

Output

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.

Sample

Input 

1 5
10 20
0 0

Output 

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

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

Description

Your task is to Calculate a + b.

Input

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

Output

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

Sample

Input 

1 5
10 20

Output 

6

30
#include<stdio.h>
int main()
{
	int a, b;
	while (scanf("%d %d", &a, &b)!=EOF) {
		printf("%d\n\n", a + b);//\n\n即双换行
	}
	return 0;
}

J - 小金问呀问不会问题

Description

众所周知,C语言的学习是我们程序设计基础的重点和主要内容。
小金在班里是一个爱学习的好孩子,但是他的编程能力却有点差,不过他坚信自己一定可以进步并追上其他同学。
 

Input

多组输入。
从键盘读入一个整数n,如果n >= 0代表小金考试进步了,如果n < 0代表小金退步了。

Output

如果小明进步了输出”Yes”,反之输出”No”。输出不包括引号,输入输出各占一行,保证数据合法。

Sample

Input 

100
-100

Output 

Yes
No
#include<stdio.h>
int main()
{
	int n;
	while (scanf("%d", &n) != EOF) {
		if (n >= 0)printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}

K - 优越数

Description


给定3个数,如果有两个数大于他们的平均数则称这组数为优越数。(定义纯属虚构)

Input


输入第一行是一个整数: 表示测试数据的组数。
对于每组测试数据,仅一行3个整数。

Output


对于每组输入数据输出一行,判断它是否为一组优越数,如果是输出“Yes”(输出不包括引号),否则输出“No”。

Sample

Input 

2
1 2 3
1 4 4

Output 

No
Yes
#include<stdio.h>
int main()
{
	int n; //定义组数
	int a, b, c;//三个整数
	double p;//定义平均数
	scanf("%d", &n);//输入组数第一行
	while (n --) {
		int i=0;//定义次数即个数
		scanf("%d %d %d", &a, &b, &c);
		p = (a + b + c) / 3.0;
		if (a > p)i++;
		if (b > p)i++;
		if (c > p)i++;
		if (i>= 2)printf("Yes\n");
		else printf("N o\n");
	}
	return 0;
}

L - 分段函数求值

Description

有如下分段函数
F(x) = x^2 + 1   当x> 0时;
F(x) = -x   当x<0时;
F(x) = 100.0  当x=0时;
编程根据输入的不同x(x为实数),输出其对应的函数值

Input

多组输入,每组一个实数x。处理到文件结束。

Output

对于每组输入x,输出其对应的F(x),每组一行,结果保留1位小数。

Sample

Input 

8.00
-5.0

Output 

65.0
5.0
#include<stdio.h>
int main()
{
	double x = 0; double fx = 0;
	while (scanf("%lf", &x)!=EOF) {
		if (x > 0) { fx = x * x + 1; }
		else if (x <0) { fx = -x; }
		else { fx = 100.0; }
	printf("%.1lf\n", fx);
}
	return 0;
}
法二---
#include<stdio.h>
int main()
{
	double x = 0; 
	while (scanf("%lf", &x)!=EOF) {
		if (x > 0) printf("%.1lf\n", x * x + 1);
		if (x < 0)printf("%.1lf\n", -x);
		if (x==0)printf("100.0\n");
}
	return 0;
}

M - 压岁钱

Description

   SuShan过年要给孩子们发压岁钱喽,由于家里孩子很多,这可急坏了SuShan。你肯定以为她在担心钱不够,那你错了,她可是个有钱人儿,不差钱儿。她担心的是每个人分多少从而保证公平。
   SuShan从瑞士银行提出1000000来给孩子们分,由于来的孩子的数目不确定,所以SuShan希望你能帮他计算一下每个孩子给多少钱,从而保证每个孩子得到的都是整数。

Input

输入有多组数据,第一行 T 代表数据的组数。
接下来有 T 行,每行一个整数 N,代表孩子的数目,1<= N <= 10000000。

Output

只有一行。如果能够分给每个孩子相同数目的压岁钱,且都是整数,则输出每个孩子得到的钱数。否则输出No。

Sample

Input 

3
1
2
3

Output 

1000000
500000
No
#include<stdio.h>
int main()
{
	int t = 0;//t组数据
	scanf("%d", &t);
	int n, m;//n代表每行输入的整数
	while (t > 0) {
		t--;
		scanf("%d", &n);
		if (1000000 % n == 0) {
			m = 1000000 / n;
			printf("%d\n", m);
		}
		else printf("No\n");
	}
	return 0;
}

N - 计算球体积

Description

根据输入的半径值,计算球的体积。

Input

输入数据有多组,每组占一行,每行包括一个实数,表示球的半径。

Output

输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数。

Sample

Input 

1
1.5

Output 

4.189
14.137

Hint

已知 PI = 3.1415927

#define PI 3.1415927

#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;
}

O - 洗衣服

Description

X是一个勤劳的小孩,总是会帮助大人做家务。现在他想知道对于一根长为L的绳子能晾开多少件宽为W的衣服,显然这些衣服不能相互叠压。

Input

 多组输入。

每组输入两个整数L,W。

Output

 输出答案。

Sample

Input 

10 5
10 4

Output 

2
2

Hint

#include<stdio.h>
int main()
{
	int l, m, n;
	while (scanf("%d %d", &l, &m) != EOF) {
		n = l / m;
		printf("%d\n", n);
	}
	return 0;
}


网站公告

今日签到

点亮在社区的每一天
去签到