2007 平方和与立方和
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Problem Description
给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和。
Intput
输入数据包含多组测试实例,每组测试实例包含一行,由两个整数m和n组成。
Output
对于每组输入数据,输出一行,应包括两个整数x和y,分别表示该段连续的整数中所有偶数的平方和以及所有奇数的立方和。
你可以认为32位整数足以保存结果。
Sample Intput
1 3
2 5
Sample Output
4 28
20 152
Submit
#include<stdio.h>
int main()
{
int m, n, i;
while(~scanf("%d %d", &m, &n))
{
int a = 0, b = 0;
if (m > n)
{
i = m;
m = n;
n = i;
}
for(i = m ; i <=n; i++)
{
if(i % 2 == 0)
a += i*i;
if(i % 2 == 1)
b += i*i*i;
}
printf("%d %d\n", a, b);
}
return 0;
}
2008 数值统计
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Problem Description
统计给定的n个数中,负数、零和正数的个数。
Intput
输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的个数,然后是n个实数;如果n=0,则表示输入结束,该行不做处理。
Output
对于每组输入数据,输出一行a,b和c,分别表示给定的数据中负数、零和正数的个数。
Sample Intput
6 0 1 2 3 -1 0
5 1 2 3 4 0.5
0
Sample Output
1 2 3
0 0 5
Submit
#include<stdio.h>
int main()
{
int n;
while(scanf("%d", &n) != 0)
{
int a = 0, b = 0, c = 0;
double aa;
if (n == 0)
break;
while(n--)
{
scanf("%lf", &aa);
if(aa > 0)
a++;
else if(aa == 0)
b++;
else
c++;
}
printf("%d %d %d\n", c, b, a);
}
return 0;
}
2009 求数列的和
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Problem Description
数列的定义如下:
数列的第一项为n,以后各项为前一项的平方根,求数列的前m项的和。
Intput
输入数据有多组,每组占一行,由两个整数n(n<10000)和m(m<1000)组成,n和m的含义如前所述。
Output
对于每组输入数据,输出该数列的和,每个测试实例占一行,要求精度保留2位小数。
Sample Intput
81 4
2 2
Sample Output
94.73
3.41
Submit
#include<stdio.h>
#include<math.h>
int main()
{
int m;
double n, count;
while(~scanf("%lf %d", &n, &m))
{
count = 0;
while(m--)
{
count = count + n;
n = sqrt(n);
}
printf("%.2lf\n", count);
}
return 0;
}
2010 水仙花数
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Problem Description
春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:
“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=13+53+3^3。
现在要求输出所有在m和n范围内的水仙花数。
Intput
输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。
Output
对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开;
如果给定的范围内不存在水仙花数,则输出no;
每个测试实例的输出占一行。
Sample Input
100 120
300 380
Sample Output
no
370 371
Submit
#include<stdio.h>
int aa(int i)
{
int a, b ,c;
a = i / 100;
b = i%100/10;
c = i%100%10;
if(i == a*a*a + b*b*b + c*c*c)
return 1;
else
return 0;
}
int main()
{
int m, n, i;
while(~scanf("%d %d", &m, &n))
{
int flag = 0;
for(i = m; i <= n; i++)
{
if(aa(i))
{
if (flag == 0)
printf("%d", i);
else
printf(" %d", i);
flag++;
}
}
if(flag == 0)
printf("no\n");
else
printf("\n");
}
return 0;
}