首页 > 代码库 > NYOJ题目436sum of all integer numbers

NYOJ题目436sum of all integer numbers

技术分享

--------------------------------------

需要注意的地方就是n的绝对值不大于10000,表示n有可能是负数。

AC代码:

 1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4  5 public class Main { 6  7     public static void main(String[] args) throws NumberFormatException, IOException { 8          9         BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));10         11         boolean first=true;12         while(first || reader.ready()){13             first=false;14             15             int n=Integer.parseInt(reader.readLine());16             long ans=n;17             if(n>0){18                 while(--n>=1) ans+=n;19             }else{20                 while(++n<=1) ans+=n;21             }22             System.out.println(ans);23         }24         25     }26     27 }

 题目来源: http://acm.nyist.net/JudgeOnline/problem.php?pid=436

NYOJ题目436sum of all integer numbers