Address Link: Pat B 1082. Shooting Competition (20)
Topic description:
The rules of the shooting competition given in this topic are very simple. Whoever hits the bullet hole closest to the bullseye is the champion; whoever is the farthest is the rookie. This question gives the plane coordinates (x, y) of a series of bullet holes, please write a program to find out the champion and the rookie. We assume the bullseye is at the origin (0,0).
Problem solving process:
This question is relatively simple, as long as you enter an ID and (x,y) every time you calculate the distance and update it.
program:
#include <stdio.h> #include <math.h> int main(int argc, char const *argv[]) { int N; scanf("%d", &N); int ID[N], Dis[N]; /* MAX and MAXID store the farthest distance from the bullseye and the player ID MIN and MINID store the closest distance to the bullseye and the player ID*/ int MAX = 0, MIN = 200, MAXID, MINID; for (int i = 0; i < N; i++) { int x, y; scanf("%d %d %d", &ID[i], &x, &y); Dis[i] = sqrt(x*x + y*y); if (MAX < Dis[i]) { /* Update max distance and player ID */ MAX = Dis[i]; MAXID = ID[i]; } if (MIN > Dis[i]) { /* Update shortest distance and player ID */ MIN = Dis [i]; MINID = ID[i]; } } /* formatted output */ printf("%04d %04d\n", MINID, MAXID); return 0; }