|
|
вернуться в форумC++ Answer #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; } Re: C++ Answer hell whats problm with mine . #include <iostream> using namespace std; int main() { int A=0; double Ans=0; cin>>A; if(A<=10000) { if(A>=1) Ans=A*(A+1)/2; if(A<0) Ans=(A*-1)*(A-1)/2+1; cout<<Ans; } return 0; } Re: C++ Answer What about less then 10000? Re: C++ Answer Послано ShT3ch 24 авг 2012 00:46 WA 9? Floating types can return answer like 123e+10. So you have to do "int Ans" before got answ. In add: Just use formula s(a1,an)=(a1,an)/2*n like that
int(((1+n)/2)*(abs(1-n)+1))); Edited by author 24.08.2012 00:47 Re: C++ Answer hello, whats problm with mine . #include <iostream> bool flag=true;//the number more than 0 int num=0; bool GetNum() { scanf("%d",&num); return num>=0; } int GetResoults() { //Sn=(a1+an)n/2 if (flag)//>0 { return (1+num)*num/2; } else//<0 { return (num+1)*((-num)+2)/2; } } int main() { flag=GetNum(); printf("%d\n",GetResoults()); return 0; } Re: C++ Answer -10000-10000 |
|
|