HDOJ(HDU) 2132 An easy problem

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

水题。。注意范围。!!!java用long型可以AC,只是注意中间计算结果也有可能溢出int型范围,也要转换为long才行。
还有,注意判断条件退出不是输入-1,而是输入小于0的数就是退出了。

import java.util.Scanner;

public class Main{
    static long db[] = new long[100001];
    public static void main(String[] args) {
        dabiao();
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n =sc.nextInt();
            if(n<0){
                return;
            }
            System.out.println(db[n]);
        }
    }
    private static void dabiao() {
        db[1]=1;
        db[2]=3;
        for(int i=3;i<db.length;i++){
            if(i%3==0){
                db[i]=db[i-1]+i*(long)i*i;
                //这里的i*i要强转成long,long*int还是long,否则i*i*i会超int范围
            }else{
                db[i]=db[i-1]+i;
            }
        }

    }

}
时间: 2024-09-20 07:50:28

HDOJ(HDU) 2132 An easy problem的相关文章

HDOJ(HDU) 2123 An easy problem(简单题...)

Problem Description In this problem you need to make a multiply table of N * N ,just like the sample out. The element in the ith row and jth column should be the product(乘积) of i and j. Input The first line of input is an integer C which indicate the

hdu 2123 An easy problem

http://acm.hdu.edu.cn/showproblem.php?pid=2123 注意这里行末不能有空格!!! #include <iostream> using namespace std; int main() { int t,m; cin>>t; while(t--) { cin>>m; for(int i=1; i<=m; i++) { for(int j=1; j<=m; j++) { cout<<i*j; if(j==m)

【HDU 5572 An Easy Physics Problem】计算几何基础

2015上海区域赛现场赛第5题. 题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5572 题意:在平面上,已知圆(O, R),点B.A(均在圆外),向量V. 一个小球从A出发,沿速度向量V匀速前进,若撞到圆则发生弹性碰撞,沿"反射方向"继续匀速前进.问A点能否经过B点. 题目读懂了,把所有情况都考虑全,流程图就出来了,然后直接套模版即可(注意有些功能可剪裁~)   我的第一道计算几何,WA了一个星期啊...原因有姿势不对,精度不对,还有输

HDOJ/HDU 5686 Problem B(斐波拉契+大数~)

Problem Description 度熊面前有一个全是由1构成的字符串,被称为全1序列.你可以合并任意相邻的两个1,从而形成一个新的序列.对于给定的一个全1序列,请计算根据以上方法,可以构成多少种不同的序列. Input 这里包括多组测试数据,每组测试数据包含一个正整数N,代表全1序列的长度. 1≤N≤200 Output 对于每组测试数据,输出一个整数,代表由题目中所给定的全1序列所能形成的新序列的数量. Sample Input 1 3 5 Sample Output 1 3 8 Hin

HDOJ(HDU) 1898 Sempr == The Best Problem Solver?(水题、、、)

Problem Description As is known to all, Sempr(Liangjing Wang) had solved more than 1400 problems on POJ, but nobody know the days and nights he had spent on solving problems. Xiangsanzi(Chen Zhou) was a perfect problem solver too. Now this is a story

HDOJ/HDU 1022 Train Problem I(模拟栈)

Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes

HDOJ 2055 An easy problem

Problem Description we define f(A) = 1, f(a) = -1, f(B) = 2, f(b) = -2, - f(Z) = 26, f(z) = -26; Give you a letter x and a number y , you should output the result of y+f(x). Input On the first line, contains a number T.then T lines follow, each line

HDOJ/HDU 1161 Eddy&amp;#39;s mistakes(大写字母转换成小写字母)

Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for example "computer science" is written frequently "coMpUtEr scIeNce" by him, this mistakes lets Eddy's English teacher be extremely disco

HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)

Problem Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more t