|
|
вернуться в форумПоказать все сообщения Спрятать все сообщенияI submitted 1068 twice and I always receive "WA" Then I read about the formula and I compared the answers(as you can see down) and they were all the same. Then I submitted the task once again with the the formula and I got "AC". Can someone explain why? var i,sum1,n,sum:longint; begin for n:=-10000 to 10000 do begin {formula} if n < 1 then sum := (( 1 + n) * ( 2 - n)) div 2 else if n >=1 then sum := (( n + 1) * n) div 2; {my solution} sum1:=0; if n=1 then sum1:=1 else if n<=0 then for i:=1 downto n do sum1:=sum1+i else for i:=1 to n do sum1:=sum1+i;
{if there were different answers} if sum<>sum1 then writeln(n,' ',sum1,' ',sum1); readln; end; end. > I submitted 1068 twice and I always receive "WA" > Then I read about the formula and I compared the > answers(as you can see down) and they were all > the same. Then I submitted the task once again > with the the formula and I got "AC". Can someone > explain why? > > var i,sum1,n,sum:longint; > begin > for n:=-10000 to 10000 do > begin > {formula} > if n < 1 then sum := (( 1 + n) * ( 2 - n)) div 2 > else if n >=1 then sum := (( n + 1) * n) div 2; > > {my solution} > sum1:=0; > if n=1 then sum1:=1 > else > if n<=0 then > for i:=1 downto n do sum1:=sum1+i > else > for i:=1 to n do sum1:=sum1+i; > > {if there were different answers} > if sum<>sum1 then writeln(n,' ',sum1,' ',sum1); readln; > end; > > end. > This is not my output this is a program that compares the two methods of finding the solution if sum<>sum1 then writeln(n,' ',sum1,' ',sum1); the main reson belongs to the server. Maybe some thing wrong with compiling i've submit your solution var i,sum1,n,sum:longint; begin read(n); {my solution} sum1:=0; if n=1 then sum1:=1 else if n<=0 then for i:=1 downto n do sum1:=sum1+i else for i:=1 to n do sum1:=sum1+i;
writeln(sum1);
end. and got WA, but when i change it to var i,sum1,n,sum:longint; begin read(n); {my solution} sum1:=0; if n=1 then sum1:=1 else if n<=0 then (changing in this line) for i:=n to 1 do sum1:=sum1+i else for i:=1 to n do sum1:=sum1+i;
writeln(sum1);
end. And i got AC, i don't know why, but maybe the compiler define that n is always >=1 and it does not run the command "for i:=1 downto n", so u get WA. So that, don't use "downto "when not necessary ! Btw, your program will run slower than any program that use the simple formula. QH@ |
|
|