因为这是家庭作业答案在最后,我希望你不需要它。
只读了几行的时间,尽量看你怎么一点需要阅读之前你看着办吧YOURSELF
假设你有一个点的数量,作为一个例子,让我们做points=16。
1.多少个三分球?
您可以从points多少个三个指针?那么,最多points/3.0=5.333。让我们把这个下降到5。在一般情况下:
int maxThreePointers=points/3;
java中的整数运算自动舍入。现在我们知道三个指针的数量必须在0 .. maxThreePointers的范围内。现在我们知道很多!
2.有多少实地目标?
接任何的三个指针数从上述范围内,作为一个例子,让我们做的情况下threePointers = 2。考虑剩下要分发的点数。在我们的案例中为int pointsMissing = points - threePointers*3;pointsMissing = 16 - 2*3 = 10。
冲洗并重复第一个想法:我们现在有10分,我们可以有多少场目标? int maxFieldGoals = pointsMissing/2;,即在示例中的0和5之间。
3.犯规次数多少?
挑选任意来自上述范围的三个指针和字段目标的数量。保留点必须是恶意目标。完成!
这可以在一组嵌套的循环进行编码:
扰流壁
扰流壁
扰流壁
扰流壁
扰流壁
扰流壁
扰流板壁上
扰流板壁上
int points = 16; // read this from stdin.
int maxThreePointers = points/3;
int count = 0;
for(int threePointers = 0; threePointers <= maxThreePointers; threePointers++){
int remainingPoints = points - threePointers*3;
int maxFieldGoals = remainingPoints/2;
for(int fieldGoals = 0; fieldGoals <= maxFieldGoals; fieldGoals ++){
// last one is easy!
int foulShots = remainingPoints - fieldGoals*2;
count++;
System.out.println(threePointers + " three pointers, " + fieldGoals + " field goals and " + foulShots + " foul shots");
}
}
System.out.println("There are " + count + " ways to score " + points + " points");