一道簡單的邏輯題
要求:斯諾克臺球共有21個球,其中有15個紅色一分球,另有其它顏色的2,3,4,5,6,7分球各一個,規則是先打一個紅色球,然后可以打一個其他分值的球,如此反復,如果臺面還有紅色球,打入的高分球計分,同時拿出來放回原位置,當打入最后一個紅球后再去打其它色球時,那個色球同樣要拿出來放回原位,接著按照分數由低到高,分別將2到7分的各色球打盡.要求寫一段簡單的java函數,實現輸入臺面上剩余球的數目,計算出可得的最高分。(不考慮罰分等特殊情況)
作算術分析:每打一個紅球和一個色球能得到的最高分值是1+7=8分,全部紅球打盡所得最高分值:15*8=120分,再加上其它色球的總分值:120+2+3+4+5+6+7=147分.至于怎么樣實現輸入球數就能得到最高分值嘛,
public class CountMaxScore{
public int CountMaxScore(int ballnum){//計算最高得分
int max = 0; //
int ball = ballnum; //ballnum 傳入的剩下的球數.
if(ball <= 6){ //
max = (21 - ball) * 8;
return max;
}else{
max = 15 * 8;
if(int i = 2;i <= (7-ball);i++){
max = max + i;
}
return max;
}
}
static public void main(String[] args){
CountMaxScore msc = new CountMaxScore();
BufferReader in = new BufferReader(new InputStreamReader(System.in));
try{
String s = in.ReadLine();
int ball = Integer.parseInt(s);
maxscor = msc.CountMaxScore(ball);
System.out.println("桌面上剩下"+ball+"個球時,選手的最高得分可達:"+maxscore+"分");
}catch(Exception e){}
}
}
作算術分析:每打一個紅球和一個色球能得到的最高分值是1+7=8分,全部紅球打盡所得最高分值:15*8=120分,再加上其它色球的總分值:120+2+3+4+5+6+7=147分.至于怎么樣實現輸入球數就能得到最高分值嘛,
public class CountMaxScore{
public int CountMaxScore(int ballnum){//計算最高得分
int max = 0; //
int ball = ballnum; //ballnum 傳入的剩下的球數.
if(ball <= 6){ //
max = (21 - ball) * 8;
return max;
}else{
max = 15 * 8;
if(int i = 2;i <= (7-ball);i++){
max = max + i;
}
return max;
}
}
static public void main(String[] args){
CountMaxScore msc = new CountMaxScore();
BufferReader in = new BufferReader(new InputStreamReader(System.in));
try{
String s = in.ReadLine();
int ball = Integer.parseInt(s);
maxscor = msc.CountMaxScore(ball);
System.out.println("桌面上剩下"+ball+"個球時,選手的最高得分可達:"+maxscore+"分");
}catch(Exception e){}
}
}