2011 多项式求和
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int m, n, i;
double sum ,s= 1;
while(scanf("%d",&m)!=EOF){
while(m--){
sum =0;
scanf("%d",&n);
for(i=1; i<=n; i++)
sum+= s/i * pow(-1, i-1);
printf("%.2lf\n",sum);
}
}
return 0;
}
2012 素数判定
#include<stdio.h>
#include<math.h>
int main()
{
int x, y, i, m, j,t,a;
while (scanf("%d %d", &x,&y) != EOF)
{
if (x == 0 && y == 0) break;
if (x > y)
{
a = x;
x = y;
y = a;
}
t = 0;
for (i = x; i <= y; i++)
{
m = i * i + i + 41;
for (j = 2; j < m; j++)
{
if (m % j == 0)
{
t++;
break;
}
}
}
if (t == 0) printf("OK\n");
else printf("Sorry\n");
}
return 0;
}
2013 蟠桃记
#include<stdio.h>
#include<math.h>
int main()
{
int n,x,m,i;
x = 1;
while (scanf("%d", &n) != EOF)
{
for (i = n - 1; i >= 1; i--)
{
x = 2 * (x + 1);
}
printf("%d\n", x);
x = 1;
}
return 0;
}
简单递归
2014 青年歌手大赛
#include<stdio.h>
#include<math.h>
int main()
{
int n, max = 0, min = 100, i, a;
float sum = 0, score;
while (scanf("%d", &n)!=EOF)
{
for(i=1;i<=n;i++)
{
scanf("%d",&a);
if (a > max) max = a;
if (a < min) min = a;
sum = sum + a;
}
sum = sum - max - min;
score = sum / (n - 2);
printf("%.2f\n", score);
sum = 0;
min = 100;
max = 0;
}
return 0;
}
2015 偶数求和
2016 数据的交换输出
#include<stdio.h>
#include<math.h>
int main()
{
int n,i,a,min,t,temp,x;
int b[100];
while (scanf("%d", &n)!=EOF)
{
if (n == 0) break;
t = 0;
min = 10000000;
for (i = 0; i < n; i++)
{
scanf("%d", &a);
b[i] = a;
if (a < min) {
min = a;
x = i;
}
}
temp = min;
b[x]= b[0];
b[0] = temp;
for (i = 0; i < n; i++)
{
if (t != n-1)
{
printf("%d ", b[i]);
t++;
}
else printf("%d\n", b[i]);
}
}
return 0;
}
需要注意格式:最后一个字符后无空格