|
|
back to boardDiscussion of Problem 1068. SumWhere is my mistake?Tell me please.C++. #include <iostream.h> main() {int n,i,s=0; cin>>n; if(n==0)cout<<1; else for(i=1;i<=n;i++)s+=i; cout<<s<<endl; return 0;} Your mistake is that n may be negative Posted by strider 15 Jan 2009 03:09 Re: Where is my mistake?Tell me please.C++. Posted by ARCH 26 Jan 2009 23:00 Artur jan error is that цыфры могут быть отрицательными тоже :) Here is on Pascal :) var n,s:longint; begin readln (n); s:=0; if n=0 then s:=1; if n>=0 then while n<>0 do begin s:=s+n; n:=n-1; end; if n<0 then while n<>2 do begin s:=s+n; n:=n+1; end; WriteLn(s); end. |
|
|