首页 > 专题系列 > Java解POJ > POJ 2272 Bullseye [解题报告] Java
2013
11-11

POJ 2272 Bullseye [解题报告] Java

Bullseye

问题描述 :

A simple dartboard consists of a flat, circular piece of cork with concentric rings drawn on it. Darts are thrown at the board by players in an attempt to hit the center of the dartboard (the Bullseye). The region between each pair of rings (or the center and the first ring) represents a certain point value. The closer the region is to the center of the dartboard, the more points the region is worth, as shown in the diagram below:



Ring radii are at 3″, 6″, 9″, 12″ and 15″ (the Bullseye has a diameter of 6″). A game of Simple Darts between two players is played as follows. The first player throws 3 darts at the board. A score is computed by adding up the point values of each region that a dart lands in. The darts are removed. The second player throws 3 darts at the board; the score for player two is computed the same way as it is for player one. The player with the higher score wins.

For this problem, you are to write a program that computes the scores for two players, and determine who, if anyone, wins the game. If a dart lands exactly on a ring (region boundary), the higher point value is awarded. Any dart outside the outer ring receives no points. For the purposes of this problem, you can assume that a dart has an infinitely fine point and can not land paritially on a ring; it is either on the ring or it is not on the ring. Standard double precision floating point operations will be should be used.

输入:

Input consists of 1 or more datasets. A dataset is a line with 12 double-precision values separated by spaces. Each pair of values represents the X and Y distances respectively of a dart from the center of the board in inches. (the center is located at X = 0, Y = 0. The range of values are: -20.0<=X, Y<=20.0. Player one's darts are represented by the first 3 pairs of values, and player two's by the last 3 pairs of values. Input is terminated by the first value of a dataset being -100.

输出:

For each dataset, print a line of the form:
SCORE: N to M, PLAYER P WINS. 

Or:

SCORE: N to M, TIE. 

N is player one’s score, and M is player two’s score. P is either 1 or 2 depending on which player wins. All values are non-negative integers.

Formula

Recall: r2 = x2 + y2 where r is the radius, and (x, y) are the coordinates of a point on the circle.

样例输入:

-9 0 0 -4.5 -2 2 9 0 0 4.5 2 -2
-19.0 19.0 0 0 0 0 3 3 6 6 12 12
-100 0 0 0 0 0 0 0 0 0 0 0

样例输出:

SCORE: 240 to 240, TIE.
SCORE: 200 to 140, PLAYER 1 WINS.

解题代码:

import java.util.*;
/**
 *
 * @author Leo
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static int score(double x,double y){
        double r=x*x+y*y;
        if(r<=9.) return 100;
        if(r<=36.) return 80;
        if(r<=81.) return 60;
        if(r<=144.) return 40;
        if(r<=225.) return 20;
        return 0;
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        double x,y;
        int sum1,sum2;
        while(true){
            sum1=0;sum2=0;
            x=sc.nextDouble();
            y=sc.nextDouble();
            if(x==-100) break;
            sum1+=score(x,y);
            for(int i=1;i<=2;i++){
                x=sc.nextDouble();
                y=sc.nextDouble();
                sum1+=score(x,y);
            }
            for(int i=1;i<=3;i++){
                x=sc.nextDouble();
                y=sc.nextDouble();
                sum2+=score(x,y);
            }
            if(sum1>sum2)
                System.out.println("SCORE: "+sum1+" to "+sum2+", PLAYER 1 WINS.");
            else if(sum1==sum2)
                System.out.println("SCORE: "+sum1+" to "+sum2+", TIE.");
            else
                System.out.println("SCORE: "+sum1+" to "+sum2+", PLAYER 2 WINS.");

        }
    }

}

  1. 学算法中的数据结构学到一定程度会乐此不疲的,比如其中的2-3树,类似的红黑树,我甚至可以自己写个逻辑文件系统结构来。

  2. 有限自动机在ACM中是必须掌握的算法,实际上在面试当中几乎不可能让你单独的去实现这个算法,如果有题目要用到有限自动机来降低时间复杂度,那么这种面试题应该属于很难的级别了。

  3. 这道题目虽然简单,但是小编做的很到位,应该会给很多人启发吧!对于面试当中不给开辟额外空间的问题不是绝对的,实际上至少是允许少数变量存在的。之前遇到相似的问题也是恍然大悟,今天看到小编这篇文章相见恨晚。