|
|
back to boardDiscussion of Problem 1068. SumWA TEST #2 Posted by Yuriy 19 Feb 2012 17:13 tell me what's wrong in my code ??? #include <stdio.h> #include <math.h> void main () { long N=-1,sum=0; int i=0; scanf("%d",&N); if (abs(N)<=10000) { if (N>0) { for (i=N;i>1;--i) { sum+=i; } } else if (N<0) { for (i=N;i<-1;++i) { sum+=i; } } printf("%ld",sum); } } Re: WA TEST #2 for (i=N;i<-1;++i) "лежащих между 1 и N включительно." may be because 1 != -1 ? for(i=N;i<=+1;i++) Edited by author 22.02.2012 02:08 Re: WA TEST #2 Edited by author 07.03.2012 18:11 Edited by author 07.03.2012 18:11 Re: WA TEST #2 Try so ;) #include <iostream> #include <cmath> using namespace std; int main() { int a, sum = 0; cin >> a; if( a > 0 ) { for ( int i = 1; i <= a; i++) sum += i; } else { for (int i = 1; i <= abs(a); i++) sum += i; sum = 1 - sum; } cout << sum; return 0; } |
|
|