Дописать код игры так, что, когда приближается враг, расстрелять его - C (СИ)
Формулировка задачи:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/**
* CodinGame planet is being attacked by slimy insectoid aliens.
* <---
* Hint:To protect the planet, you can implement the pseudo-code provided in the statement, below the player.
**/
int main()
{
// game loop
while (1) {
char enemy1[256]; // name of enemy 1
scanf("%s", enemy1);
int dist1; // distance to enemy 1
scanf("%d", &dist1);
char enemy2[256]; // name of enemy 2
scanf("%s", enemy2);
int dist2; // distance to enemy 2
scanf("%d", &dist2);
// Write an action using printf(). DON'T FORGET THE TRAILING \n
// To debug: fprintf(stderr, "Debug messages...\n");
printf("enemy\n"); // replace "enemy" with a correct ship name to shoot
}
}Решение задачи: «Дописать код игры так, что, когда приближается враг, расстрелять его»
textual
Листинг программы
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/**
* CodinGame planet is being attacked by slimy insectoid aliens.
* <---
* Hint:To protect the planet, you can implement the pseudo-code provided in the statement, below the player
**/
int main()
{
// game loop
while (1) {
char enemy1[256]; // name of enemy 1
scanf("%s", enemy1);
int dist1; // distance to enemy 1
scanf("%d", &dist1);
char enemy2[256]; // name of enemy 2
scanf("%s", enemy2);
int dist2; // distance to enemy 2
scanf("%d", &dist2);
// Write an action using printf(). DON'T FORGET THE TRAILING \n
// To debug: fprintf(stderr, "Debug messages...\n");
if (dist2 < dist1) {
printf("%s\n", enemy2);
}
else {
printf("%s\n", enemy1);
}
}
}
Объяснение кода листинга программы
- Включаем необходимые заголовочные файлы
- Получаем информацию об врагах: их имена и расстояние до них
- Сравниваем расстояния до врагов и выводим имя ближайшего врага
- Повторяем шаги 2-3 в цикле, пока игра не будет остановлена