Problem statement and C solution for NCCU CS Assignment 2 "Dr. Stone".
NCCU CS Assignment 2: Dr. Stone (C Solution)
Objective:h2
Practice how to write a simple program including input parsing, math calculating and conditional logic.
Description:h2
Dr. Stone is a two-player game played with two circles. Each circle has a center , a radius , and a score . Each player throws a stone once at coordinates . A player earns if their stone lies strictly inside circle (distance to center ). A single throw may score from both circles. The player with the higher score wins; if scores are equal, it is a draw.
Input Formath2
The input consists of four lines:
x1 y1 r1 s1x2 y2 r2 s2xA yAxB yBConstraints:h3
Output Formath2
Print two lines:
- A single word: Player 1, Player 2, or Draw
- Two integers: Player 1’s score and Player 2’s score
Samplesh2
- input 1
0 0 5 103 0 5 202 010 0- output 1
Player 130 0- input 2
0 0 5 10100 100 2 501 12 0- output 2
Draw10 10Hinth2
For sample 1:h3
Player 1’s throw at lies inside both circles (distance to , distance to ), so earns . Player 2’s throw is outside both circles and earns 0. Player 1 wins.
For sample 2:h3
Both players’ throws lie strictly inside (distances and ), outside , so each earns → tie (Draw).
Solutionh2
#include <stdio.h>#include <math.h>
/* Written by 115703023 in 2026.09.16 */
int main(void){ int x1, y1, r1, s1; int x2, y2, r2, s2; int xA, yA; int xB, yB; scanf("%d %d %d %d", &x1, &y1, &r1, &s1); scanf("%d %d %d %d", &x2, &y2, &r2, &s2); scanf("%d %d", &xA, &yA); scanf("%d %d", &xB, &yB);
double dis_1A, dis_1B, dis_2A, dis_2B;
dis_1A = sqrt((xA - x1)*(xA - x1) + (yA - y1)*(yA - y1)); dis_1B = sqrt((xB - x1)*(xB - x1) + (yB - y1)*(yB - y1)); dis_2A = sqrt((xA - x2)*(xA - x2) + (yA - y2)*(yA - y2)); dis_2B = sqrt((xB - x2)*(xB - x2) + (yB - y2)*(yB - y2));
int p1_s = 0; int p2_s = 0;
if (dis_1A < r1){ p1_s += s1; } if (dis_2A < r2){ p1_s += s2; } if (dis_1B < r1){ p2_s += s1; } if (dis_2B < r2){ p2_s += s2; }
if (p1_s > p2_s){ printf("Player 1\n"); printf("%d %d", p1_s, p2_s); } if (p1_s < p2_s){ printf("Player 2\n"); printf("%d %d", p1_s, p2_s); } if (p1_s == p2_s){ printf("Draw\n"); printf("%d %d", p1_s, p2_s); }
printf("%d", &p1_s); return 0;}Explanationh2
Use the distance formula between two points to calculate the distance from each player’s throw to the circle centers, determine if the distance is less than the radius to accumulate scores, and finally compare both scores to decide the winner.
Comments