序列求和
资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
求1+2+3+…+n的值。
输入格式
输入包括一个整数n。
输出格式
输出一行,包括一个整数,表示1+2+3+…+n的值。
样例输入
4
样例输出
10
样例输入
100
样例输出
5050
数据规模与约定
1 <= n <= 1,000,000,000。
方法一、
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
long n;
n = sc.nextLong();
System.out.println((1 + n) * n / 2);
}
}
方法二、
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
Integer n;
n = sc.nextInt();
for(int i = 1; i<=n; i++)
res += i;
System.out.println(res);
}
}