一道簡(jiǎn)單的邏輯題
要求:斯諾克臺(tái)球共有21個(gè)球,其中有15個(gè)紅色一分球,另有其它顏色的2,3,4,5,6,7分球各一個(gè),規(guī)則是先打一個(gè)紅色球,然后可以打一個(gè)其他分值的球,如此反復(fù),如果臺(tái)面還有紅色球,打入的高分球計(jì)分,同時(shí)拿出來(lái)放回原位置,當(dāng)打入最后一個(gè)紅球后再去打其它色球時(shí),那個(gè)色球同樣要拿出來(lái)放回原位,接著按照分?jǐn)?shù)由低到高,分別將2到7分的各色球打盡.要求寫(xiě)一段簡(jiǎn)單的java函數(shù),實(shí)現(xiàn)輸入臺(tái)面上剩余球的數(shù)目,計(jì)算出可得的最高分。(不考慮罰分等特殊情況)
作算術(shù)分析:每打一個(gè)紅球和一個(gè)色球能得到的最高分值是1+7=8分,全部紅球打盡所得最高分值:15*8=120分,再加上其它色球的總分值:120+2+3+4+5+6+7=147分.至于怎么樣實(shí)現(xiàn)輸入球數(shù)就能得到最高分值嘛,
public class CountMaxScore{
public int CountMaxScore(int ballnum){//計(jì)算最高得分
int max = 0; //
int ball = ballnum; //ballnum 傳入的剩下的球數(shù).
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+"個(gè)球時(shí),選手的最高得分可達(dá):"+maxscore+"分");
}catch(Exception e){}
}
}
作算術(shù)分析:每打一個(gè)紅球和一個(gè)色球能得到的最高分值是1+7=8分,全部紅球打盡所得最高分值:15*8=120分,再加上其它色球的總分值:120+2+3+4+5+6+7=147分.至于怎么樣實(shí)現(xiàn)輸入球數(shù)就能得到最高分值嘛,
public class CountMaxScore{
public int CountMaxScore(int ballnum){//計(jì)算最高得分
int max = 0; //
int ball = ballnum; //ballnum 傳入的剩下的球數(shù).
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+"個(gè)球時(shí),選手的最高得分可達(dá):"+maxscore+"分");
}catch(Exception e){}
}
}
posted on 2006-03-09 00:44 原語(yǔ) 閱讀(535) 評(píng)論(0) 編輯 收藏