小白學JAVA-----P1001
2016-09-22 15:43
316 查看
[align=left]Problem Description[/align]
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
[align=left]Input[/align]
The input will consist of a series of integers n, one integer per line.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1
100
[align=left]Sample Output[/align]
1
5050
[align=left]Author[/align]
DOOM III
------------------------------------------------
import java.util.Scanner;
class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int sum=0;
int a=sc.nextInt();
for(int i=1;i<=a;i++){
sum=sum+i;
}
System.out.println(sum);
System.out.println();
}
}
}
很简单的一道水题,意思就是 输入一个数,求这个数的前n项和,例如输入100就是1+2+3+··100;唯一需要注意的是每输出一个案例需要额外换一次行,这就是ACM,不容许一点错误
注意的是
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
[align=left]Input[/align]
The input will consist of a series of integers n, one integer per line.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1
100
[align=left]Sample Output[/align]
1
5050
[align=left]Author[/align]
DOOM III
------------------------------------------------
import java.util.Scanner;
class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int sum=0;
int a=sc.nextInt();
for(int i=1;i<=a;i++){
sum=sum+i;
}
System.out.println(sum);
System.out.println();
}
}
}
很简单的一道水题,意思就是 输入一个数,求这个数的前n项和,例如输入100就是1+2+3+··100;唯一需要注意的是每输出一个案例需要额外换一次行,这就是ACM,不容许一点错误
注意的是
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACMer博客瀑布流分析
- ACM程序设计大赛题目分类
- 2015年acm国内排名
- 计算字符串最后一个单词长度
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 蚂蚁爬行问题
- 蚂蚁爬行问题
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- HDOJ 1002 A + B Problem II (Big Numbers Addition)
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1002