Разработать схему алгоритма для подсчёта количества отрицательных чисел среди целых чисел а,в,с.Протестировать алгоритм для всех возможных случаев (когда количество отрицательных чисел равно 0,1,2,3,)Помогите кто понимает))
Разработать схему алгоритма для подсчёта количества отрицательных чисел среди целых чисел а,в,с.Протестировать алгоритм для всех возможных случаев (когда количество отрицательных чисел равно 0,1,2,3,)
Помогите кто понимает))
Ответ(ы) на вопрос:
/* Язык C++11. Здесь решение вместе с юнит-тестами */
#include
#include
#include
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
// 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),(-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 - тест прошел
Не нашли ответ?
Похожие вопросы