现在的位置: 首页 > 综合 > 正文

杭电acm 2132

2013年08月17日 ⁄ 综合 ⁄ 共 1094字 ⁄ 字号 评论关闭

                     An easy problem

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6143    Accepted Submission(s): 1666

Problem Description
We once did a lot of recursional problem . I think some of them is easy for you and some if hard for you.
Now there is a very easy problem . I think you can AC it.
  We can define sum(n) as follow:
  if i can be divided exactly by 3 sum(i) = sum(i-1) + i*i*i;else sum(i) = sum(i-1) + i;
  Is it very easy ? Please begin to program to AC it..-_-

Input
  The input file contains multilple cases.
  Every cases contain only ont line, every line contains a integer n (n<=100000).
  when n is a negative indicate the end of file.

Output
  output the result sum(n).

Sample Input
1 2 3 -1

Sample Output
1 3 30

Author
Wendell

Source

Recommend

威士忌

对于这个问题呢   一点要注意n的取值哦    n要用_int64来定义   否则会报错哦    所以一定要小心   

#include<iostream>
#include<cstdio>

using namespace std;
int main()
{
    _int64 sum,n;

    while(cin>>n)
    {
        sum=0;
        if(n<0)
        break;

        if(n%3==0)
       {
           n=n/3;
	       sum=(n+1)*(n+1)*n*n/4*27+3*n*n;
       }
        else if(n%3==1)
       {
           n=n/3;
	       sum=(n+1)*(n+1)*n*n/4*27+3*n*n+n*3+1;
       }
        else if(n%3==2)
        {
           n=n/3;
	       sum=(n+1)*(n+1)*n*n/4*27+3*n*n+n*3*2+3;
//这里要小心一点很容易出错哦   呵呵        }
        cout<<sum<<endl;
    }
    return 0;
}

 

抱歉!评论已关闭.