HDU-2123 Calculate the formula

Calculate the formula

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3836    Accepted Submission(s): 1126


Problem Description
You just need to calculate the sum of the formula: 1^2+3^2+5^2+……+ n ^2.
 


Input
In each case, there is an odd positive integer n.
 


Output
Print the sum. Make sure the sum will not exceed 2^31-1
 


Sample Input
3
 


Sample Output
10
 
  这题首先是测出了他到2 ^31-1时N是多少。2344就溢出了,只要算到2343即可。  噼里啪啦敲完了,可是排名实在靠后,输入外挂,对,这时好东西。但是这题又有点不好处理 ,那就是出入数据是要来判定是否已经读到文件结尾,熟悉了scanf这个函数可以用 == EOF来判定是否结束,其实getchar也能够用来判定是否结尾。。。
  代码如下:
 1 #include <cstdio>
2 #include <cstring>
3 #include <cstdlib>
4 using namespace std;
5
6 int rec[2345];
7
8 bool getint( int &t )
9 {
10 char c;
11 while( c = getchar(), c < '0' || c > '9' )
12 {
13 if( c == EOF )
14 return false;
15 }
16 t = c - '0';
17 while( c = getchar(), c >= '0' && c <= '9' )
18 t = t * 10 + c - '0';
19 return true;
20 }
21
22 int main()
23 {
24 rec[1] = 1;
25 for( int i = 3; i <= 2343; i += 2 )
26 {
27 rec[i] = rec[i-2] + i * i;
28 }
29 int N;
30 while( getint( N ) )
31 {
32 printf( "%d\n", rec[N] );
33 }
34 return 0;
35 }

  

你可能感兴趣的:(form)