An easy problem
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14373 Accepted Submission(s): 3966
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
import java.util.Scanner;
public class Main {
private static Scanner scanner;
private static long arr[];
public static void main(String[] args) {
scanner = new Scanner(System.in);
dabiao();
while (scanner.hasNext()) {
int n = scanner.nextInt();
if (n < 0) {
break;
}
System.out.println(arr[n]);
}
}
private static void dabiao() {
arr = new long[100001];
arr[1] = 1;
for (long i = 2; i < arr.length; i++) {
int j = (int)i;
if(j%3 == 0){
//i*i*i的过程中 当i等于99999的时候 用int去存的话 有溢出
//(这里是吧i*i*i的值放在一个int的空间里面 然后再进行赋值运算 要尤其注意)
arr[j] = arr[j-1]+i*i*i;
}else {
arr[j] = arr[j-1]+j;
}
}
}
}