|
|
back to boardDiscussion of Problem 1068. Sum1068. Sum - Java Posted by Alex 2 Oct 2013 10:01 Dear, sirs Could you help to find out the reason of WA ? // thank you import java.util.*; public class sumN { public static void main(String[] args) { Scanner in = new Scanner(System.in); int k = 1; int sum = 0; int N = in.nextInt(); int M = Math.abs(N); if (M<=10000) { int arr[] = new int[M-1];
for (int i=0; i<(M-1); i++) { k++; arr[i]=k; } for (int i=0; i<(M-1); i++) sum += arr[i]; System.out.print(sum); } } } Re: 1068. Sum - Java Posted by Alex 2 Oct 2013 10:18 also I tried import java.util.*; public class sumM { public static void main(String[] args) { Scanner in = new Scanner(System.in); int sum = 0; int N = in.nextInt(); int M = Math.abs(N); if (M<=10000) {
for (int i=1; i<=(M); i++) sum += i; System.out.print(sum-1);
} } } Edited by author 02.10.2013 10:19 Re: 1068. Sum - Java Posted by Alex 2 Oct 2013 10:52 solved import java.util.*; public class sumM { public static void main(String[] args) { Scanner in = new Scanner(System.in); int sum = 0; int N = in.nextInt(); if ((N>0) & (Math.abs(N)<=10000)) {
for (int i=1; i<=N; i++) sum += i;
} else if ((N<=0) & (Math.abs(N)<=10000)) {
for (int i=N; i<=1; i++) sum += i;
} System.out.print(sum); } } Re: 1068. Sum - Java this solution doesn't give the right answer |
|
|