蓝桥杯练习系统之入门训练 BEGIN-2 序列求和

BEGIN-2 序列求和

问题描述
求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) {
    
    
		
		Scanner in = new Scanner(System.in);
		long sum=1;
		int  n = in.nextInt();
	
		for(int i=2,j=n;i<=j;i++,j--)
		{
    
    
			if(i==j)
				sum+=i;
			else
				sum+=i+j;
		}
		System.out.println(sum);

	}

}

猜你喜欢

转载自blog.csdn.net/qq_39273319/article/details/103357028