Вычислите количество всех натуральных чисел в интервале от А до В, которые делятся на C без остатка. Input.txt: А, В, С (1<=A, B, C<=10^{5} Uotput.txt: в выходном файле записывается число всех N значений ряда, делящихся на С без остатка
Romanf romanf отличник 2013-01-10t16: 13: 22+00: 00 1. подумать над алгоритмом 2. вот сам алгоритм: а. спросить у пользователя значения б. расчитать ответ в. выдать ответ на экран 3. написать код в паскале 4. исправить ошибки компиляции 5. протестировать, вводить разные числа 6. обнаружила, что если вводить числа наугад ответ получается отрицательным иногда 7. вставить код проверки введенных пользователем значений 8. убрать ошибки компиляции 9. протестировать 10. готово а вот и сама программа: program aerobus; uses crt; const totalplace = 160; var businessplaces, economyplaces: integer; businessprice, economyprice: real; totalcharge: real; a,b: integer; correctinput: boolean; begin clrscr; businessplaces: =totalplace div 4; economyplaces: = totalplace - businessplaces; writeln('business places count: ', businessplaces); writeln('economy places count: ', economyplaces); correctinput: =false; while not correctinput do begin write('please input business class ticket price: '); readln(businessprice); if(businessprice> 0) then begin correctinput: =true; end else begin writeln('the price should be a positive number, please try again'); end; end; economyprice: =businessprice/2; writeln('economy ticket price is: ', economyprice: 0: 2); correctinput: =false; while not correctinput do begin write('how many business tickets are left? : '); readln(a); if(a> =0) and (a< =businessplaces)then correctinput: =true; if(a< 0) then begin writeln('please input a positive number or 0, please try again'); end; if(a> businessplaces) then begin writeln('please input a number which is less or equal to the tolal business place count, please try again'); end; end; correctinput: =false; while not correctinput do begin write('how many economy tickets are left? : '); readln(b); if(b> =0) and (b< =economyplaces)then correctinput: =true; if(b< 0) then begin writeln('please input a positive number or 0, please try again'); end; if(b> economyplaces) then begin writeln('please input a number which is less or equal to the tolal economy place count, please try again'); end; end; totalcharge: =(businessplaces-a)*businessprice; totalcharge: =totalcharge+(economyplaces-b)*economyprice; writeln('the total charge is: ', totalcharge: 0: 2); writeln; writeln('press enter to exit'); readln; end.
Общее решение, позволяющее удалять из строк любой "мусор"
// PascalABC.Net 3.0, сборка 1066 procedure CheckString(var s:string; var n:integer); var i:integer; begin i:=Length(s); while i>0 do begin if not(s[i] in ['a'..'z']) then Delete(s,i,1); Dec(i) end; n:=Length(s) end;
var s1,s2:string; i,p,n1,n2:integer; begin Write('Введите первую строку: '); Readln(s1); CheckString(s1,n1); if n1=0 then Writeln('Введенная строка не содержит допустимых символов') else begin Write('Введите вторую строку: '); Readln(s2); CheckString(s2,n2); if n2=0 then Writeln('Введенная строка не содержит допустимых символов') else { теперь обе строки содержат только маленькие латинские буквы } if n1=n2 then begin for i:=1 to n1 do begin p:=Pos(s1[i],s2); if p=0 then Break else Delete(s2,p,1) end; if Length(s2)=0 then Writeln('Решение имеется') else Writeln('Решения нет') end else Writeln('Решения нет'); end end.
Тестовое решение: Введите первую строку: this is my own deal! Введите вторую строку: Тест: *is now=l2ead my sthi? Решение имеется
Также имеется современное решение, которому пока что в школах не учат: // PascalABC.Net 3.0, сборка 1066 begin var s1:=ReadString('Введите первую строку: ').Where(x->x in ['a'..'z']); if s1.Count=0 then Writeln('Введенная строка не содержит допустимых символов') else begin var s2:=ReadString('Введите вторую строку: ').Where(x->x in ['a'..'z']); if s2.Count=0 then Writeln('Введенная строка не содержит допустимых символов') else if s1.Except(s2).Count>0 then Writeln('Решения нет') else Writeln('Решение имеется') end end.
// PascalABC.Net 3.0, сборка 1066
procedure CheckString(var s:string; var n:integer);
var
i:integer;
begin
i:=Length(s);
while i>0 do begin
if not(s[i] in ['a'..'z']) then Delete(s,i,1);
Dec(i)
end;
n:=Length(s)
end;
var
s1,s2:string;
i,p,n1,n2:integer;
begin
Write('Введите первую строку: '); Readln(s1);
CheckString(s1,n1);
if n1=0 then Writeln('Введенная строка не содержит допустимых символов')
else begin
Write('Введите вторую строку: '); Readln(s2);
CheckString(s2,n2);
if n2=0 then Writeln('Введенная строка не содержит допустимых символов')
else
{ теперь обе строки содержат только маленькие латинские буквы }
if n1=n2 then begin
for i:=1 to n1 do begin
p:=Pos(s1[i],s2);
if p=0 then Break
else Delete(s2,p,1)
end;
if Length(s2)=0 then Writeln('Решение имеется')
else Writeln('Решения нет')
end
else
Writeln('Решения нет');
end
end.
Тестовое решение:
Введите первую строку: this is my own deal!
Введите вторую строку: Тест: *is now=l2ead my sthi?
Решение имеется
Также имеется современное решение, которому пока что в школах не учат:
// PascalABC.Net 3.0, сборка 1066
begin
var s1:=ReadString('Введите первую строку: ').Where(x->x in ['a'..'z']);
if s1.Count=0 then
Writeln('Введенная строка не содержит допустимых символов')
else begin
var s2:=ReadString('Введите вторую строку: ').Where(x->x in ['a'..'z']);
if s2.Count=0 then
Writeln('Введенная строка не содержит допустимых символов')
else
if s1.Except(s2).Count>0 then Writeln('Решения нет')
else Writeln('Решение имеется')
end
end.