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 }

  

转载于:https://www.cnblogs.com/Lyush/archive/2011/08/29/2158822.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值