HUST Online Judge WebBoard
Problem 4094 >> 输出超限
201803140220 @ 2019-04-07 19:59:41
[ Quote ] [ Edit ] [ Delete ] 1#
#include<stdio.h>

int f(int x);

int main()
{
int n,x;

while(scanf("%d",&n),n>0)
{
x=f(n);
printf("%d\n",x);
}

return 0;
}

int f(int x)
{
int i,j=1,s=0;

for(i=1;i<=x;i++)
{
j=j*(-1);
s=s+i*j;
}
return s;
}
201703120121 @ 2019-04-07 22:02:55
[ Quote ] [ Edit ] [ Delete ] 2#
while(scanf()!=EOF) while(~scanf())
如果是多组输入一般是读到文件尾,用上述输入。
另外,这个程序很暴力,试一下高效率的解法?
201803120226 @ 2019-04-10 16:45:46
[ Quote ] [ Edit ] [ Delete ] 3#
我的同样是时间超限 是输入的问题?
#include<stdio.h>

int fun(int x);
int main()
{
int n,j;
int a[2000][2];
int b=1500;
for(j=0;j<2000;j++){
if(scanf("%d",&a[j][0])!=1) break;
a[j][1]=fun(a[j][0]);

}
for(int i=0;i<j;i++){
printf("%d\n",a[i][1]);
}
return 0;
}

int fun(int x)
{
int i=1,sum=0,j=-1;

for(;i<=x;i++){
sum+=j*i;
j=-j;
}
return sum;
}
201703120121 @ 2019-04-10 18:39:39
[ Quote ] [ Edit ] [ Delete ] 4#
TO:201803120226
你的程序好像无法输出,而题目要求是输入一个数就计算输出数据