"Huawei OJ" "Algorithm Total chapter" "Huawei OJ" "061-Arithmetic Progression" "Project Download" topic description
功能:等差数列 2,5,8,11,14。。。。输入:正整数N >0输出:求等差数列前N项和返回:转换成功返回 0 ,非法输入与异常返回-1
Enter a description
输入一个正整数。
Output description
输出一个相加后的整数。
Input example
2
Output example
7
Algorithm implementation
ImportJava.util.Scanner;/** * Author: Wang Junshu * date:2015-12-25 16:59 * All rights Reserved!!! */ Public class Main { Public Static void Main(string[] args) {Scanner Scanner =NewScanner (system.in);//Scanner Scanner = new Scanner (Main.class.getClassLoader (). getResourceAsStream ("Data.txt")); while(Scanner.hasnext ()) {String input = Scanner.nextline ();Try{System.out.println (SUM (integer.parseint (input))); }Catch(Exception e) {System.out.println ("-1"); }} scanner.close (); }Private Static int sum(intN) {return(3* n +1) * N/2; }}
"Huawei OJ" "061-arithmetic progression"