ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1068. Sum

Where is my mistake?Tell me please.C++.
Posted by Arthur (Arti) Arabyan 14 Jan 2009 23:40
#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.