Problem statement and C solution for NCCU CS Assignment 2 "Dr. Stone".

NCCU CS Assignment 2: Dr. Stone (C Solution)

2 mins
435 words
Loading views

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 cic_i has a center (xi,yi)(x_i, y_i), a radius rir_i, and a score sis_i. Each player throws a stone once at coordinates (x,y)(x,y). A player earns sis_i if their stone lies strictly inside circle cic_i (distance to center << rir_i). 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 s1
x2 y2 r2 s2
xA yA
xB yB

Constraints:h3

  • 1≤x,y,r,s≤10001≤x,y,r,s≤1000

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 10
3 0 5 20
2 0
10 0
  • output 1
Player 1
30 0
  • input 2
0 0 5 10
100 100 2 50
1 1
2 0
  • output 2
Draw
10 10

Hinth2

For sample 1:h3

Player 1’s throw at (2,0)(2,0) lies inside both circles (distance 2<52<5 to c1c_1, distance 1<51<5 to c2c_2), so earns 10+20=3010+20=30. Player 2’s throw is outside both circles and earns 0. Player 1 wins.

For sample 2:h3

Both players’ throws lie strictly inside c1c_1 (distances √2<5√2<5 and 2<52<5), outside c2c_2, so each earns 1010 → 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