保龄球计分算法

今天看到一个有趣的面试题-保龄球计分算法,由于题目是英文的,而且从来没有接触过保龄球,顿时不知所措,觉得这个算法很复杂,后来慢慢分析才明白,其实非常简单,看来冷静地分析才是王道啊.今天回来自己写了一个实现,虽然很值得完善,但先贴出来吧,今后再完善。

A game of tenpins bowling lasts ten frames, in each of which the bowler makes one or two attempts to knock down ten pins arranged in a triangle. If the bowler knocks down all ten pins on the first attempt (that’s called a “strike”), he scores ten pins plus the number of pins knocked down on his next two rolls. If the bowler knocks down all ten pins after two attempts (that’s called a “spare”), he scores ten pins plus the number of pins knocked down on his next roll. If the bowler fails to knock down all ten pins (that’s called an “open frame”), he scores the number of pins he knocked down. The scores accumulate through all ten frames. At the last frame, if necessary, the pins are reset for one or two additional rolls to count the final bonus. The sample scoresheet below shows how the calculations work:


For instance, the score in the second frame is 9, the sum of the two balls in the open frame. The score in the third frame is 15, which is 10 for the spare plus 5 on the next ball. The score in the ninth frame is 20, which is 10 for the strike plus 10 for the spare on the first two balls of the tenth frame. The score in the tenth frame is 16, which is 10 for the spare plus 6 for the extra ball, which is a bonus ball not really part of any frame (the two balls of the tenth frame have already been rolled).

我的实现如下:

public class Hello {

	private static int totalScore = 0;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Frame[] frames = new Frame[10];
		
		frames[0] = new Frame(1,4,0);
		frames[1] = new Frame(4,5,0);
		frames[2] = new Frame(6,4,0);
		frames[3] = new Frame(5,5,0);
		frames[4] = new Frame(10,0,0);
		frames[5] = new Frame(0,1,0);
		frames[6] = new Frame(7,3,0);
		frames[7] = new Frame(6,4,0);
		frames[8] = new Frame(10,0,0);
		frames[9] = new Frame(2,8,6);
		
		for(int i = 0; i < 10; i++){
			if(i != 9){
				if(frames[i].getFirstscore() == 10){
					totalScore += frames[i+1].getFirstscore()+frames[i+1].getSecondscore()+10;
				}
				else if(frames[i].getFirstscore()+frames[i].getSecondscore()==10){
					totalScore += frames[i+1].getFirstscore()+10;
				}else {
					totalScore += frames[i].getFirstscore()+frames[i].getSecondscore();
				}
			}
			else{
				totalScore += frames[i].getFirstscore()
						+frames[i].getSecondscore()
						+frames[i].getThiredscore();
			}	
		}
		
		System.out.println("total score is " + totalScore);
	}
}

class Frame {
	
	public Frame(int firstscore,int secondscore,int thiredscore){
		this.firstscore = firstscore;
		this.secondscore = secondscore;
		this.thiredscore = thiredscore;
	}
	
	private int firstscore;
	private int secondscore;
	private int thiredscore = 0;
	
	public int getFirstscore() {
		return firstscore;
	}
	public void setFirstscore(int firstscore) {
		this.firstscore = firstscore;
	}
	public int getSecondscore() {
		return secondscore;
	}
	public void setSecondscore(int secondscore) {
		this.secondscore = secondscore;
	}
	public int getThiredscore() {
		return thiredscore;
	}
	public void setThiredscore(int thiredscore) {
		this.thiredscore = thiredscore;
	}
}
输出结果如下:

total score is 133



你可能感兴趣的:(算法,面试题,保龄球,计分)