|
|
back to boardDiscussion of Problem 1068. SumWrong answer test 2 on c++ #include <iostream> using namespace std; int main() { int n, res = 0; cin >> n; if (n < 0) { for (int i = -2; i >= n; i--) { res = res + i; } cout << res; } else { for (int i = 1; i <= n; i++) { res = res + i; } cout << res; } return 0; } Re: Wrong answer test 2 on c++ Posted by German 23 Oct 2016 16:47 int i = -2; i >= n; i-- i = 1 Re: Wrong answer test 2 on c++ Optimization trick with initial i=-2 is funny. But now you aren't processing case "n == 0" correctly. Re: Wrong answer test 2 on c++ Posted by RENT 10 Jan 2017 11:05 if(n == 0) --> ans = ? bro.. i don't understand... i'm from vietnam Re: Wrong answer test 2 on c++ Lets read task: > sum of all integer numbers lying between 1 and N inclusive If N==0 then set of integers to sum is [0..1], sum is 1. |
|
|