一、A+B for Input-Output Practice (II)
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
Your task is to Calculate a + b.
Input
Your task is to Calculate a + b.
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
2
1 5
10 20
Sample Output
6
30
#include<stdio.h>
int main()
{
int a,b,n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d%d",&a,&b);
printf("%d\n",a+b);
}
return 0;
}
二、C语言实验——计算1到n的和(循环结构)
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
从键盘上输入任意一个整数n,计算1到n的和。
Input
从键盘输入任意整数n。
Output
输出1到n的和。
Sample Input
3
Sample Output
6
#include<stdio.h>
int main()
{
int s,n;
scanf("%d",&n);
s = 0;
for(int i=1;i<=n;i++)
{
s = s + i;
}
printf("%d\n",s);
return 0;
}
三、C语言实验——求阶乘(循环结构)
Time Limit: 3000 ms Memory Limit: 65536 KiB
Problem Description
从键盘输入任意一个大于等于0的整数n,然后计算n的阶乘,并把它输出。
提示: 0!是 1 。
Input
输入任意一个大于等于0的整数n。
Output
输出n!
Sample Input
3
Sample Output
6
#include<stdio.h>
int main()
{
int s,n;
scanf("%d",&n);
s = 1;
for(int i=1;i<=n;i++)
{
s = s * i;
}
printf("%d\n",s);
return 0;
}
四、C语言实验——两个数比较
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
求2个数中较大者。
Input
第一行为测试的数据组数N,接下来的N行分别是两个待比较的整数。
Output
输出N行,每一行的值为每组数中较大的整数。
Sample Input
2
1 2
15 10
Sample Output
2
15
#include<stdio.h>
int main()
{
int a,b,n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a,&b);
if(a>b)
printf("%d\n",a);
else
printf("%d\n",b);
}
return 0;
}
五、C语言实验——判断素数(循环结构)
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
从键盘上输入任意一个正整数,然后判断该数是否为素数。
如果是素数则输出"This is a prime."
否则输出“This is not a prime.”
Input
输入任意一个正整数n(1 <= n <= 1000000)。
Output
判断n是否为素数,并输出判断结果:
如果n是素数则输出"This is a prime."
否则输出“This is not a prime.”
特别提醒:请注意对1的判定,1不是素数。
Sample Input
3
Sample Output
This is a prime.
#include<stdio.h>
#include<math.h>
int main()
{
int n,flag;
scanf("%d",&n);
flag=0;
if(n>=2)
{
for(int i=2; i<=sqrt(n); i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==1)
printf("This is not a prime.\n");
else
printf("This is a prime.\n");
}
else
printf("This is not a prime.\n");
return 0;
}
六、求绝对值最大值
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
求n个整数中的绝对值最大的数。
Input
输入数据有2行,第一行为n,第二行是n个整数。
Output
输出n个整数中绝对值最大的数。
Sample Input
5
-1 2 3 4 -5
Sample Output
-5
#include<stdio.h>
#include<math.h>
int main()
{
int n,max,a;
scanf("%d",&n);
max=0;
for(int i=0;i<n;i++){
scanf("%d",&a);
if(fabs(a)>fabs(max)) fabs()是求绝对值函数。
max=a;
}
printf("%d\n",max);
return 0;
}
七、C语言实验——圆周率
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
输入n值,并利用下列格里高里公式计算并输出圆周率:
Input
输入公式中的n值。
Output
输出圆周率,保留5位小数。
Sample Input
1
Sample Output
2.66667
#include<stdio.h>
int main()
{
int n;
double pi;
scanf("%d",&n);
pi = 0;
for(double i=1;i<=n;i++){
pi = pi + 1/(4*i-3)-1/(4*i-1);
}
pi=4*pi;
printf("%.5lf\n",pi);
return 0;
}
八、数列求和
Time Limit: 1000 ms Memory Limit: 32768 KiB
Problem Description
数列求和是一类常见的问题,本题有一定的代表性:
求s=a+aa+aaa+aaaa+……+aa…aa(n位)
其中,a的值由键盘输入,位数n也由键盘输入。
Input
第一行输入a的值;
第二行输入位数n。
Output
输出对n个数完成求和运算后的结果。
比如a=3,n=6时,s=3+33+333+3333+33333+333333
Sample Input
3
6
Sample Output
370368
#include<stdio.h>
int main()
{
int sum,s,a,b;
scanf("%d%d",&a,&b);
sum = a;
s = a;
for(int i=1; i<b; i++)
{
s = s * 10 + a;
sum = sum+s;
}
printf("%d\n",sum);
return 0;
}
九、小鑫の日常系列故事(五)——卡片游戏
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
小鑫和健健喜欢在课间的时候玩游戏,今天他手里有n张卡片,每张卡片有一个数字。于是他就想和健健比一比更幸运。
玩法是这样的:这n张卡片上的数字是没有规律的。他俩轮流取走最上面的一张卡片,最后谁取走卡片上的数字之和最大,谁就更幸运。(因为这个游戏是小鑫想出来的,总是小鑫拿第一张)
你能帮他们判断谁更幸运么?
Input
第一行为一个数字 n。100000>n>0
第二行为n个数,是n张卡片上的数字,第一个数为最上面的卡片上的数,依次向下。N个数是在整型范围内。
Output
如果小鑫更幸运输出“Greater than”,如果健健更幸运输出“Less than”,否则“Equal”。(输出均不含引号)
Sample Input
6
1 1 2 2 3 3
Sample Output
Equal
#include<stdio.h>
int main()
{
int n,sum1,sum2,a;
scanf("%d",&n);
sum1=sum2=0;
for(int i=0; i<n; i++)
{
scanf("%d",&a);
if(i%2==0)
sum1 = sum1 + a;
else
sum2 = sum2 + a;
}
if(sum1>sum2)
printf("Greater than\n");
else if(sum2>sum1)
printf("Less than\n");
else
printf("Equal\n");
return 0;
}
十、做乘法
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
请用C语言编写一个程序。此程序接收一个正整数N,然后打印输出“N次N*(1->N)格式”的数据。例如:此程序接收正整数5,那会输出以下格式的数据:
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
Input
只有一个正整数N(N<=100)。
Output
输出共N行数据,如上面的例子所示。
Sample Input
5
Sample Output
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
printf("%d*%d=%d\n",n,i,n*i);
}
return 0;
}
十一、简单计算
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
接受从键盘输入的N个整数,输出其中的最大值、最小值和平均值(平均值为整除的商)。
Input
第一行一个正整数N(N<=100);
第二行有N个用空格隔开的整数Ti (1 <= i <= N, 0 <= Ti <= 10000000)
Output
三个有空格隔开的整数分别为最大值、最小值和平均值,其中平均值为整除的商。
Sample Input
5
1 2 3 5 4
Sample Output
5 1 3
#include<stdio.h>
int main()
{
int n,a,max,min,sum;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%d",&a);
if(i==1){
max = min = sum = a;
}
else
{
if(a>max){
max = a;
}
else if(a<min){
min = a;
}
sum = sum + a;
}
}
printf("%d %d %d\n",max,min,sum/n);
return 0;
}
十二、猜糖块
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
小狗对小猫说:你猜猜我的口袋里有几块糖?小猫说:猜对了你给我吃吗?小狗点点头:嗯,猜对了两块都给你!小猫咽了咽口水说:我猜五块!然后,小狗笑着把糖放到小猫手里,说:我还欠你三块。
既然小猫这么喜欢吃糖,小狗决定每天都给小猫几块糖,但是呢,不能每天都给相同块数的糖,那样就太单调了。于是,第一天小狗给小猫1*1=1块,第二天2*2=4块……第 n 天给的糖数为 n*n 。现在已知小狗家共有 N 块糖,你需要帮他计算下这些糖最多可以给小猫几天?
Input
输入只有一个整数 N (0 <= N <= 10000)。
Output
输出对应的天数。
Sample Input
15
Sample Output
3
#include<stdio.h>
int main()
{
int n,sum;
scanf("%d",&n);
sum=0;
for(int i=1;;i++)
{
sum = sum + i*i;
if(sum>n)
{
printf("%d\n",i-1);
break;
}
}
return 0;
}
十三、C语言实验——分数序列
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
有一个分数序列:2/1, 3/2, 5/3, 8/5, 13/8, …编写程序求出这个序列的前n项之和。
Input
输入只有一个正整数n,1≤n≤10。
Output
输出该序列前n项和,结果保留小数后6位。
Sample Input
3
Sample Output
5.166667
#include<stdio.h>
int main()
{
int n;
double sum,a,b,x;
scanf("%d",&n);
sum=0;
a = 2;
b = 1;
for(double i=1;i<=n;i++)
{
sum = sum + a/b;
x = a;
a = a + b;
b = x;
}
printf("%.6lf\n",sum);
return 0;
}
十四、期末考试之分等级
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
期末考试结束了,老师想要根据学生们的成绩划分出等级。共有5个等级A,B,C,D和E。
划分方法如下,90分(含90)以上的为A,80~90(含80)间的为B,70~80(含70)间的为C,
60~70(含60)的为D,不及格的为E。
根据输入的成绩,编程输出各个级别段人数。
Input
输入第一行包含一个正整数N(N<= 100)代表学生的数目,接下来有N行数据每行一个整数(0~100)代表
一个学生的成绩。
Output
输出有五行格式如下:
A nA
B nB
C nC
D nD
E nE
其中A,B,C,D,E代表等级,nA,nB等代表个等级的人数,等级和人数之间有一个空格。
Sample Input
6
66
73
85
99
100
59
Sample Output
A 2
B 1
C 1
D 1
E 1
#include<stdio.h>
int main()
{
int n,x,a,b,c,d,e;
scanf("%d",&n);
a = b = c = d = e = 0;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
if(x>=90)
a++;
else if(x>=80&&x<90)
b++;
else if(x>=70&&x<80)
c++;
else if(x>=60&&x<70)
d++;
else
e++;
}
printf("A %d\nB %d\nC %d\nD %d\nE %d\n",a,b,c,d,e);
return 0;
}
十五、平方数
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
飞飞特别喜欢平方数,可是他数学并不好,你能帮他计算n与m之间所有平方数之和吗?
提示:若一个整数的开方还是整数,它就是平方数。例如:4、9、16、25是平方数。
Input
第一行 T 代表数据的组数。
接下来有 T 行,每行两个整数n,m (0 <= n, m <= 100000000)
Output
输出一个整数,代表所求区间内平方数之和。
Sample Input
3
1 4
3 10
17 20
Sample Output
5
13
0
#include<stdio.h>
int main()
{
int n,x,y,z,s;
scanf("%d",&n);
while(n--)
{
s = 0;
scanf("%d%d",&x,&y);
if(x>y) 必须判断x , y值的大小,因为输入不一定谁大。
{
z = x;
x = y;
y = z;
}
for(int i=0; i*i<=y; i++)
{
if(i*i>=x)
{
s = s+i*i;
}
}
printf("%d\n",s);
}
return 0;
}
十六、C语言实验——打印菱形
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
从键盘输入一个整数n(1≤n≤9),打印出指定的菱形。
Input
正整数n(1≤n≤9)。
Output
指定的菱形。
第一行前面有n-1个空格,第二行有n-2个空格,依此类推。
Sample Input
5
Sample Output
*
***
*****
*******
*********
*******
*****
***
*
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n-i; j++)
{
printf(" ");
}
for(int j=1; j<=2*i-1; j++)
{
printf("*");
}
printf("\n");
}
for(int i=1; i<n; i++)
{
for(int j=1; j<=i; j++)
{
printf(" ");
}
for(int j=1; j<=2*(n-i)-1; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
十七、水仙花数
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,是这样定义的:
“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=13+53+33。
现在要求输出所有在m和n范围内的水仙花数。
Input
输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。
Output
对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开;
如果给定的范围内不存在水仙花数,则输出no;
每个测试实例的输出占一行。
Sample Input
100 120
300 380
Sample Output
no
370 371
#include<stdio.h>
int main()
{
int m,n,a,b,c,flag;
while(~scanf("%d%d",&m,&n))
{
flag = 0;
for(int i = m; i<=n; i++)
{
a = i/100;
b = (i/10)%10;
c = i%10;
if(i == a*a*a+b*b*b+c*c*c)
{
if(flag==0)
{
printf("%d",i);
flag = 1;
}
else
printf(" %d",i);
}
}
if(flag==0)
printf("no");
printf("\n");
}
return 0;
}
十八、C语言实验——余弦
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
输入n的值,计算cos(x)。
Input
输入数据有多行,每行两个数,包括x和n。第一数据为x,第二个数据为n。
Output
输出cos(x)的值,保留4位小数。
Sample Input
0.0 100
1.5 3
Sample Output
1.0000
0.0701
#include<stdio.h>
int main()
{
double m,n,x,y,a,s;
while(~scanf("%lf%lf",&m,&n))
{
a = 1;
s = 1;
for(int i = 1; i<=n; i++)
{
x = -m*m;
y = 4*i*i-2*i;
a = a*x/y;
s = s + a;
}
printf("%.4lf\n",s);
}
return 0;
}
十九、完美的素数
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
素数又称质数。指一个大于1的自然数,除了1和此整数自身外,不能被其他自然数整除的数。我们定义:如果一个素数是完美的素数,当且仅当它的每一位数字之和也是一个素数。现在给你一个正整数,你需要写个程序判断一下这个数按照上面的定义是不是一个完美的素数。
Input
输入包含多组测试数据。
每组测试数据只包含一个正整数 n (1 < n <= 10^6)。
Output
对于每组测试数据,如果 n 是完美的素数,输出“YES”,否则输出“NO”(输出均不含引号)。
Sample Input
11
13
Sample Output
YES
NO
#include<stdio.h>
#include<math.h>
int main()
{
int n;
while(~scanf("%d",&n))
{
int flag,flag1,a,s;
flag=flag1=0;
for(int i = 2; i<=sqrt(n); i++)
{
if(n%i==0)
{
flag = 1;
break;
}
}
if(flag==0)
{
s = n%10;
while(n/10!=0)
{
a = (n/10)%10;
s = s + a;
n = n/10;
}
for(int j = 2; j<=sqrt(s); j++)
{
if(s%j==0)
{
flag1 = 1;
break;
}
}
}
if(flag==0&&flag1==0)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
二十、小金掰呀掰不够玉米
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
众所周知,C语言的学习是我们程序设计基础的重点和主要内容。
有一天,小金(a1s4z5)觉得好饿、好饿,于是去地里找玉米吃。他拿了一个很大的背包,可以装下很多很多玉米。
他掰玉米有一个习惯,第1次的时候掰1个,第2次的时候掰2个,第3次的时候掰3个...第n次的时候掰n个,他打算掰完第n次的时候就回家吃玉米。
在苞米地里,他越掰越高兴越掰越高兴,终于当他摩擦到要停不下来的时候,发现自己根本背不动他的背包了。于是他要将前m次掰的玉米全都扔掉才能回家开饭。但是小金的数学很不(li)好(hai),请你帮他算一算袋子里还有多少玉米。
Input
多组输入。
每组输入两个空格隔开的整数n和m,含义如题意描述。(0 < m < n < 10^4)
Output
输出小金的背包里最后剩下多少玉米。输入输出各占一行,保证数据合法。
Sample Input
5 2
6 3
Sample Output
12
15
#include<stdio.h>
int main()
{
int n,m,s;
while(~scanf("%d%d",&n,&m))
{
s = 0;
for(int i = m+1; i<=n; i++)
{
s = s + i;
}
printf("%d\n",s);
}
return 0;
}
二十一、C/C++练习7---求某个范围内的所有素数
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
求小于n的所有素数,按照每行10个显示出来。
Input
输入整数n(n<10000)。
Output
每行10个依次输出n以内(不包括n)的所有素数。如果一行有10个素数,每个素数后面都有一个空格,包括每行最后一个素数。
Sample Input
100
Sample Output
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
Hint
请注意题目中求的是小于n的所有素数。
#include<stdio.h>
#include<math.h>
int main()
{
int n,s,flag;
scanf("%d",&n);
flag = 0;
for(int i=2; i<n; i++)
{
s = 0;
for(int j=2; j<=sqrt(i); j++)
{
if(i%j==0)
{
s = 1;
break;
}
}
if(s==0)
{
flag++;
if(flag%10==1)
printf("%d",i);
else if(flag%10==0)
printf(" %d\n",i);
else
printf(" %d",i);
}
}
return 0;
}
二十二、C语言实验——素数
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
输出100->200之间的素数的个数,以及所有的素数。
Input
无
Output
100->200之间的素数的个数,以及所有的素数。
Sample Input
Sample Output
21
101 103 ... 197 199
#include<stdio.h>
#include<math.h>
int main()
{
int s,flag,a[30];
flag = 0;
for(int i=101; i<200; i++)
{
s = 0;
for(int j=2; j<=sqrt(i); j++)
{
if(i%j==0)
{
s = 1;
break;
}
}
if(s==0)
{
a[flag]=i;
flag++;
}
}
printf("%d\n",flag);
for(int i=0; i<flag; i++)
{
if(i<flag-1)
printf("%d ",a[i]); 这道题用到了后面的数组,也可以直接输出21;
else
printf("%d\n",a[i]);
}
return 0;
}
二十三、九九乘法表
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
九九乘法表是数学学习的基础,今天我们就来看看乘法表的相关问题。《九九乘法歌诀》,又常称为“小九九”,如下图所示。你的任务是写一个程序,对于给定的一个正整数 n ,输出“九九乘法表”的前 n 行。例如,输入 n 为 9,你的程序的输出将为下图:
Input
输入包含多组测试数据,以 EOF 结束。每组测试数据只包含一个正整数 n (0 < n < 10)。
Output
对于每组测试数据,输出上图所示“九九乘法表”的前 n 行。
Sample Input
2
3
Sample Output
1*1=1
1*2=2 2*2=4
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
Hint
必须使用for循环,如果你的代码中出现例如
if(n == 1) printf(“1*1=1\\n”);
if(n == 2) printf(“1*1=1\\n1*2=2 2*2=4\\n”);
或其类似语句,本题不得分。
#include<stdio.h>
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i = 1; i<=n; i++)
{
for(int j = 1; j<=i; j++)
{
if(j!=i)
printf("%d*%d=%d ",j,i,i*j);
else
printf("%d*%d=%d\n",j,i,i*j);
}
}
}
return 0;
}
二十四、区域内点的个数
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
X晚上睡不着的时候不喜欢玩手机,也不喜欢打游戏,他喜欢数星星。
Input
多组输入。
每组先输入一个整数N(N <= 10000),接着输入两个点代表矩形的左下点B(x,y)和右上点T(x,y),然后输入N个(X,Y)代表N颗星星。问有多少颗星星在窗子内部,在窗边上的不计。
Output
输出一个整数,代表有多少颗星星在窗子内部。
Sample Input
3
0 1
3 4
1 1
2 2
3 3
2
1 1
5 5
4 4
0 6
Sample Output
1
1
#include<stdio.h>
int main()
{
int n,a,b,x,y,o,p,s;
while(~scanf("%d",&n))
{
s = 0;
scanf("%d%d%d%d",&a,&b,&x,&y);
while(n--){
scanf("%d%d",&o,&p);
if(o>a&&o<x&&p>b&&p<y)
s++;
}
printf("%d\n",s);
}
return 0;
}