Created
August 23, 2022 21:04
-
-
Save odhiambo123/26c27b18743b11840804072c98aae933 to your computer and use it in GitHub Desktop.
John picks p balls then Jack picks q balls from a bag of n balls. find who picks last and how many they have in hand.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void get_total_number_from_last_picker(int n, int p, int q) { | |
int total_P = 0; | |
int p_turns = 0; | |
int total_Q = 0; | |
int q_turns = 0; | |
for (n=n; n>0; n=n-p) { | |
n = n - p; | |
total_P +=p; | |
p_turns++; | |
for (n=n; n>0; n=n-q ) { | |
n = n - q; | |
total_Q += q; | |
q_turns++; | |
} | |
} | |
if (p_turns > q_turns) { | |
System.out.println("P went last and has " + total_P + " in hand"); | |
} else { | |
System.out.println("Q went last and has " + total_Q + " in hand"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment