Разработать схему алгоритма для подсчёта количества отрицательных чисел среди целых чисел а,в,с.Протестировать алгоритм для всех возможных случаев (когда количество отрицательных чисел равно 0,1,2,3,)Помогите кто понимает))

Ответы:
Ксюша Толмачёва
01-02-2014 07:03

/* Язык 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 __DEBUGint main(int argc, const char *argv[]){  runTests();  return 0;}#elseint main(int argc, const char *argv[]){  solution();  return 0;}#endif

Также наши пользователи интересуются:

⭐⭐⭐⭐⭐ Лучший ответ на вопрос «Разработать схему алгоритма для подсчёта количества отрицательных чисел среди целых чисел а,в,с.Протестировать алгоритм для всех возможных случаев (когда количество отрицательных чисел равно 0,1,2,3,)Помогите кто понимает))» от пользователя Вадим Пилипенко в разделе Экономика. Задавайте вопросы и делитесь своими знаниями.

Открой этот вопрос на телефоне - включи камеру и наведи на QR-код!