File18. Дан файл вещественных чисел. Найти его первый локальный минимум (локальным минимумом называется элемент, который меньше своих соседей).
>Решение Pascal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | program File18; var S:String; F_in,F_out: File of Real; El2,El1,El:Real; begin Write('File: '); Readln(S); Assign(F_in,S); Reset(F_in); Read(F_in,El1); Read(F_in,El); El2:=El1; if (El1<El) then Writeln(El1) else begin while (not eof(F_in)) and not((El>El1) and (El1<El2)) do begin El2:=El1; El1:=El; Read(F_in,El); end; if (not eof(F_in)) then Writeln(El1) else if eof(F_in) and (El1>El) then Writeln(El); end; Close(F_in); end. |
Решение C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include <iostream> #include <fstream> #include <string> using namespace std; double myAtof ( string &num){ double tmp; sscanf ( num.c_str(), "%lf" , &tmp); return tmp; } int main(){ float x1, x2; string buf; ifstream ifs("numbers.txt"); if ( ! ifs.is_open() ){ cerr << "Can't open input file!" << endl; return 1; } if (ifs.eof()){ cerr << "Input file is empty!" << endl; return 1; } getline(ifs, buf); x1 = myAtof(buf); if (ifs.eof()){ cout << x1 << endl; return 0; } while (!ifs.eof()){ getline(ifs, buf); x2 = myAtof(buf); if(x1 < x2){ cout << x1; return 0; } x1 = x2; } cout << x1; return 0; } |