首页 > 其他 > 详细

Calculate the formula

时间:2014-11-10 23:08:52      阅读:270      评论:0      收藏:0      [点我收藏+]
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
 
用普通的做法会超时,只能用公式算n*(4*n*n-1)/3(其中n为第几个数),还有题目明明说好结果在int范围内的,但是必须用__int64才能过,郁闷。。。
 
 1 #include <stdio.h>
 2 
 3 int main(){
 4     __int64 number;
 5     __int64 n;
 6     __int64 result;
 7 
 8     while(scanf("%I64d",&number)!=EOF){
 9         n=(number+1)/2;
10 
11         result=n*(4*n*n-1)/3;
12         printf("%I64d\n",result);
13 
14     }
15     return 0;
16 }

 

 
 

 

Calculate the formula

原文:http://www.cnblogs.com/zqxLonely/p/4088238.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!