1

Write a program that determines all the different ways for a player to score a certain number of points in a basketball game. Three pointers are worth 3 points (duh), field goals are worth 2 points, and foul shots are worth 1 point each. Be sure to print the total number of combinations at the end.

Run:

How many points were scored? 8

2 three pointer(s), 1 field goal(s), 0 foul shot(s)

2 three pointer(s), 0 field goal(s), 2 foul shot(s)

1 three pointer(s), 2 field goal(s), 1 foul shot(s)

1 three pointer(s), 1 field goal(s), 3 foul shot(s)

1 three pointer(s), 0 field goal(s), 5 foul shot(s)

0 three pointer(s), 4 field goal(s), 0 foul shot(s)

0 three pointer(s), 3 field goal(s), 2 foul shot(s)

0 three pointer(s), 2 field goal(s), 4 foul shot(s)

0 three pointer(s), 1 field goal(s), 6 foul shot(s)

0 three pointer(s), 0 field goal(s), 8 foul shot(s)

There are 10 different ways to score 8 points

What I have so far:

import java.util.Scanner;
    public class MarchMadness 
    {
        public static void main(String[] args)
        {
            Scanner kbReader = new Scanner(System.in);
            System.out.println("How many points were scored?");
            int points = kbReader.nextInt();
            int ft = 1;
            int fg = 2;
            int tp = 3;

            if (points % tp == 0)
            {   
               System.out.println((points/tp) + " three pointers.");
            }


        }
}

I know how to make the program applicable to SPECIFIC situations, like 8 points, but I do not know how to make it accept any amount of points and get the correct output or print out the number of possible solutions. What should I do?

jpsizzls
  • 35
  • 1
  • 3
  • 1
    is this homework or something similar? if so, please tag appropriately. – katzenhut Feb 26 '16 at 19:07
  • You can use dynamic programming to solve this. Take a look at [this article](http://www.capacode.com/dynamic-programming/money-change/) which solves a similar problem. Maybe you'll get some ideas. Alternatively, [this SO question](http://stackoverflow.com/q/8031816/1828486) asks about dynamic programming as well. – Mage Xy Feb 26 '16 at 19:24
  • 1
    In my opinion, instead of starting with the code directly, you should work out the problem on pen and paper. See if you can figure out how you can make different combinations to reach the target. If you work on it for a while, you will definitely see a pattern. Then think about how you can come up with an algorithm to make it work for all possible scenarios. Then only start with writing code. If you dive in the code directly, it will only confuse you more. The above approach will help you in general with your problem solving ability. Good luck! – software_writer Feb 26 '16 at 19:29
  • 1
    @katzenhut there is no homework tag. see http://meta.stackexchange.com/questions/147100/the-homework-tag-is-now-officially-deprecated – kritzikratzi Feb 26 '16 at 20:40
  • @jpsizzls please mark an answer as correct – kritzikratzi Feb 27 '16 at 12:47
  • @kritzikratzi - I see. Thanks for the link. – katzenhut Feb 28 '16 at 23:48

1 Answers1

3

because this is homework the answer is at the very end, and i hope you don't need it.

READ ONLY A FEW LINES AT A TIME, TRY TO SEE HOW LITTLE YOU NEED TO READ BEFORE YOU FIGURE IT OUT YOURSELF


Assume you have a number of points, as an example let's make points=16.

1. How many three pointers?

How many three pointers can you make from points? Well, at maximum points/3.0=5.333. Let's round this down to 5. In the general case:

int maxThreePointers=points/3;

Integer arithmetic in java rounds down automatically. Now we KNOW that the number of three pointers must be in the range 0 .. maxThreePointers. Now we know a lot!

2. How many field goals?

Pick any number of three pointers from the above range, as an example let's do the case threePointers = 2. Think about the number of points left to be distributed. int pointsMissing = points - threePointers*3; in our case pointsMissing = 16 - 2*3 = 10.

Rinse and repeat the first idea: We now have 10 points, how many field goals can we have at maximum? int maxFieldGoals = pointsMissing/2;, that is between 0 and 5 in the example.

3.How many foul shots?

Pick any number of three pointers and field goals from the above ranges. The remaing points must be foul goals. It's done!

This can be coded in a set of nested loops:


spoiler wall


spoiler wall


spoiler wall


spoiler wall


spoiler wall


spoiler wall


spoiler wall


spoiler wall


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" );
kritzikratzi
  • 19,662
  • 1
  • 29
  • 40