"Huawei OJ" "061-arithmetic progression"

Source: Internet
Author: User

"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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.