Grauznis Posted August 16, 2014 Share Posted August 16, 2014 Uzdevums : Sastādīt programmu, kas atrod dotā naturālā skaitļa ciparu summu; pēc tam – iegūtāskaitļa ciparu summu utt. Līdz palicis viens cipars. Mana programma: Program skaitlis; Var a : longint; Function sk( x : longint) : longint; Var t :longint; Begin t := 0; Repeat t := t + ( x mod 10); x:= x div 10; until x = 0; if (t < 10) then sk := t else sk(t); End; Begin readln(a); writeln(sk(a)); End. Programma drabojas skaitļiem, kā 25 , bet ievadot skaitli 976 izvada 0. Vai kāds varētu paskaidrot kāpēc tā? Pareizo rezultātu izdevās izvadīt ar šadu programmu : Program skaitlis; Var a : longint; Function sk( x : longint) : longint; Var t :longint; Begin t := 0; Repeat t := t + ( x mod 10); x:= x div 10; until x = 0; if (t < 10) then begin writeln(t); halt; end else sk(t); End; Begin readln(a); sk(a); End. Kāpēc pirmā programma nedarbojas nedorbojas? Link to comment Share on other sites More sharing options...
marrtins Posted August 16, 2014 Share Posted August 16, 2014 if (t < 10) then sk := t else sk(t); if (t < 10) then sk := t else sk:=sk(t); Link to comment Share on other sites More sharing options...
Grauznis Posted August 16, 2014 Author Share Posted August 16, 2014 Paldies! Nepamanīju. Link to comment Share on other sites More sharing options...
AndrisBB Posted August 16, 2014 Share Posted August 16, 2014 (edited) Neko nezinu par paskalu, bet man izskatas ka tu te neko neatgriez, ja izpildas ELSE if (t < 10) then sk := t else sk(t); Manuprat vajadzetu kka ta: if (t < 10) then sk := t else sk := sk(t); .... oi, sorry izskatas ka pa lenu domaju un rakstu Edited August 16, 2014 by AndrisBB Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now