Dobry den,
jsem zacatecnik s pascalem. Dostal jsem se k jednomu ukolu a nevim jak s nim dal, nemusite mi posilat cely program, jenom poradit nejake tipy, treba jake cykly pouzit. Mam do programu zadat 2 cisla - interval. V tomto intervalu ma program najit vsechny suda a licha cisla a zvlast je secist. Nemusim to mit, ale rad bych vedel jak na to. Zajima me to. Za jakoukoli odpoved dekuju
Fórum › Pascal
Pro Vas hracka:-)

program project1;
uses crt;
var parne, neparne, i, x: Integer;
begin
clrscr;
write('Zadajte minimum intervalu: '); readln(i);
write('Zadajte maximum intervalu: '); readln(x);
parne:=0; neparne:=0;
for i:=i to x do
begin
if (i mod 2 = 0) then parne := parne+i
else neparne := neparne+i;
end;
WriteLn('Sucet parnych cisel je: ', parne);
WriteLn('Sucet neparnych cisel je: ', neparne);
readkey;
end.
Azda to je ok...
Jistě, normální programátor samozřejmě použije cyklus. Ale DOSem ošlehaný pascalovský geek odkojený AThelem a zocelený assemblerem si vystačí i bez cyklů. Výsledkem je nesrovnatelně rychlejší kód.
Procedure ZobrazVysledek(s,l,c:longint);
begin
writeln('soucet vsech cisel: ',c);
writeln('sudych: ',s);
writeln('lichych: ',l);
readln;
halt;
end;
var a,b,c,s,l,o,p:longint;
r,t:real;
begin
write('Zadej zacatek intervalu: ');
readln(a);
write('Zadej konec intervalu: ');
readln(b);
if a>b then
begin
writeln('Prazdny interval');
Halt;
end;
if a=b then
if odd(a) then ZobrazVysledek(0,a,a)
else ZobrazVysledek(a,0,a);
{jednoduche pripady skoncily, zacneme s matematikou}
o:=a+b; {soucet jedne dvojice}
p:=b-a+1; {kolik cisel se bude scitat}
c:=o*p div 2; {soucet jedne dvojice vynasobime poctem dvojic}
r:=c/2; {pri rozdeleni na suda a licha cisla budeme vychazet z}
{presne poloviny vysledku}
{ted provedeme distribuci rozdilu mezi sudou a lichou slozku}
if odd(p) then t:=o/4 {varianta pro lichy pocet cisel}
else t:=p/4; {a pro sudy}
l:=round(r+t); {defaultne dame vetsi cast sude slozce}
s:=c-l;
if not odd(b) then {...a eventualne to prohodime}
begin c:=l;l:=s;s:=c;c:=s+l;end;
ZobrazVysledek(s,l,c);
end.
Princip je ten, že čísla v posloupnosti můžeme rozdělit na N dvojic se stejným součtem. Příklad:
1+2+3+4+5+6= (1+6)+(2+5)+(3+4)
součet každé závorky je 7 a závorek je (6-1)+1
Tento princip objevil matematik Gauss.
Požadavek na oddělené sečtení sudých a lichých čísel je velká komplikace, ale nakonec jsem vymyslel jak na to. Jestli to objevil někdo přede mnou nevím, ale nejspíš jo :-)

Přidej příspěvek
Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×






























































Uživatelé prohlížející si toto vlákno
Uživatelé on-line: 0 registrovaných, 24 hostů
Podobná vlákna
Pro programatory hracka — založil Milan
Programátor pro Vás — založil Daniel Havránek
Váš názor = ? — založil Petroff
Podla vas... — založil ps2console
Moderátoři diskuze