2009-01-15 16:21:41來源不明

2006 NPSC D. 海加爾山之戰

輸入的數,全部平方相加即是答案。

/*******************************************************/

  1. #include<stdio.h>      
  2. #include<stdlib.h>   
  3. #include<string.h>   
  4. main()   
  5. {   
  6.  int a,b,c,temp=0;   
  7.  while(scanf("%d",&a)==1&&a!=0)   
  8.   {temp=0;   
  9.    for(b=0;b<a;b++)   
  10.     {   
  11.      scanf("%d",&c);   
  12.      temp=temp+c*c;   
  13.     }   
  14.    printf("%d\n",temp);    
  15.   }      
  16.  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;