The task is to simulate a wheel of fortune, which you are allowed to turn ten times.
You can spin as many times as you like, but as soon as the 0 comes, all points are gone. The program should stop the round as soon as a score over 10 is reached or a 0 comes. The results should be added at the end.
We are now at the point where the points are added and fields are fixed, but we can't think of anything to do with stopping or adding the results.
Does anyone have an idea?
Thanks in advance!
import java.util.Map;
import java.util.LinkedHashMap;
public class RandomBeispielzwei {
private static final Map<Double, Integer> GRENZEN = new LinkedHashMap<Double, Integer>();
static {
GRENZEN.put(0.1, 1);
GRENZEN.put(0.2, 2);
GRENZEN.put(0.3, 3);
GRENZEN.put(0.4, 1);
GRENZEN.put(0.5, 2);
GRENZEN.put(0.6, 3);
GRENZEN.put(0.7, 1);
GRENZEN.put(0.8, 2);
GRENZEN.put(0.9, 3);
GRENZEN.put(1.0, 0);
}
private Integer naechsteZufallzahl() {
double random = Math.random();
for (Map.Entry<Double, Integer> entry : GRENZEN.entrySet()) {
if (random <= entry.getKey().doubleValue()) {
return entry.getValue();
}
}
throw new UnsupportedOperationException("Fuer die Zufallszahl wurde kein passender Wert in der Map gefunden");
}
public static void main(String[] args) {
int anzahl1 = 0;
int anzahl2 = 0;
int anzahl3 = 0;
int anzahl0 = 0;
RandomBeispielzwei b = new RandomBeispielzwei();
for (int i = 0; i < 10; i++) {
Integer z = b.naechsteZufallzahl();
if (z.intValue() == 1) {
anzahl1++;
} else if (z.intValue() == 2) {
anzahl2++;
} else if (z.intValue() == 3) {
anzahl3++;
} else {
anzahl0++;
}
}
int ges1 = anzahl1 * 1;
int ges2 = anzahl1 * 2;
int ges3 = anzahl1 * 3;
System.out.println("1: " + anzahl1);
System.out.println("Punktzahl 1: " + ges1);
System.out.println("2: " + anzahl2);
System.out.println("Punktzahl 2: " + ges2);
System.out.println("3: " + anzahl3);
System.out.println("Punktzahl 3: " + ges3);
System.out.println("0: " + anzahl0);
System.out.println("Gesamtzahl: " + (anzahl1 + anzahl2 + anzahl3 + anzahl0));
System.out.println("Gesamtpunktzahl: " + (ges1 + ges2 + ges3));
}
}
For exiting the for-loop (and any other loop), you can use the "break" statement, which simply ends the loop (similar to how "return" will exit a method). In order to be able to stop once the total score reaches ten, you of course need to keep track of the total score. To do this, the easiest way would be to introduce aa integer variable (e.g. "gesamtpunktzahl"), to which you add the amount of points scored in each turn. In all, it would look something like this:
import java.util.Map;
import java.util.LinkedHashMap;
public class RandomBeispielZwei {
private static final Map<Double, Integer> GRENZEN = new LinkedHashMap<Double, Integer>();
static {
GRENZEN.put(0.1, 1);
GRENZEN.put(0.2, 2);
GRENZEN.put(0.3, 3);
GRENZEN.put(0.4, 1);
GRENZEN.put(0.5, 2);
GRENZEN.put(0.6, 3);
GRENZEN.put(0.7, 1);
GRENZEN.put(0.8, 2);
GRENZEN.put(0.9, 3);
GRENZEN.put(1.0, 0);
}
private Integer naechsteZufallzahl() {
double random = Math.random();
for (Map.Entry<Double, Integer> entry : GRENZEN.entrySet()) {
if (random <= entry.getKey().doubleValue()) {
return entry.getValue();
}
}
throw new UnsupportedOperationException("Fuer die Zufallszahl wurde kein passender Wert in der Map gefunden");
}
public static void main(String[] args) {
int anzahl1 = 0;
int anzahl2 = 0;
int anzahl3 = 0;
int anzahl0 = 0;
int gesamtpunktzahl = 0; // this will store what the total score is so far
RandomBeispielzwei b = new RandomBeispielzwei();
for (int i = 0; i < 10000; i++) {
Integer z = b.naechsteZufallzahl();
if (z.intValue() == 1) {
anzahl1++;
gesamtpunktzahl++; // a 1 was scored, so we increase the total score by 1
} else if (z.intValue() == 2) {
anzahl2++;
gesamtpunktzahl += 2; // same with a 2
} else if (z.intValue() == 3) {
anzahl3++;
gesamtpunktzahl += 3; // same with a 3
} else {
anzahl0++;
break; // a 0 was rolled, so we end the game (by exiting the for-loop)
}
if (gesamtpunktzahl >= 10) break; // at least 10 points were scored so far, so we exit the for-loop
}
int ges1 = anzahl1 * 1;
int ges2 = anzahl1 * 2;
int ges3 = anzahl1 * 3;
System.out.println("1: " + anzahl1);
System.out.println("Punktzahl 1: " + ges1);
System.out.println("2: " + anzahl2);
System.out.println("Punktzahl 2: " + ges2);
System.out.println("3: " + anzahl3);
System.out.println("Punktzahl 3: " + ges3);
System.out.println("0: " + anzahl0);
System.out.println("Gesamtzahl: " + (anzahl1 + anzahl2 + anzahl3 + anzahl0));
System.out.println("Gesamtpunktzahl: " + gesamtpunktzahl); // since we calculated it anyway, we might as well just use it here
}
}
I suggest using the loop do ... while for the counting and printing.
And please check you code - does you need count the same variable anzahl1 for each ges?:
int ges1 = anzahl1 * 1;
int ges2 = anzahl1 * 2;
int ges3 = anzahl1 * 3;
if not - then replace ges[i] = anzahles[1] * (i+1); to ges[i] = anzahles[i+1] * (i+1);
public static void main(String[] args) {
int[] anzahles = new int[4];
int gesamtpunktzahl = 0;
Integer z = 0;
RandomBeispielzwei b = new RandomBeispielzwei();
do {
z = b.naechsteZufallzahl();
anzahles[z]++;
gesamtpunktzahl += z;
} while (!(z == 0 || gesamtpunktzahl >= 10));
int ges[] = new int[3];
for (int i = 0; i < 3; i++) {
ges[i] = anzahles[1] * (i+1);
System.out.println((i+1) + ": " + anzahles[i+1]);
System.out.println("Punktzahl " + (i+1) + ": " + ges[i]);
}
System.out.println("0: " + anzahles[0]);
System.out.println("Gesamtzahl: " + Arrays.stream(anzahles).sum());
System.out.println("Gesamtpunktzahl: " + Arrays.stream(ges).sum());
}
Related
I use Java implemented Held-KarpTSP algorithm algo to solve a 25 cities TSP problem.
The program passes with 4 cities.
When it runs with 25 cities it won't stop for several hours. I use jVisualVM to see what's the hotspot, after some optimization now it shows
98% of time is in real computing instead in Map.contains or Map.get.
So I'd like to have your advice, and here is the code:
private void solve() throws Exception {
long beginTime = System.currentTimeMillis();
int counter = 0;
List<BitSetEndPointID> previousCosts;
List<BitSetEndPointID> currentCosts;
//maximum number of elements is c(n,[n/2])
//To calculate m-set's costs just need to keep (m-1)set's costs
List<BitSetEndPointID> lastKeys = new ArrayList<BitSetEndPointID>();
int m;
if (totalNodes < 10) {
//for test data, generate them on the fly
SetUtil3.generateMSet(totalNodes);
}
//m=1
BitSet beginSet = new BitSet();
beginSet.set(0);
previousCosts = new ArrayList<BitSetEndPointID>(1);
BitSetEndPointID beginner = new BitSetEndPointID(beginSet, 0);
beginner.setCost(0f);
previousCosts.add(beginner);
//for m=2 to totalNodes
for (m = 2; m <= totalNodes; m++) {// sum(m=2..n 's C(n,m)*(m-1)(m-1)) ==> O(n^2 * 2^n)
//pick m elements from total nodes, the element id is the index of nodeCoordinates
// the first node is always present
BitSet[] msets;
if (totalNodes < 10) {
msets = SetUtil3.msets[m - 1];
} else {
//for real data set, will read from serialized file
msets = SetUtil3.getMsets(totalNodes, m-1);
}
currentCosts = new ArrayList<BitSetEndPointID>(msets.length);
//System.out.println(m + " sets' size: " + msets.size());
for (BitSet mset : msets) { //C(n,m) mset
int[] candidates = allSetBits(mset, m);
//mset is a BitSet which makes sure begin point 0 comes first
//so end point candidate begins with 1. candidate[0] is always begin point 0
for (int i = 1; i < candidates.length; i++) { // m-1 bits are set
//set the new last point as j, j must not be the same as begin point 0
int j = candidates[i];
//middleNodes = mset -{j}
BitSet middleNodes = (BitSet) mset.clone();
middleNodes.clear(j);
//loop through all possible points which are second to the last
//and get min(A[S-{j},k] + k->j), k!=j
float min = Float.MAX_VALUE;
int k;
for (int ki = 0; ki < candidates.length; ki++) {// m-1 calculation
k = candidates[ki];
if (k == j) continue;
float middleCost = 0;
BitSetEndPointID key = new BitSetEndPointID(middleNodes, k);
int index = previousCosts.indexOf(key);
if (index != -1) {
//System.out.println("get value from map in m " + m + " y key " + middleNodes);
middleCost = previousCosts.get(index).getCost();
} else if (k == 0 && !middleNodes.equals(beginSet)) {
continue;
} else {
System.out.println("middleCost not found!");
continue;
// System.exit(-1);
}
float lastCost = distances[k][j];
float cost = middleCost + lastCost;
if (cost < min) {
min = cost;
}
counter++;
if (counter % 500000 == 0) {
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException iex) {
System.out.println("Who dares interrupt my precious sleep?!");
}
}
}
//set the costs for chosen mset and last point j
BitSetEndPointID key = new BitSetEndPointID(mset, j);
key.setCost(min);
currentCosts.add(key);
// System.out.println("===========================================>mset " + mset + " and end at " +
// j + " 's min cost: " + min);
// if (m == totalNodes) {
// lastKeys.add(key);
// }
}
}
previousCosts = currentCosts;
System.out.println("...");
}
calcLastStop(lastKeys, previousCosts);
System.out.println(" cost " + (System.currentTimeMillis() - beginTime) / 60000 + " minutes.");
}
private void calcLastStop(List<BitSetEndPointID> lastKeys, List<BitSetEndPointID> costs) {
//last step, calculate the min(A[S={1..n},k] +k->1)
float finalMinimum = Float.MAX_VALUE;
for (BitSetEndPointID key : costs) {
float middleCost = key.getCost();
Integer endPoint = key.lastPointID;
float lastCost = distances[endPoint][0];
float cost = middleCost + lastCost;
if (cost < finalMinimum) {
finalMinimum = cost;
}
}
System.out.println("final result: " + finalMinimum);
}
You can speed up your code by using arrays of primitives (it's likely to have to better memory layout than a list of objects) and operating on bitmasks directly (without bitsets or other objects). Here is some code (it generates a random graph but you can easily change it so that it reads your graph):
import java.io.*;
import java.util.*;
class Main {
final static float INF = 1e10f;
public static void main(String[] args) {
final int n = 25;
float[][] dist = new float[n][n];
Random random = new Random();
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
dist[i][j] = dist[j][i] = random.nextFloat();
float[][] dp = new float[n][1 << n];
for (int i = 0; i < dp.length; i++)
Arrays.fill(dp[i], INF);
dp[0][1] = 0.0f;
for (int mask = 1; mask < (1 << n); mask++) {
for (int lastNode = 0; lastNode < n; lastNode++) {
if ((mask & (1 << lastNode)) == 0)
continue;
for (int nextNode = 0; nextNode < n; nextNode++) {
if ((mask & (1 << nextNode)) != 0)
continue;
dp[nextNode][mask | (1 << nextNode)] = Math.min(
dp[nextNode][mask | (1 << nextNode)],
dp[lastNode][mask] + dist[lastNode][nextNode]);
}
}
}
double res = INF;
for (int lastNode = 0; lastNode < n; lastNode++)
res = Math.min(res, dist[lastNode][0] + dp[lastNode][(1 << n) - 1]);
System.out.println(res);
}
}
It takes only a couple of minutes to complete on my computer:
time java Main
...
real 2m5.546s
user 2m2.264s
sys 0m1.572s
So I am building the petals on a rose game and the dice defined as dice1, dice 2 etc. i then run a loop to decide for each dice what its value is and add that to the game value. What i want is for every sequence of the loop for it to switch which variable it is looking at. So first run through look at dice1. and then second look at dice2.
public class Rosegame
{
private int dice1;
private int dice2;
private int dice3;
private int dice4;
private int dice5;
private int gameValue;
public void rollDice()
{
dice1 = (int) ((6-1+1) * Math.random()) + 1;
dice2 = (int) ((6-1+1) * Math.random()) + 1;
dice3 = (int) ((6-1+1) * Math.random()) + 1;
dice4 = (int) ((6-1+1) * Math.random()) + 1;
dice5 = (int) ((6-1+1) * Math.random()) + 1;
}
public void printValues()
{
System.out.println("Dice 1 is:" + dice1);
System.out.println("Dice 2 is:" + dice2);
System.out.println("Dice 3 is:" + dice3);
System.out.println("Dice 4 is:" + dice4);
System.out.println("Dice 5 is:" + dice5);
}
public int calculatePetalsOnRose()
{
gameValue = 0;
for(int i = 1; i <=5; i++)
{
if (dice1 == 5)
{
gameValue = gameValue + 4;
}
else if (dice1 == 3)
{
gameValue = gameValue + 2;
}
else
{
gameValue = gameValue;
}
}
return gameValue;
}
}
this is my current code and what I need is inside the if statement where it has dice1, i want that to be able to change everytime it loops. Their is also a driver that runs the methods and allows input but so far that is working. Thank you so much in advance
Edit:
switching the variables to an array i now have which gives me an error saying java.lang.NullPointerException. It happens when the rollDice function is being called.
public class PetalsGame
{
private int[] anArrayDice;
private int gameValue;
public void rollDice()
{
anArrayDice[0] = (int) ((6-1+1) * Math.random()) + 1;
anArrayDice[1] = (int) ((6-1+1) * Math.random()) + 1;
anArrayDice[2] = (int) ((6-1+1) * Math.random()) + 1;
anArrayDice[3] = (int) ((6-1+1) * Math.random()) + 1;
anArrayDice[4] = (int) ((6-1+1) * Math.random()) + 1;
}
public void printDice()
{
System.out.println("Dice 1 is:" + anArrayDice[0]);
System.out.println("Dice 2 is:" + anArrayDice[1]);
System.out.println("Dice 3 is:" + anArrayDice[2]);
System.out.println("Dice 4 is:" + anArrayDice[3]);
System.out.println("Dice 5 is:" + anArrayDice[4]);
}
public int calculateAllPetals()
{
gameValue = 0;
for(int i = 0; i <=4; i++)
{
if (anArrayDice[i] == 5)
{
gameValue = gameValue + 4;
}
else if (anArrayDice[i] == 3)
{
gameValue = gameValue + 2;
}
else
{
gameValue = gameValue;
}
}
return gameValue;
}
}
I was able to fix it by adding anArrayDice = new int[5]; inside of the rollDice method. Thank you for the help.
You only seem to roll one die (at least that is the only value you seem to be interested in), so I would start by extracting a method to roll one die. I would also prefer, for readability, Random.nextInt(int). Something like,
private static final Random rand = new Random();
private static int rollDie(int sides) {
return rand.nextInt(sides) + 1;
}
Then you can roll and calculate your rose with that method as needed. Also, you can use x += y; instead of x = x + y;. Something like,
public int calculatePetalsOnRose() {
int gameValue = 0;
for (int i = 1; i <= 5; i++) {
int dice = rollDie(6);
System.out.printf("Dice %d is: %d%n", i, dice);
if (dice == 5) {
gameValue += 4;
} else if (dice == 3) {
gameValue += 2;
}
}
return gameValue;
}
I think it would be alot easier to do this if you use an array to store your dice. Also it saves you from having to repeat the same lines of code over and over again.
public int [] diceArray;
private int gameValue;
public void rollDice()
{
for(int i =0; i < diceArray.length; i++)
{
diceArray[i] = 6 *((int)Math.random()+ 1);
}
}
public void printValues()
{
for(int i=0; i < diceArray.length; i++)
{
System.out.println("Dice" + (i+1) +"is:" + diceArray[i]);
}
}
public int calculatePetalsOnRose()
{
gameValue = 0;
for(int i = 0; i < diceArray.length; i++)
{
if(diceArray[i] == 5)
{
gameValue += 4;
}
else if (diceArray[i] == 3)
{
gameValue +=2;
}
}
return gameValue;
}
I want to take the array of random values I've generated and print the aforementioned array with parentheses outside the longest run of the same number.
For example, if the array was [0,1,1,1,2,4,7,4] I'd like to receive 0(111)2474 as an output.
This is my code thus far.
import java.util.Random;
import java.util.Arrays;
/**
* Write a description of class ArrayRunner1 here.
*
* #author Ibrahim Khan
* #version (a version number or a date)
*/
public class ArrayRunner1 {
/**
* This method will generate my random numbers for my array.
* #param min minimum random value wanted
* #param max maximum random value wanted
* #return randomNum a random number between 1 and 6 inclusive
*/
public static int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
public static void main(String[] args) {
System.out.println("\f");
//Part 1 - Generate a random array of length 40 with random 1-6 inclusive
int[] array1 = new int[40];
for (int i = 0; i < array1.length; i++) {
array1[i] = randInt(1, 6);
}
System.out.println(Arrays.toString(array1));
//Counts and RETURN: reports how many times each number is present
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int counter5 = 0;
int counter6 = 0;
for (int i = 0; i < array1.length; i++) {
if (array1[i] == 1) {
counter1++;
}
if (array1[i] == 2) {
counter2++;
}
if (array1[i] == 3) {
counter3++;
}
if (array1[i] == 4) {
counter4++;
}
if (array1[i] == 5) {
counter5++;
}
if (array1[i] == 6) {
counter6++;
}
}
System.out.println("There are " + counter1 + " ones.");
System.out.println("There are " + counter2 + " twos.");
System.out.println("There are " + counter3 + " threes.");
System.out.println("There are " + counter4 + " fours.");
System.out.println("There are " + counter5 + " fives.");
System.out.println("There are " + counter6 + " sixes.");
//Counts the longest run of the same number. A run continues only when consecutive numbers have the same value.
//RETURN: The repeated number and the length of the run is then printed
int counter = 1;
int runMax = 1;
int runMin = 0;
int variableNum = 0;
int startCounter = 0;
int endCounter = 0;
for (int i = 0; i < array1.length - 1; i++) {
if (array1[i] == array1[i + 1]) {
counter++;
if (counter >= runMax {
runMax = counter;
runMin = i - counter + 1;
variableNum = array1[i];
startCounter = i - counter + 2;
endCounter = i + counter - 1;
}
} else {
counter = 1;
}
}
System.out.println("The longest run is " + runMax + " times and the number is " + variableNum + ". ");
System.out.println("The run starts at " + startCounter + " and ends at " + endCounter);
//Prints the array with parentheses outside the longest run, if there is more than one max run, use the last one.
}
}
try this code:
import java.util.Arrays;
import java.util.Random;
public class Snippet {
/**
* This method will generate my random numbers for my array.
*
* #param min
* minimum random value wanted
* #param max
* maximum random value wanted
* #return randomNum a random number between 1 and 6 inclusive
*/
public static int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
public static void main(String[] args) {
System.out.println("\f");
// Part 1 - Generate a random array of length 40 with random 1-6
// inclusive
int[] array1 = new int[40];
for (int i = 0; i < array1.length; i++) {
array1[i] = randInt(1, 6);
}
System.out.println(Arrays.toString(array1));
// Counts and RETURN: reports how many times each number is present
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int counter5 = 0;
int counter6 = 0;
for (int i = 0; i < array1.length; i++) {
if (array1[i] == 1) {
counter1++;
}
if (array1[i] == 2) {
counter2++;
}
if (array1[i] == 3) {
counter3++;
}
if (array1[i] == 4) {
counter4++;
}
if (array1[i] == 5) {
counter5++;
}
if (array1[i] == 6) {
counter6++;
}
}
System.out.println("There are " + counter1 + " ones.");
System.out.println("There are " + counter2 + " twos.");
System.out.println("There are " + counter3 + " threes.");
System.out.println("There are " + counter4 + " fours.");
System.out.println("There are " + counter5 + " fives.");
System.out.println("There are " + counter6 + " sixes.");
// Counts the longest run of the same number. A run continues only when
// consecutive numbers have the same value.
// RETURN: The repeated number and the length of the run is then printed
int counter = 1;
int runMax = 0;
int runMin = 0;
int variableNum = 0;
int startCounter = 0;
int endCounter = 0;
for (int i = 0; i < array1.length - 1; i++) {
if (array1[i] == array1[i + 1]) {
counter++;
if (counter >= runMax) {
runMax = counter;
startCounter = i - counter +2;
// runMin = i-counter+1;
variableNum = array1[i];
endCounter = i+1;
}
} else {
counter = 1;
}
}
System.out.println("The longest run is " + runMax
+ " times and the number is " + variableNum + ". ");
System.out.println("The run starts at " + startCounter
+ " and ends at " + endCounter);
for (int i = 0; i < array1.length; i++) {
if (i==startCounter) {
System.out.print("(");
}
System.out.print(array1[i]);
if (i==endCounter) {
System.out.print(")");
}
}
System.out.println();
// Prints the array with parentheses outside the longest run, if there
// is more than one max run, use the last one.
}
}
Okay. I think I have this. The first answer was close, but if you run the program a few times, you discover issues. There is a logic error somewhere in your above code, but I have a work around. I think it is how you get the endCounter. It seems to count odd. But I got the program to work as far as I can tell. Try this out. I have run it several times and it seems consistent.
import java.util.Random;
import java.util.Arrays;
/**
* Write a description of class ArrayRunner1 here.
*
* #author Ibrahim Khan
* #version (a version number or a date)
*/
public class ArrayRunner1 {
/**
* This method will generate my random numbers for my array.
* #param min minimum random value wanted
* #param max maximum random value wanted
* #return randomNum a random number between 1 and 6 inclusive
*/
public static int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
public static void main(String[] args) {
System.out.println("\f");
//Part 1 - Generate a random array of length 40 with random 1-6 inclusive
int[] array1 = new int[40];
for (int i = 0; i < array1.length; i++) {
array1[i] = randInt(1, 6);
}
System.out.println(Arrays.toString(array1));
//Counts and RETURN: reports how many times each number is present
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int counter5 = 0;
int counter6 = 0;
for (int i = 0; i < array1.length; i++) {
if (array1[i] == 1) {
counter1++;
}
if (array1[i] == 2) {
counter2++;
}
if (array1[i] == 3) {
counter3++;
}
if (array1[i] == 4) {
counter4++;
}
if (array1[i] == 5) {
counter5++;
}
if (array1[i] == 6) {
counter6++;
}
}
System.out.println("There are " + counter1 + " ones.");
System.out.println("There are " + counter2 + " twos.");
System.out.println("There are " + counter3 + " threes.");
System.out.println("There are " + counter4 + " fours.");
System.out.println("There are " + counter5 + " fives.");
System.out.println("There are " + counter6 + " sixes.");
//Counts the longest run of the same number. A run continues only when consecutive numbers have the same value.
//RETURN: The repeated number and the length of the run is then printed
int counter = 1;
int runMax = 1;
int runMin = 0;
int variableNum = 0;
int startCounter = 0;
int endCounter = 0;
for (int i = 0; i < array1.length - 1; i++) {
if (array1[i] == array1[i + 1]) {
counter++;
if (counter >= runMax ){
runMax = counter;
runMin = i - counter ;// was plus one I cahnged this.
variableNum = array1[i];
startCounter = i - counter + 2;
endCounter = i + counter -1;
}
} else {
counter = 1;
}
}
System.out.println("The longest run is " + runMax + " times and the number is " + variableNum + ". ");
System.out.println("The run starts at " + startCounter + " and ends at " + endCounter);
//Prints the array with parentheses outside the longest run, if there is more than one max run, use the last one.
String output = "";// added this
for(int x = 0; x < array1.length; x++)
{
if( x == startCounter)
{
output += "("+array1[x];
}
else if( x == startCounter + runMax )
{
else if( x == startCounter + runMax )
{
if(x == array1.length-1)
{
output += ")";
}
else
{
output += ")"+array1[x];
}
}
else
{
output += array1[x];
}
}
System.out.print("\n"+output);
}
}
Here's a shorter, more generic solution. This method takes any array of ints and prints parenthesis around the longest run of numbers. If there are two runs of the same lengths it prints it around the first one.
public String makeString(int[] ints) {
if (ints.length == 0) return ""; // Quit early if there's nothing to do.
// Initialize variables.
int lastNumber = ints[0];
// We keep track of the all time best run. Defaults to first int found.
int bestStart = 0;
int bestRun = 1;
// ... as well as the current run.
int currentStart = 0;
int currentRun = 1;
String s = ""+ints[0];
// Starting from the second int, we check if the current run is continuing.
for (int i = 1; i < ints.length; i++) {
int current = ints[i];
// If the current run continues, we update currentStart/currentRun, else we reset it.
if (current == lastNumber) {
currentRun++;
} else {
currentStart = i;
currentRun = 1;
}
// Now we check if the currentRun is better than the best.
// If so, we update bestStart/bestRun.
if (currentRun > bestRun) {
bestStart = currentStart;
bestRun = currentRun;
}
lastNumber = current;
s += current;
}
// Now that we've found it, we insert parenthesis aaaaaaand we're done!
return s.substring(0, bestStart)
+"("+s.substring(bestStart, bestStart+bestRun)+")"
+s.substring(bestStart+bestRun);
}
my first question here. My assignment is to generate a random number between 1 and 10, 20 times. After that I'm supposed to somehow write the numbers into two separated strings for Odd and Even numbers. We're also supposed to count how many total numbers we had for each group of numbers. This is what I have so far, I need help on how to display the separated numbers. I would like to use JOptionPane as well please, but any help is appreciated!
package lab5arudy;
import javax.swing.JOptionPane;
public class Lab5ARudy
{
public static void main(String[] args)
{
int randomNumber = 0;
int evenNumbers = 0;
int oddNumbers = 0;
String evenNums;
String oddNums;
for (int i = 0; i < 20; i++)
{
randomNumber = 1 + (int) (Math.random() * 10);
if (randomNumber % 2 ==0)
{
evenNumbers++;
}
else
oddNumbers++;
}
JOptionPane.showMessageDialog(null,"Even Numbers: " + evenNumbers);
JOptionPane.showMessageDialog(null,"Even Numbers: " + oddNumbers );
}
}
It sounds as though you just need to add all your random numbers to lists so that you can print them afterwards. Something like the following:
Random rand = new Random();
List<Integer> odds = new ArrayList<>();
List<Integer> evens = new ArrayList<>();
for (int i = 0; i < 20; i++) {
int next = rand.nextInt(10) + 1;
if (next % 2 == 0)
evens.add(next);
else
odds.add(next);
}
showMessageDialog("Even :" + evens + " (" + evens.size() + ")");
showMessageDialog("Odds :" + odds + " (" + odds.size() + ")");
You can do it like this.
public static void main(String[] args) {
int randomNumber;
int evenNumbers = 0;
int oddNumbers = 0;
String evenNums = "";//initialize empty strings
String oddNums = "";//initialize empty strings
for (int i = 0; i < 20; i++) {
randomNumber = 1 + (int) (Math.random() * 10);
if (randomNumber % 2 == 0) {
evenNumbers++;
if (evenNums.equals("")) {//if the evenNums is empty, append the number
evenNums = evenNums + randomNumber;
} else {
evenNums = evenNums + "," + randomNumber;//if the evenNums is not empty, append the number with a , in between
}
} else {
oddNumbers++;
if (oddNums.equals("")) {
oddNums = oddNums + randomNumber;//if the oddNums is empty, append the number
} else {
oddNums = oddNums + "," + randomNumber;//if the oddNums is not empty, append the number with a , in between
}
}
}
JOptionPane.showMessageDialog(null, "Even Numbers: " + evenNumbers+"\n"+evenNums);//Show the even numbers
JOptionPane.showMessageDialog(null, "Odd Numbers: " + oddNumbers+"\n"+oddNums);//Show the odd numbers
}
I have n bags of candies such that no two bags have the same number of candies inside (i.e. it's a set A[] = {a0,a1,a2,...,ai,...,aj} where ai != aj).
I know how many candies is in each bag and the total number M of candies I have.
I need to divide the bags among three children so that the candies are distributed as fairly as possible (i.e. each child gets as close to M/3 as possible).
Needless to say, I may not tear into the bags to even out the counts -- then the question would be trivial.
Does anyone have any thoughts how to solve this -- preferably in Java?
EDIT:
the interviewer wanted me to use a 2-D array to solve the problem: the first kid gets x, the second kid y, the third gets the rest: S[x][y].
This after I tried following:
1] sort array n lg n
2] starting with largest remaining bag, give bag to kid with fewest candy.
Here is my solution for partitioning to two children (it is the correct answer). Maybe it will help with getting the 3-way partition.
int evenlyForTwo(int[] A, int M) {
boolean[] S = new boolean[M+1];
S[0]=true;//empty set
for(int i=0; i<A.length; i++)
for(int x=M; x >= A[i]; x--)
if(!S[x])
S[x]=S[x-A[i]];
int k = (int) M/2;
while(!S[k])
k--;
return k;//one kid gets k the other the rest.
}//
The problem you describe is known as the 3-Partition problem and is known to be NP-hard. The problem is discussed a bit on MathOverflow. You might find some of the pointers there of some value.
Here is a little solution, crude but gives correct results. And you can even change the number of children, bags, etc.
public class BagOfCandies {
static public void main(String...args) {
int repeat = 10;
int childCount = 3;
int bagsCount = childCount + (int) (Math.random() * 10);
for (int k=0; k<repeat; k++) {
int candyCount = 0, n=0;
int[] bags = new int[bagsCount];
for (int i=0; i<bags.length; i++) {
n += 1 + (int) (Math.random() * 2);
bags[i] = n;
candyCount += n;
}
shuffle(bags); // completely optional! It works regardless
boolean[][] dist = divideBags(bags, childCount);
System.out.println("Bags of candy : " + Arrays.toString(bags) + " = " + bags.length);
System.out.println("Total calculated candies is " + candyCount);
int childCandySum = 0;
for (int c=0; c<childCount; c++) {
int childCandies = countSumBags(bags, dist[c]);
System.out.println("Child " + (c+1) + " = " + childCandies + " --> " + Arrays.toString(dist[c]));
childCandySum += childCandies;
}
System.out.println("For a total of " + childCandySum + " candies");
System.out.println("----------------");
}
}
static private void shuffle(int[] bags) {
for (int i=0, len=bags.length; i<len; i++) {
int a = (int)Math.floor(Math.random()*len);
int b = (int)Math.floor(Math.random()*len);
int v = bags[a];
bags[a] = bags[b];
bags[b] = v;
}
}
static private boolean[][] divideBags(int[] bags, int childCount) {
int bagCount = bags.length;
boolean[][] dist = new boolean[childCount][bagCount];
for (int c=0; c<childCount; c++)
Arrays.fill(dist[c], false);
for (int i=0; i<bagCount; i+=childCount)
for (int j=i, c=0; c<childCount && j<bagCount; j++, c++)
dist[c][j] = true;
if (childCount == 1) return dist; // shortcut here
int sumDiff = 1;
int oldDiff = 0;
while (sumDiff != oldDiff) {
oldDiff = sumDiff;
sumDiff = 0;
// start comparing children in pair
for (int child1=0; child1<childCount-1; child1++) {
for (int child2=child1+1; child2<childCount; child2++) {
int count1 = countSumBags(bags, dist[child1]);
int count2 = countSumBags(bags, dist[child2]);
int diff = Math.abs(count1 - count2);
// a difference less than 2 is negligeable
if (diff > 1) {
// find some bags with can swap to even their difference
int c1=-1, c2=-1, cdiff;
boolean swap = false;
for (int i=0; i<bagCount-1; i++) {
for (int j=i; j<bagCount; j++) {
if (dist[child1][i] && dist[child2][j]) {
cdiff = Math.abs((count1 - bags[i] + bags[j]) - (count2 + bags[i] - bags[j]));
if (cdiff < diff) {
c1 = i; c2 = j;
diff = cdiff;
swap = true;
}
}
if (dist[child1][j] && dist[child2][i]) {
cdiff = Math.abs((count1 - bags[j] + bags[i]) - (count2 + bags[j] - bags[i]));
if (cdiff < diff) {
c1 = j; c2 = i;
diff = cdiff;
swap = true;
}
}
}
}
if (swap) {
//System.out.println("Swaping " + c1 + " with " + c2);
dist[child1][c1] = false; dist[child1][c2] = true;
dist[child2][c1] = true; dist[child2][c2] = false;
}
}
//System.out.println("Diff between " + child1 + "(" + countSumBags(bags, dist[child1]) + ") and " + child2 + "(" + countSumBags(bags, dist[child2]) + ") is " + diff);
sumDiff += diff;
}
}
//System.out.println("oldDiff="+oldDiff+", sumDiff="+sumDiff);
}
return dist;
}
static private int countSumBags(int[] bags, boolean[] t) {
int count = 0;
for (int i=0; i<t.length; i++) {
if (t[i]) {
count+=bags[i];
}
}
return count;
}
}
I don't know if this the result you were looking for, but it seems to be, from my understanding of the question.