2009-01-15 16:21:41來源不明
2006 NPSC D. 海加爾山之戰
輸入的數,全部平方相加即是答案。
/*******************************************************/
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- main()
- {
- int a,b,c,temp=0;
- while(scanf("%d",&a)==1&&a!=0)
- {temp=0;
- for(b=0;b<a;b++)
- {
- scanf("%d",&c);
- temp=temp+c*c;
- }
- printf("%d\n",temp);
- }
- return 0;
- }
/********************************/
輸入太多,來個優化輸入吧!
#include<stdio.h>
#include<stdlib.h>
int input()
{
char cha;
int x=0;
while(cha=getchar())
if(cha!=' '&&cha!='\n') break;
x=cha-48;
while(cha=getchar())
{
if(cha==' '||cha=='\n') break;
x=x*10+cha-48;
}
return x;
}
main()
{
int a,b,c,temp=0;
while(scanf("%d",&a)==1&&a!=0)
{temp=0;
for(b=0;b<a;b++)
{
c=input();
temp=temp+c*c;
}
printf("%d\n",temp);
}
return 0;
}