HDU2132 an easy Problem "Java"

Source: Internet
Author: User

An easy problemTime limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total Submission (s): 9194 Accepted Submission (s): 2491

Problem Descriptionwe once did a lot of recursional problem. I think some of them is easy for you and some if hard for you.
Now there are 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 with 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
123-1

Sample Output
1330

Authorwendell
SOURCEHDU 2007-11 programming Contest_warmup Java water too.

Import Java.util.scanner;import Java.math.biginteger;public class Main {    static final int maxn = 100005;    Static biginteger[] arr = new BIGINTEGER[MAXN];    public static void Main (string[] args) {        Scanner cin = new Scanner (system.in);        Arr[0] = Biginteger.zero;        BigInteger tmp;        for (int i = 1; I <= 100000; ++i) {            tmp = biginteger.valueof (i);            if (i% 3 = = 0)                Arr[i] = Arr[i-1].add (tmp.multiply (TMP). Multiply (TMP));            else Arr[i] = Arr[i-1].add (TMP);        }        int n;        while (true) {            n = cin.nextint ();            if (n < 0) break;            System.out.println (Arr[n]);}}    

C code seems to be simpler.

#include <stdio.h> #define MAXN 100005typedef __int64 LL; LL Arr[maxn];int Main () {    int n, i;    for (i = 1; I <= 100000; ++i)        if (i% 3) arr[i] = arr[i-1] + i;        else arr[i] = Arr[i-1] + (LL) i * i * i;    while (scanf ("%d", &n), n >= 0)        printf ("%i64d\n", Arr[n]);}


HDU2132 an easy Problem "Java"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.