public class Test { public static void main(String [] args) { Test t = new Test(); for(int i=1;i<21;i++) System.out.println("当台面上还有 "+i+" 个球时,可得的最高分是:"+ t.Max(i)); } public int Max(int count) { if(count>=21 || count<1) return 0; /*判断输入的数是否正确*/ else{ if(count>=7) return 8; /*当台面的球大于等于时,返回 8*/ else if(count>=2) return 13; else return 7; } } }
public int score(int ball) { int max = ball * 8 + 27;//2+3+4+5+6+7=27 return max; }
public class Test { public static void main(String [] args) { Test t = new Test(); for(int i=1;i<21;i++) System.out.println("当台面上还有"+i+"个球时,可得的最高分是:"+ t.Max(i)); } public int Max(int count) { if(count>=21 || count<1) return 0; /*判断输入的数是否合法*/ else{ if(count>=7) return 8; /*当台面的球大于等于7时,最多可得8分*/ else if(count>=2) return 13;/*否则,当台面的球大于等于2时,最多可得13分*/ else return 7;/*只剩1一个球时,只能是7了。*/ } } }
错了,刚才看还以为是剩下的红球的数目呢。 再来 public int score(int ball) { int max = 0; int black = 7; if(ball >= 7) { int max = ball * 8 + 27;//2+3+4+5+6+7=27 return max; } else { for(int i=0;i<ball;i++ ) { max = max + black; black--; } return max; } }这次应该没有问题了。
int max = ball * 8 + 27;//2+3+4+5+6+7=27 改成 max = ball * 8 + 27;//2+3+4+5+6+7=27 晕,多写了个int
经过测试的程序如下: public class CountSnokMaxScore { public int countMaxScore(int ballnum) { int max =0 ; //选手能得到的最高分数. int ball=ballnum;//剩下的台球数. if(ball>=6) //如果剩下的台球数多于6个,这时打进的都是红球, { //则能得到的最高分数为:打进的红球数* //每打进一个红球和一个色球能得到的最高分数 max = (21-ball) * 8 ; return max; } else //如果剩下的台球数少于6个,则进到球袋里的球有红球和其它色球, //按照斯若克的规则,只剩下色球的时候是由低分球向高分 //球打起的,则有: { max=15 * 8;//已打进所以红球能得到的最高分.
int ma=d得到VECTOR 的最大值
result=x1*ma+x2所有分值的和
int count_score(int balls) {
if (balls > 15+score_ball.length) { //超出球的数目`
return -1;
} int score = 0;
if (balls <= score_ball.length) { //没有1分球
for (int i=score_ball.length-balls, i<score_ball.length; i++) {
score += score_ball[i];
}
return score;
} score = balls - score_ball.length; //有多少个1分球就先计算多少分
score = score*score_ball[score_ball.length-1]; //以最乐观的计算最大分数(每个1分球打入后打进最高分值黑球) for(int i=0; i<score_ball.length; i++) { //最后依次打入计分球
score += score_ball[i];
}
return sore;
}//如果楼主的规则是没有红球时,打入高分球也计分的话,就在上面的基础上,在return之前再加上
for (int i=0; i<score_ball.length-2) { //以最乐观的考虑,没有1分红球时还有score_ball.length-2次机会打入最高分黑球
score += score_ball[i];
}
{
public static void main(String [] args)
{
Test t = new Test();
for(int i=1;i<21;i++)
System.out.println("当台面上还有 "+i+" 个球时,可得的最高分是:"+ t.Max(i));
}
public int Max(int count)
{
if(count>=21 || count<1)
return 0; /*判断输入的数是否正确*/
else{
if(count>=7)
return 8; /*当台面的球大于等于时,返回 8*/
else if(count>=2)
return 13;
else return 7;
}
}
}
{
int max = ball * 8 + 27;//2+3+4+5+6+7=27
return max;
}
{
public static void main(String [] args)
{
Test t = new Test();
for(int i=1;i<21;i++)
System.out.println("当台面上还有"+i+"个球时,可得的最高分是:"+ t.Max(i));
}
public int Max(int count)
{
if(count>=21 || count<1)
return 0; /*判断输入的数是否合法*/
else{
if(count>=7)
return 8; /*当台面的球大于等于7时,最多可得8分*/
else if(count>=2)
return 13;/*否则,当台面的球大于等于2时,最多可得13分*/
else return 7;/*只剩1一个球时,只能是7了。*/
}
}
}
再来
public int score(int ball)
{
int max = 0;
int black = 7;
if(ball >= 7)
{
int max = ball * 8 + 27;//2+3+4+5+6+7=27
return max;
}
else
{
for(int i=0;i<ball;i++ )
{
max = max + black;
black--;
}
return max;
}
}这次应该没有问题了。
改成 max = ball * 8 + 27;//2+3+4+5+6+7=27
晕,多写了个int
作算术分析:每打一个红球和一个色球能得到的最高分值是1+7=8分,全部红球打尽所得最高分值为:15*8=120分,再加上其它色球的总分值:120+2+3+4+5+6+7=147分.至于怎么样实现输入球数就能得到最高分值嘛,各位高手显下身手吧.
应该是:max = (ball-6) * 8 + 27;//2+3+4+5+6+7=27吧!
public class CountSnokMaxScore
{ public int countMaxScore(int ballnum)
{ int max =0 ; //选手能得到的最高分数.
int ball=ballnum;//剩下的台球数.
if(ball>=6) //如果剩下的台球数多于6个,这时打进的都是红球,
{ //则能得到的最高分数为:打进的红球数*
//每打进一个红球和一个色球能得到的最高分数
max = (21-ball) * 8 ;
return max;
}
else //如果剩下的台球数少于6个,则进到球袋里的球有红球和其它色球,
//按照斯若克的规则,只剩下色球的时候是由低分球向高分
//球打起的,则有:
{
max=15 * 8;//已打进所以红球能得到的最高分.
for(int i=2;i<=(7-ball);i++)//打进的色球能得到的最高分.
{
max=max+i;
}
return max;
}
} public static void main(String[] args)
{
CountSnokMaxScore testvar=new CountSnokMaxScore();
int ball=Integer.parseInt(args[0]);
int maxscore=testvar.countMaxScore(ball);
System.out.println("桌面上剩下"+ball+"个球时,选手的最高得分可达:"+maxscore+"分");
}
}