Sum Problem
Time Limit: 1000/500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 140390 Accepted Submission(s): 32755
Problem Description
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
Input
The input will consist of a series of integers n, one integer per line.
Output
For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.
Sample Input
Sample Output
Author
package endual;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in) ;
long sum = 0 ;
int n = 0 ;
while(cin.hasNextInt()){
n = cin.nextInt();
sum = digui(n) ;
System.out.println(sum + "\n\n");
}
}
private static long digui(int n) {
if (1 == n) {
return 1 ;
}
return n + digui(n-1) ;
}
}
不知道是为什么要两个斜杠,我知道这是说明的是换行
分享到:
相关推荐
这是北京大学acm1001 的原代码有用得着的可以看看
杭电acm第1001题,Sum Problem
#include using namespace std; int main() { int m,k=0; while(cin>>m) { if(m%2)k=(m+1)/2*m; else if((m+1)%2)k=m/2*(1+m); cout; } return 0; }
北大 acm 程序源代码 1001 1002 …… 1008
适合初学者参考交流。The input will consist of a series of integers n, one integer per line。For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the...
杭电ACM第1001题的代码。这个A+B PROBLEM的变形。初学者只要习惯了就好
HIT-ACM代码(1001-2517[部分题目]) 这是 哈工大ACM 1001-2517题中 部分题 的代码,哈工大ACM代码不好找,也不好下啊,特此提供下载。
ACM第1001题的答案,计算1到N的和。 Input: The input will consist of a series of integers n, one integer per line. Output: For each case, output SUM(n) in one line, followed by a blank line. You may ...
杭州电子科技大学acm1001
acm 1001 到1009代码,已通过验证
对刚刚进行acm训练的人使用,主要是输入格式问题,只有输入正确才能ac...
北大acm的p1001试题,经过验证已经通过,新手写的,算法比较简单,供大家参考
北京大学ACM详解poj1001, 内容很充实。
PKU 1001 手写实现高精度算法... 用于浮点运算.... ACcepted.
北大 ACM 第 1001 题 程序 源码:语言(C++)
ACM试题答案,从1001题到2517题,小部分题目因水平有限还没作答,还望原谅
是关于杭州电子科技大学的OJACM问题以及对应问题的代码,从1001题之2000题之间的大部分题目都有。
1001到1005都有,最权威的答案,都可以ac哦
这个是自己做的可以A过的。。不过有的还是没有优化好
#include int main(void) { int n, i, s, t; while (scanf("%d", &n) != EOF) { for (s = 1, i = 0 ; i ; i++) { scanf("%d", &t); if (t & 1) s *= t; } printf("%d\n", s);... 杭电acm的一些代码