This question already has answers here:
Random class acting odd?
(6 answers)
Closed 7 years ago.
I came across one code snippet which is using Random to get 10 integers. But Random is not returning random numbers, instead 1 always.
Code :
public static void main(String[] args) {
Random random = new Random(441287210);
for (int i = 0; i < 10; i++)
System.out.print(random.nextInt(10) + " ");
}
And I could not find explanation to this. Can anybody help to understand this behaviour?
Not always. Given 441287210 as the seed, it just so happens that the next 10 random numbers between 0 and 10 turn out to be all 1s...
Random random = new Random(441287210);
for (int i = 0; i < 100; i++) {
System.out.print(random.nextInt(10) + " ");
}
If you generate 100 numbers, you'll get:
1 1 1 1 1 1 1 1 1 1 3 4 7 2 2 6 0 3 0 2 8 4 1 6 0 0 0 2 8 2 9 8 9 2 5 2 1 1 4 5 3 4 1 4 1 8 7 6 6 0 6 5 0 4 5 5 6 0 8 3 8 9 7 4 0 9 9 7 7 9 3 9 6 4 5 0 6 3 7 4 9 8 7 6 2 8 9 8 4 4 8 4 9 0 1 6 9 6 1 5
Related
how to create a half pyramid inverted with matrix pattern like this with loop?
expected output
5 5 5 5 5 4 3 2 1
5 4 4 4 4 3 2 1
5 4 3 3 3 2 1
5 4 3 2 2 1
5 4 3 2 1
my code like this
public static void main(String[] args) {
int N = 5;
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= N; j++)
{
int min = i < j ? i : j;
System.out.print(N - min + 1 + " ");
}
for (int k = N-1; k >=1; k --) {
System.out.print(k + " ");
}
System.out.println();
}
}
and my output like this
5 5 5 5 5 4 3 2 1
5 4 4 4 4 4 3 2 1
5 4 3 3 3 4 3 2 1
5 4 3 2 2 4 3 2 1
5 4 3 2 1 4 3 2 1
Your nested loops needs to be 3 deep:
Loop over the rows:
5 5 5 5 5 4 3 2 1 ┐
5 4 4 4 4 3 2 1 ┤
5 4 3 3 3 2 1 ┤ Loop 1
5 4 3 2 2 1 ┤
5 4 3 2 1 ┘
Loop over the number values:
┌─────┬─┬─┬─┐ Loop 2
┌───┴───┐
5 5 5 5 5 4 3 2 1
5 4 4 4 4 3 2 1
5 4 3 3 3 2 1
5 4 3 2 2 1
5 4 3 2 1
└─┴─┴─┴─┘ Loop 2
Loop over the repeats of a number value:
┌─────...
┌─┬─┼─┬─┐ Loop 3
5 5 5 5 5 4 3 2 1
5 4 4 4 4 3 2 1
5 4 3 3 3 2 1
5 4 3 2 2 1
5 4 3 2 1
Good luck coding that, since it looks like an assignment that you need to complete.
Since I can't leave a simple challenge like that alone, here is a solution. I added logic so it can handle pyramid sizes above 9, and then compressed the code to obscure it a bit.
static void printHalfPyramidInverted(int n) {
String f, fmt = " %" + Integer.toString(n).length() + "d";
for (int k, j, i = n; i > 0; i--, System.out.println())
for (f = fmt.substring(1), j = n; j > 0; j--)
for (k = (i == j ? j : 1); k > 0; k--, f = fmt)
System.out.printf(f, j);
}
Output (size 5)
5 5 5 5 5 4 3 2 1
5 4 4 4 4 3 2 1
5 4 3 3 3 2 1
5 4 3 2 2 1
5 4 3 2 1
Output (size 9)
9 9 9 9 9 9 9 9 9 8 7 6 5 4 3 2 1
9 8 8 8 8 8 8 8 8 7 6 5 4 3 2 1
9 8 7 7 7 7 7 7 7 6 5 4 3 2 1
9 8 7 6 6 6 6 6 6 5 4 3 2 1
9 8 7 6 5 5 5 5 5 4 3 2 1
9 8 7 6 5 4 4 4 4 3 2 1
9 8 7 6 5 4 3 3 3 2 1
9 8 7 6 5 4 3 2 2 1
9 8 7 6 5 4 3 2 1
Output (size 15)
15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
15 14 14 14 14 14 14 14 14 14 14 14 14 14 14 13 12 11 10 9 8 7 6 5 4 3 2 1
15 14 13 13 13 13 13 13 13 13 13 13 13 13 13 12 11 10 9 8 7 6 5 4 3 2 1
15 14 13 12 12 12 12 12 12 12 12 12 12 12 12 11 10 9 8 7 6 5 4 3 2 1
15 14 13 12 11 11 11 11 11 11 11 11 11 11 11 10 9 8 7 6 5 4 3 2 1
15 14 13 12 11 10 10 10 10 10 10 10 10 10 10 9 8 7 6 5 4 3 2 1
15 14 13 12 11 10 9 9 9 9 9 9 9 9 9 8 7 6 5 4 3 2 1
15 14 13 12 11 10 9 8 8 8 8 8 8 8 8 7 6 5 4 3 2 1
15 14 13 12 11 10 9 8 7 7 7 7 7 7 7 6 5 4 3 2 1
15 14 13 12 11 10 9 8 7 6 6 6 6 6 6 5 4 3 2 1
15 14 13 12 11 10 9 8 7 6 5 5 5 5 5 4 3 2 1
15 14 13 12 11 10 9 8 7 6 5 4 4 4 4 3 2 1
15 14 13 12 11 10 9 8 7 6 5 4 3 3 3 2 1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 2 1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
You could start constructing from bottom and notice how middle is changed (added i at i times).Each line is assembled from right to left.(adapted properly on the end) Using additional data structure can make some task easier.
public class TestPyramid {
public static void main(String[] args) {
int N = 6;
int next = N;
List<String> list = new ArrayList<String>();
for(int i=1;i<=N;i++)
{
List<Integer> line=new ArrayList<Integer>();
for(int j=1;j<=N;j++)
{
//middle
if(next-N+2==j)
{
for(int k=1;k<next-N+1;k++)
{
line.add(i);
}
}
line.add(j);
}
if(i==N)
{
for(int k=1;k<next-N+1;k++)
{
line.add(i);
}
}
next++;
String adaptedLine = line.stream().map(t->String.valueOf(t)).collect(Collectors.joining(" "));
StringBuilder sb = new StringBuilder(adaptedLine);
list.add(sb.reverse().toString());
}
Collections.reverse(list);
list.forEach(System.out::println);
}
}
Output:
6 6 6 6 6 6 5 4 3 2 1
6 5 5 5 5 5 4 3 2 1
6 5 4 4 4 4 3 2 1
6 5 4 3 3 3 2 1
6 5 4 3 2 2 1
6 5 4 3 2 1
Note: If N>=10 then reverse line should be adapted properly since 10 is 01 in current version
String adaptedLine = line.stream().
sorted(Collections.reverseOrder()).
map(t>String.valueOf(t)).
collect(Collectors.joining(" "));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
It's been a while since I have used java and I am currently trying to iterate over a 3d array.
This array is structured as Object[][][] dataSet, where Object[company][event][number]. Now I want to iterate over it in such a manner that I can iterate through every number, after I iterate through all these numbers I want to increase the company index, so I can check the other companies. When I reach the end of the companies I want to increase the event index and try all over again.
Sort of like:
Iterate through each number of company "m"
when company "m" is searched through, go to the next company
if all company m are searched: increase event and restart the search.
The reason for doing it in this manner is, I want to create a list at the end that contains all numbers from all companies for each event like so: T1 is all numbers for event 1, T2 all numbers for event 2, etc.
I did a 3 nested for loop, and I am modifying it, but I am a bit confused over what to do exactly and would like some guidance about the idea.
Edit 1:
As of now, my current try follows this format and idea:
for(int i=0; i<dataSet.length; i++) {//company
for(int j=0; j<dataSet[i].length; j++) {//event
for(int k=0; k<dataSet[i][j].length; k++) {//numbers
System.out.print(dataSet[i][j][k]);
System.out.println(" positions: "+ i + " " + j + " ");
}
}
}
Currently, I am trying to sort of swap the for loops trying to make the first for loop into a loop that iterates through events using an external variable. and the second loop would iterate through companies.
Making this:
for(int i=0; i<dataSet[company].length; i++) {//event
if (company < dataSet.length-1) {
company++;
}
for(int j=0; j<dataSet.length; j++) {//company
for(int k=0; k<dataSet[j][i].length; k++) {//numbers
System.out.print(dataSet[j][i][k]);
System.out.println(" positions: "+ j + " " + i + " ");
}
}
}
The result this is currently giving is essentially the same as having a normal 3 nested loop
A bit more info:
Set[0][0] = 9
Set[0][1] = 0 1 3 4 6 7 8 9
Set[0][2] = 9
Set[0][3] = 9
Set[0][4] = 0 1 2 4 5 6 7 8 9
Set[0][5] = 0 1 2 3 4 5 7 8 9
Set[0][6] = 0 2 3 4 5 6 7 8 9
Set[0][7] = 0 1 2 3 4 6 7 8 9
Set[0][8] = 9
Set[0][9] = 9
Set[1][0] =
Set[1][1] =
Set[1][2] =
Set[1][3] =
Set[1][4] =
Set[1][5] = 2 3 4 5 6 7 8
Set[1][6] =
Set[1][7] =
Set[1][8] =
Set[1][9] =
these are my sets that are my 3d array
9 positions: 0 0
0 positions: 0 1
1 positions: 0 1
3 positions: 0 1
4 positions: 0 1
6 positions: 0 1
7 positions: 0 1
8 positions: 0 1
9 positions: 0 1
9 positions: 0 2
9 positions: 0 3
0 positions: 0 4
1 positions: 0 4
2 positions: 0 4
4 positions: 0 4
5 positions: 0 4
6 positions: 0 4
7 positions: 0 4
8 positions: 0 4
9 positions: 0 4
0 positions: 0 5
1 positions: 0 5
2 positions: 0 5
3 positions: 0 5
4 positions: 0 5
5 positions: 0 5
7 positions: 0 5
8 positions: 0 5
9 positions: 0 5
2 positions: 1 5
3 positions: 1 5
4 positions: 1 5
5 positions: 1 5
6 positions: 1 5
7 positions: 1 5
8 positions: 1 5
these are my iterations, skipping any that is empty
The correct output should have something along these lines:
9 positions: 0 0
empty positions: 1 0
0 positions: 0 1
empty positions: 1 1
...
0 positions: 0 5
1 positions: 0 5
2 positions: 0 5
3 positions: 0 5
4 positions: 0 5
5 positions: 0 5
7 positions: 0 5
8 positions: 0 5
9 positions: 0 5
2 positions: 1 5
3 positions: 1 5
4 positions: 1 5
5 positions: 1 5
6 positions: 1 5
7 positions: 1 5
8 positions: 1 5
Hope that gives a bit more insight.
Edit 2: New idea: It is a given information that I can always know how many events and companies there will be in this case: 2 companies and 10 events, so I will try to make a for loop with this in mind and will update with my results.
It appears that over my complication and confusion I failed to properly look and assess:
int event = 10;
int company = 2;
//for(int pos=0; pos < event-1; pos++) {
for(int i=0; i<event; i++) {//event
for(int j=0; j<company; j++) {//company
for(int k=0; k<dataSet[j][i].length; k++) {//numbers
System.out.print(dataSet[j][i][k]);
System.out.println(" positions: "+ j + " " + i + " ");
}
}
}
The idea I initially had does work, and I leave it here for future reference and for others if they ever come into the same issue.
The issue in my results was failing to analyze my datasets, it gave the appearance of giving me the wrong result, but that is because I did not take into account the empty sets, when I was doing it manually. When I changed the sets in order to have no empty sets you could see that the code functioned correctly like so:
Set[0][0] = 9
Set[0][1] = 0 1 3 4 6 7 8 9
Set[0][2] = 9
Set[0][3] = 9
Set[0][4] = 0 1 2 4 5 6 7 8 9
Set[0][5] = 0 1 2 3 4 5 7 8 9
Set[0][6] = 0 2 3 4 5 6 7 8 9
Set[0][7] = 0 1 2 3 4 6 7 8 9
Set[0][8] = 9
Set[0][9] = 9
Set[1][0] = 1
Set[1][1] = 2
Set[1][2] =
Set[1][3] = 1
Set[1][4] = 1
Set[1][5] = 2 3 4 5 6 7 8
Set[1][6] = 4
Set[1][7] = 6
Set[1][8] = 7
Set[1][9] = 4
9 positions: 0 0
1 positions: 1 0
0 positions: 0 1
1 positions: 0 1
3 positions: 0 1
4 positions: 0 1
6 positions: 0 1
7 positions: 0 1
8 positions: 0 1
9 positions: 0 1
2 positions: 1 1
9 positions: 0 2
9 positions: 0 3
1 positions: 1 3
0 positions: 0 4
1 positions: 0 4
2 positions: 0 4
4 positions: 0 4
5 positions: 0 4
6 positions: 0 4
7 positions: 0 4
8 positions: 0 4
9 positions: 0 4
1 positions: 1 4
0 positions: 0 5
1 positions: 0 5
2 positions: 0 5
3 positions: 0 5
4 positions: 0 5
5 positions: 0 5
7 positions: 0 5
8 positions: 0 5
9 positions: 0 5
2 positions: 1 5
3 positions: 1 5
4 positions: 1 5
5 positions: 1 5
6 positions: 1 5
7 positions: 1 5
8 positions: 1 5
0 positions: 0 6
2 positions: 0 6
3 positions: 0 6
4 positions: 0 6
5 positions: 0 6
6 positions: 0 6
7 positions: 0 6
8 positions: 0 6
9 positions: 0 6
4 positions: 1 6
0 positions: 0 7
1 positions: 0 7
2 positions: 0 7
3 positions: 0 7
4 positions: 0 7
6 positions: 0 7
7 positions: 0 7
8 positions: 0 7
9 positions: 0 7
6 positions: 1 7
9 positions: 0 8
7 positions: 1 8
9 positions: 0 9
4 positions: 1 9
So I made this method of rolling a dice 100 times with a 50% chance of rolling 6.
Basic idea is that there are 50% odd numbers and 50% even numbers between 1 and 6, so if an even number is rolled, system prints 6 else it prints a random number between 1 and 5. Do you think this is correct?
public static void printDiceRolls(Random randGenerator) {
for (int i=0; i < 30; i++) {
int temp;
temp = randGenerator.nextInt(6) + 1;
if (temp%2 == 0) {
temp = 6;
}
else
temp = randGenerator.nextInt(5) + 1;
System.out.print(" " + temp + " ");
}
}
Generate a random number between 1 and 10, inclusive on both ends. If the number be 1 to 5, you rolled that number, otherwise, you rolled 6. Note that there are 5 chances in this scheme to roll a 6 (i.e. 50%), and 5 total chances to roll 1 through 5 (i.e. the other 50%).
Random random = new Random();
int roll = random.nextInt(10) + 1;
if (roll > 5) {
System.out.println("You rolled a 6");
}
else {
System.out.println("You rolled a " + roll);
}
You could generate number from 1 to 10 and print 6 if its bigger than 6
for (int i = 0 ; i < 30 ; i++) {
int temp = randGenerator.nextInt(10) + 1;
if (temp > 6) {
temp = 6;
}
System.out.print(" "+temp+" ");
}
Dice roll with a 50% chance of rolling 6
The following method ensures 50% of the dice being 6 but they won't
come in alternative manner... I think this is the best way to ensure
that 6 being diced exactly 50% times but not in alternating sequence
which makes the result more believable, in my opinion.
In my view you only need to do it:
public static void naturalDicingWithVariablePercentageOfSix(int totalDice,int probability)
{
Random rand=new Random();
List<Integer> diceList=new ArrayList<Integer>();
System.out.println(""+( (int)(100/probability))+"% times that six being diced \n\n");
for(int i=0;i<totalDice;i++)
{
if(i%probability==0)
diceList.add(6);
else
diceList.add(rand.nextInt(5)+1);
}
Collections.shuffle(diceList);
int notSixCount=0;
int sixCount=0;
for(Integer diceVal:diceList)
{
if(diceVal!=6)
notSixCount++;
else
sixCount++;
System.out.print(diceVal+" ");
}
System.out.println("\n\n Not 6 being diced : "+notSixCount+" times "+" and Six being diced :"+sixCount+"\n\n");
}
Input:
naturalDicingWithVariablePercent6(100,2); // 50% times 6
naturalDicingWithVariablePercent6(100,3); // 33% times 6
naturalDicingWithVariablePercent6(100,4); // 25% times 6
naturalDicingWithVariablePercent6(100,5); // 20% times 6
naturalDicingWithVariablePercent6(100,6); // 16.67% times 6
Output:
50% times that six being diced
4 3 6 6 3 5 4 6 2 1 6 6 6 6 1 6 6 4 3 5 6 6 5 4 2 5 6 2 1 6 5 5 1 6 6 6 2 6 4 6 6 4 6 6 6 6 2 1 6 3 6 4 3 5 6 6 5 6 6 6 6 6 4 2 6 3 3 6 3 6 6 6 3 5 2 5 6 6 3 2 6 6 6 5 4 6 4 4 3 6 5 4 6 1 6 6 6 6 5 6
Not 6 being diced : 50 times and Six being diced :50
33% times that six being diced
2 6 1 6 5 2 1 5 6 5 6 3 3 2 1 3 5 3 6 6 4 4 2 6 6 1 1 4 2 6 6 3 6 2 6 5 6 4 1 3 6 4 1 1 2 6 5 6 6 6 2 4 2 2 3 6 4 3 4 1 6 4 4 2 4 6 6 4 5 4 6 6 2 4 4 5 3 6 6 6 3 6 6 1 2 4 1 4 6 6 6 5 4 4 5 5 6 2 3 6
Not 6 being diced : 66 times and Six being diced :34
25% times that six being diced
6 1 3 4 6 4 4 2 4 1 6 3 6 1 1 3 3 5 3 4 1 2 3 3 6 3 3 4 5 5 2 6 5 2 3 1 1 4 4 6 5 4 2 3 5 6 3 4 6 4 6 6 5 6 6 5 1 6 6 6 2 3 6 3 6 4 6 5 3 4 5 2 4 2 1 6 3 5 6 3 2 1 4 1 3 6 4 6 6 5 5 5 5 4 2 1 2 3 3 6
Not 6 being diced : 75 times and Six being diced :25
20% times that six being diced
6 4 6 1 3 5 4 4 1 4 4 1 4 5 6 3 4 3 2 3 3 5 2 1 3 3 2 6 1 3 5 6 5 2 2 6 3 1 2 2 1 6 2 1 3 1 1 1 1 1 5 4 4 6 2 2 2 2 1 6 3 5 6 5 3 6 6 5 2 4 3 2 6 4 6 6 6 6 3 5 2 3 2 4 4 5 4 2 1 6 1 6 6 3 3 5 2 5 2 5
Not 6 being diced : 80 times and Six being diced :20
16% times that six being diced
5 4 2 2 4 4 6 2 2 1 3 5 4 2 5 5 6 5 3 3 4 3 3 5 3 1 6 5 3 6 2 4 6 6 5 4 4 6 6 5 6 5 1 3 4 3 2 1 1 6 1 1 4 4 3 3 1 3 5 3 2 6 5 4 4 2 6 4 6 3 1 4 2 3 1 1 1 1 1 3 3 5 2 3 3 6 6 2 3 3 5 5 1 1 6 4 6 3 2 2
Not 6 being diced : 83 times and Six being diced :17
Don't roll a six sided die, roll a ten sided die. The die has sides marked [1, 2, 3, 4, 5, 6, 6, 6, 6, 6]. Pick a random side for each die roll.
I'm working on an assignment that asks a user to input an integer between 1-15 and then displays an integer pyramid for the number of rows they selected.
I have everything working, but if the number enters an integer greater than 10, I'm getting tripped up by the extra space needed for a double digit number. I've attached my code below. If anyone could provide a little help it would be greatly appreciated.
int lines = input.nextInt();
for (int row = 1; row <= lines; row++)
{
for (int column = 1; column <= lines - row; column++)
{
System.out.print(" ");
}
for (int num = row; num >= 1; num--)
{
System.out.print((num>=10)?+num:" "+num);
}
for (int num = 2; num <= row; num++)
{
System.out.print((num>=10)?+num:" "+num);
}
System.out.println();
}
With my current code, if the user entered 13, it would produce the following output:
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 910
11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12
13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13
I just need to figure out how to get the extra space for the double digit integers. The desired output would be:
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12
It looks like you have chosen to do this the (slightly) harder way.
The easy way would be to reserve three spaces for each number (so that single digit numbers would have two spaces between them).
What you have chosen to do is variable spacing depending on the actual length of the numbers in each column. The first step is to change your output statements to the following:
System.out.print(" "+num);
So you will always print one space between numbers on each row. If you run that, you'll notice that it almost works except the top part of the triangle is misaligned. To fix that, you'll have to adjust your
System.out.print(" ");
statement so that the number of spaces it prints in each column depends on the value of the number that will appear in each column later on. To do this, you'll have to work out some arithmetic involving column and lines, to choose between " " (two spaces) and " " (three spaces). It's straightforward but I'll let you work out the details.
It is possible to further extend this idea to support 100 lines or more, but it's not clear that you need that capability.
I was interested in solving this, so wrote a simple solution for your task. Seems to work for any lines value.
public static void main(String[] args) {
int lines = 100;
for (int row = 1; row <= lines; row++) {
for (int i = 0; i < calculateOffset(row, lines); i++) {
System.out.print(" ");
}
System.out.print(row);
for (int num = row-1; num >= 1; num--) {
System.out.print(" " + num);
}
for (int num = 2; num <= row; num++) {
System.out.print(" " + num);
}
System.out.println();
}
}
private static int calculateOffset(int row, int totalRows) {
return calculateSpace(totalRows) - calculateSpace(row);
}
private static int calculateSpace(int columnsCount) {
int categoryLowest = 1;
int categoryHighest = 9;
int categoryDigits = 1;
int charactersUsed = 0;
while (categoryLowest <= columnsCount) {
int categoryItems = Math.min(categoryHighest, columnsCount) - categoryLowest + 1;
int numbersCharacters = categoryDigits * categoryItems;
int spacesCharacters = (categoryItems - 1);
boolean previousCategoryIncluded = categoryLowest > 1;
int spaceBetweenCategoriesPresent = previousCategoryIncluded ? 1 : 0;
charactersUsed += numbersCharacters + spacesCharacters + spaceBetweenCategoriesPresent;
categoryHighest = categoryHighest * 10 + 9;
categoryLowest *= 10;
categoryDigits++;
}
return charactersUsed;
}
No idea why you chose the logic like that. However, intuitively, this is what I am going to do:
1 find mid point location (you can simply contruct the last line and find the mid-point)
2 a function to construct a line, by simply do (psuedo-code):
String getLine(int num) {
String result = "";
for (int i = num; i > 0; i--) {
result = result + i + " ";
}
for (int i = 2; i <= num; i++) {
result = result + i + (i == num? "" : " ");
}
return result;
}
3 do a loop to print each line:
int midPoint = .....; //
for (i=0; i < num; i++) {
String line = getLine(i+1);
print (midPoint - mid point of line) spaces;
print line
}
Update:
Have briefly tested, looks good to me:
public static String getLine(int num) {
String result = "";
for (int i = num; i > 0; --i) {
result = result + i + " ";
}
for (int i = 2; i <= num; ++i) {
result = result + i + (i == num ? "" : " ");
}
return result;
}
public static void main(String[] args) {
int num = 15;
int midPoint = getLine(num).length()/2 + 1;
for (int i = 0; i < num; ++i) {
String line = getLine(i+1);
int noPrefix = midPoint - (line.length()+1)/2 ;
for (int j = 0; j < noPrefix; ++j) {
System.out.print(" ");
}
System.out.println(line);
}
}
result :
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12
13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13
14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Late to the party, but here's a solution using StringBuilder and printing it out only at the end.
The idea is to calculate the amount of padding required based on the String equivalent of the current value.
Note: The solution is not efficient (with all the conversion to String), but it can be used for any input value.
Working Example:
import java.util.Scanner;
public class IntegerPyramid {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter # of lines: ");
int numLines = sc.nextInt();
StringBuilder sb = new StringBuilder();
System.out.println();
for(int row = 1; row <= numLines; row++) {
for(int num = -numLines; num <= numLines; num++) {
if(num == -1 || num == 0)
continue;
int value = Math.abs(num);
int padding = String.valueOf(value).length() + 1;
if(value <= row) {
// Print numbers
sb.append(String.format(("%" + padding + "d"), value));
}
else {
// Print spaces
sb.append(String.format(("%" + padding + "s"), ""));
}
}
sb.append("\n");
}
System.out.println(sb.toString());
sc.close();
}
}
Output:
Enter # of lines: 6
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
Enter # of lines: 15
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12
13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13
14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
You've got the conditional backwards, believe it should be
System.out.print((num >= 10) ? " " + num : num)
Easier to understand
System.out.print((num < 10) ? num : num : " " + num)
I am working in an Authomata and need three numbers that suumed equal n
For example, if n = 2 the numbers I need are:
200
020
002
110
101
011
It doesn´t matter if the combination are repeated.
If n = 3 I need:
300
030
003
210
201
021
120
012
102
111
So I read thar this was similar to the partition in the theory of numbers but I can get the particular case where only 3 numbers give me the target value (n). (The code is from a example I get here)
package automata2;
import java.util.ArrayList;
/**
*
* #author jaime
*/
public class Automata2 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
int N = 14;
partition(N);
}
public static void partition(int n) {
partition(n, n, "");
}
public static void partition(int n, int max, String prefix) {
if (n == 0) {
System.out.println(prefix);
return;
}
for (int i = Math.min(max, n); i >= 1; i--) {
partition(n-i, i, prefix + " " + i);
}
}
}
But I only need al the combinations with three digits, not all since 14 until 1 1 1 1 1 1 1 1 1 1 1 1 1 1
The algorithm is like this to generate the numbers. This code will generate all nos.
public class main1 {
public static void main(String args[]) {
int N = 5, n1, n2, n3;
for (n1 = 0; n1 <= N; n1++) {
for (n2 = 0; n2 <= N; n2++) {
for (n3 = 0; n3 <= N; n3++) {
if ( (n1+n2+n3)==N ) {
System.out.println(n1 + " " + n2 + " " + n3);
}
}
}
}
}
}
what you can do is instead of System.out.println(n1+" "+n2+" "+n3), you can store the numbers in an array list then arrange the list.
One solution is to check number of numbers is three and then print it
public static void main(String[] args) {
int N = 14;
partition(N);
}
public static void partition(int n) {
partition(n, n, "");
}
public static void partition(int n, int max, String prefix) {
// added condition here
if (n == 0 && prefix.trim().split(" ").length == 3) {
System.out.println(prefix);
return;
}
for (int i = Math.min(max, n); i >= 1; i--) {
partition(n-i, i, prefix + " " + i);
}
}
Output :
12 1 1
11 2 1
10 3 1
10 2 2
9 4 1
9 3 2
8 5 1
8 4 2
8 3 3
7 6 1
7 5 2
7 4 3
6 6 2
6 5 3
6 4 4
5 5 4
Using nested loops - only for 3 number solution
int N = 14;
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= N; j++) {
for (int k = 0; k <= N; k++) {
if (i + j + k == N) {
System.out.println(i + " " + j + " " + k);
}
}
}
}
Output :
0 0 14
0 1 13
0 2 12
0 3 11
0 4 10
0 5 9
0 6 8
0 7 7
0 8 6
0 9 5
0 10 4
0 11 3
0 12 2
0 13 1
0 14 0
1 0 13
1 1 12
1 2 11
1 3 10
1 4 9
1 5 8
1 6 7
1 7 6
1 8 5
1 9 4
1 10 3
1 11 2
1 12 1
1 13 0
2 0 12
2 1 11
2 2 10
2 3 9
2 4 8
2 5 7
2 6 6
2 7 5
2 8 4
2 9 3
2 10 2
2 11 1
2 12 0
3 0 11
3 1 10
3 2 9
3 3 8
3 4 7
3 5 6
3 6 5
3 7 4
3 8 3
3 9 2
3 10 1
3 11 0
4 0 10
4 1 9
4 2 8
4 3 7
4 4 6
4 5 5
4 6 4
4 7 3
4 8 2
4 9 1
4 10 0
5 0 9
5 1 8
5 2 7
5 3 6
5 4 5
5 5 4
5 6 3
5 7 2
5 8 1
5 9 0
6 0 8
6 1 7
6 2 6
6 3 5
6 4 4
6 5 3
6 6 2
6 7 1
6 8 0
7 0 7
7 1 6
7 2 5
7 3 4
7 4 3
7 5 2
7 6 1
7 7 0
8 0 6
8 1 5
8 2 4
8 3 3
8 4 2
8 5 1
8 6 0
9 0 5
9 1 4
9 2 3
9 3 2
9 4 1
9 5 0
10 0 4
10 1 3
10 2 2
10 3 1
10 4 0
11 0 3
11 1 2
11 2 1
11 3 0
12 0 2
12 1 1
12 2 0
13 0 1
13 1 0
14 0 0
One by one checking with skipping unnecessary partitions.
public static void main(String[] args) {
int n =14;
ArrayList<String> temp = partition(n);
for (int z =0; z<temp.size(); z++){
System.out.println(temp.get(z));
}
}
public static ArrayList<String> partition(int n){
ArrayList<String> out = new ArrayList<String>();
int a=0, b=0, c=0;
for (c=n; c>=0; c--){
for(int j =a; j<=n;j++){
if((c+j)>n){
j=n+1;
continue;
}
for (int i=b;i<=n; i++){
int p=c+j+i;
if (p>n) i=n+1;
if(p==n){
String s = Integer.toString(c)+" "+ Integer.toString(j)+" "+ Integer.toString(i);
out.add(s);
}
}
}
}
return out;
}
Out put:
14 0 0
13 0 1
13 1 0
12 0 2
12 1 1
12 2 0
11 0 3
11 1 2
11 2 1
11 3 0
10 0 4
10 1 3
10 2 2
10 3 1
10 4 0
9 0 5
9 1 4
9 2 3
9 3 2
9 4 1
9 5 0
8 0 6
8 1 5
8 2 4
8 3 3
8 4 2
8 5 1
8 6 0
7 0 7
7 1 6
7 2 5
7 3 4
7 4 3
7 5 2
7 6 1
7 7 0
6 0 8
6 1 7
6 2 6
6 3 5
6 4 4
6 5 3
6 6 2
6 7 1
6 8 0
5 0 9
5 1 8
5 2 7
5 3 6
5 4 5
5 5 4
5 6 3
5 7 2
5 8 1
5 9 0
4 0 10
4 1 9
4 2 8
4 3 7
4 4 6
4 5 5
4 6 4
4 7 3
4 8 2
4 9 1
4 10 0
3 0 11
3 1 10
3 2 9
3 3 8
3 4 7
3 5 6
3 6 5
3 7 4
3 8 3
3 9 2
3 10 1
3 11 0
2 0 12
2 1 11
2 2 10
2 3 9
2 4 8
2 5 7
2 6 6
2 7 5
2 8 4
2 9 3
2 10 2
2 11 1
2 12 0
1 0 13
1 1 12
1 2 11
1 3 10
1 4 9
1 5 8
1 6 7
1 7 6
1 8 5
1 9 4
1 10 3
1 11 2
1 12 1
1 13 0
0 0 14
0 1 13
0 2 12
0 3 11
0 4 10
0 5 9
0 6 8
0 7 7
0 8 6
0 9 5
0 10 4
0 11 3
0 12 2
0 13 1
0 14 0
import java.util.Scanner;
public class ExampleMinNumber {
public static void main(String args[])
{
System.out.println("enter number which comibnation of sum you want");
Scanner input=new Scanner(System.in);
int num=input.nextInt();
for(int i=0;i<=3;i++)
{
int sum=0;
for(int j=0;j<=3;j++)
{
for(int k=0;k<=3;k++)
{
sum=i+j+k;
if(sum==num)
{
System.out.println(+i+""+j+""+k);
}
}
}
}
input.close();
}
}