Разработать схему алгоритма для подсчёта количества отрицательных чисел среди целых чисел а,в,с.протестировать алгоритм для всех возможных случаев (когда количество отрицательных чисел равно 0,1,2,3,) кто понимает))
187
266
Ответы на вопрос:
// pascalabc.net 3.0, сборка 1052 function kn(a,b,c: integer): byte; { функция возвращает количество отрицательных среди a,b,c} var n: byte; begin if a< 0 then n: =1 else n: =0; if b< 0 then inc(n); if c< 0 then inc(n); kn: =n end; function cond(c: char; v: integer): string; { функция формирует строку вида с> 0 или c< 0 в зависимости от знака v} begin if v< 0 then cond: =c+'< 0 ' else cond: =c+'> =0' end; procedure pv(a,b,c: integer; n: byte); { печать строки теста } var m: byte; begin m: =kn(a,b,c); write(cond('a',a),' ',cond('b',b),' ',cond('c',c),' '); write('отрицательных ',m); if n=m then write(' - тест прошел') else write(' - тестовое: ',n); writeln end; { тестовое решение } const t: array[1..8,1..3] of integer=((1,1,,1,-,-1,1), (1,-1,-,1,,1,-,-1,,-1,-1)); r: array[1..8] of byte=(0,1,1,2,1,2,2,3); var i: byte; begin for i: =1 to 8 do pv(t[i,1],t[i,2],t[i,3],r[i]) end. тестирование: a> =0 b> =0 c> =0 отрицательных 0 - тест прошел a> =0 b> =0 c< 0 отрицательных 1 - тест прошел a> =0 b< 0 c> =0 отрицательных 1 - тест прошел a> =0 b< 0 c< 0 отрицательных 2 - тест прошел a< 0 b> =0 c> =0 отрицательных 1 - тест прошел a< 0 b> =0 c< 0 отрицательных 2 - тест прошел a< 0 b< 0 c> =0 отрицательных 2 - тест прошел a< 0 b< 0 c< 0 отрицательных 3 - тест прошел
/* язык c++11. здесь решение вместе с юнит-тестами */ #include < iostream> #include < sstream> #include < string> int count_negatives(int a, int b, int c) { return (a< 0 ? 1: 0) + (b< 0 ? 1: 0) + (c< 0 ? 1: 0) } void solution(std: : istream & input = std: : cin, std: : ostream & output) { int a, b, c; input > > a > > b > > c; output < < count_negatives(a, b, c) < < std: : endl; } void checktest(std: : string input_data, std: : string correct_answer) { std: : istringstream input(input_data); std: : istringstream correct_answer_stream(correct_answer); std: : stringstream algorithm_answer_stream; int correct_value, algorithm_value; correct_answer_stream > > correct_value; solution(input, algorithm_answer_stream); algorithm_answer_stream > > algorithm_value; if (correct_value ! = algorithm_value) { std: : cerr < < "input: " < < input_data < < std: : endl; std: : cerr < < "correct: " < < correct_value < < std: : endl; std: : cerr < < "algorithm: " < < algorithm_value < < std: : endl; throw std: : runtime_error("test failed"); } } void runtests() { checktest("1 2 3", "0"); checktest("-1 2 3", "1"); checktest("1 -2 3", "1"); checktest("1 2 -3", "1"); checktest("-1 -2 3", "2"); checktest("-1 2 -3", "2"); checktest("1 -2 -3", "2"); checktest("-1 -2 -3", "3"); } #ifdef __debug int main(int argc, const char *argv[]) { runtests(); return 0; } #else int main(int argc, const char *argv[]) { solution(); return 0; } #endif
Текстовой редактор- это прикладное программное обеспечение для создание текстовых документов.
Популярно: Информатика
-
kolyaan32110.11.2020 11:53
-
artenyancz18.12.2022 22:56
-
srochno617.03.2020 14:43
-
ltandreeva201611.02.2021 23:59
-
Andrey2602200224.12.2020 19:16
-
pashnina37910.03.2023 22:28
-
стеффа1128.09.2020 01:20
-
Chernysh1428.06.2020 22:19
-
kirillmotoin13.12.2021 09:03
-
bryuho2001121.02.2021 05:59