I got for example two dimensional table 3x2 where all element have got 4x minus :"----". If I write in program for example 32. The first number tells us what number it is and second how many number is in row.
我得到了例如二维表3x2,其中所有元素都有4x减去:“----”。如果我在程序中编写例如32.第一个数字告诉我们它是什么数字,第二个数字是行数。
it will make my table like this (32):
它将使我的表像这样(32):
---- ---- 33-- ----
---- ---- -> ---- ----
---- ---- ---- ----
Then when we write another one (53): it will check table [0][0] if it is empty and if it is not it will be checked table [1][0] and table [0][1] if both are empty it will select table with lower number in this case table [0][1].
然后,当我们写另一个(53)时:它将检查表[0] [0],如果它是空的,如果它不是它将检查表[1] [0]和表[0] [1]如果两者都如果是空的,它将在这种情况下选择具有较低编号的表[0] [1]。
33-- ---- 33-- 555-
---- ---- -> ---- ----
---- ---- ---- ----
there we can put other numbers:
我们可以把其他数字放在一边:
33-- 555- 33-- 555-
---- ---- -> 444- 22--
---- ---- 333- 222-
we insert number in empty place- when all place is not empty we insert number there where is more "-" symbols - we take number 42 and in 33-- change it on 3344
我们在空的地方插入数字 - 当所有地方都不是空的时我们在那里插入数字更多“ - ”符号 - 我们采取数字42和33 - 改变它在3344
33-- 555- 3344 555-
444- 22-- -> 444- 22--
333- 222- 333- 222-
if we want insert more number in place where is not enough "-"- program ends
如果我们想在不足够的地方插入更多数字“ - ” - 程序结束
I started like this:
我开始是这样的:
import java.util.Scanner;
import java.lang.Math.*;
public class Skladisce2
{
public static int dolzina;
public static int sirina;
public static int enote;
public static int tabela[][][];
////////////////////////////////////
//// PREGLED VRSTIC
////////////////////////////////////
public static boolean Vstavi(int barva, int visina) {
int pozdolzina = 0;
int pozsirina = 0;
int najbolProsto = 0;
for(int j=0; j<dolzina; j++) {
for(int i=0; i<sirina; i++) {
int prosto=0;
for(int k=0;k<enote;k++) {
if( tabela[j][i][k]==0){
prosto++;
}
if( prosto>najbolProsto ) {
pozdolzina = i;
pozsirina = j;
najbolProsto = prosto;
for (int l=enote-najbolProsto; ((l<enote) &&(visina>0)); l++) {
tabela[pozdolzina][pozsirina][l] = barva;
visina--;}
continue;
}k++;
}
}
}
return true;
}
/////////////////////////////////////
//// IZPIS TABELE
//////////////////////////////////////
public static void Izpis() {
for (int i=0; i<dolzina; i++){
for (int j=0; j<sirina; j++){
for (int k=0; k<enote; k++) {
if(tabela[i][j][k] == 0) {
System.out.print("-");
}
else{
System.out.print(tabela[i][j][k]);
}
}
System.out.print(" ");
}
System.out.println();
}
}
public static void main (String[] args) {
Scanner vnos_stevila = new Scanner(System.in);
System.out.print("Insert dimension: ");
int vnos = vnos_stevila.nextInt();
// int vnos razdeli na podenote - prva številka je dolžina, druga širina in tretja enota
dolzina = Integer.parseInt(Integer.toString(vnos).substring(0,1));
sirina = Integer.parseInt(Integer.toString(vnos).substring(1,2));
enote = Integer.parseInt(Integer.toString(vnos).substring(2,3));
// izpis tabele s črtami
tabela= new int[dolzina][sirina][enote];
// izriše črtice
Izpis();
// VPIS SODOV
while (true){
System.out.print("Insert color and number");
int sod = vnos_stevila.nextInt();
int dolzinaIzpisa = (int)(Math.log10(sod)+1);
int barva = Integer.parseInt(Integer.toString(sod).substring(0,1));
int visina = Integer.parseInt(Integer.toString(sod).substring(1,2));
Vstavi(barva,visina);
Izpis();
}}
}
but when I insert number 32 it write:
但当我插入数字32时,它写道:
33-- 33--
33-- 33--
33-- 33--
How can I make program where will check the lowest table and insert number?
我怎样才能使程序在哪里检查最低表并插入数字?
1 个解决方案
#1
0
It is simply a matter of searching for the best free slot and inserting there. You may also find it easier to think of what you have as a three dimensional array, not a 2 dimensional one.
这只是寻找最好的免费插槽并插入那里的问题。你也可能会发现更容易想到你所拥有的三维数组,而不是二维数组。
import java.io.Console;
public class ArrayDemo {
private final int sizeX, sizeY, sizeZ;
private final int[][][] values;
public ArrayDemo() {
this(2,3,4);
}
public ArrayDemo(int x, int y, int z) {
values = new int[x][y][z];
sizeX = x;
sizeY = y;
sizeZ = z;
for(int i=0;i<x;i++) {
for(int j=0;j<y;j++) {
for(int k=0;k<z;k++) {
values[i][j][k]=-1;
}
}
}
}
public boolean insert(int value, int count) {
// find first slot with enough room
int posX = -1;
int posY = -1;
int bestFree = 0;
// locate largest available slot
for(int j=0;j<sizeY;j++) {
for(int i=0;i<sizeX;i++) {
int free=0;
for(int k=0;k<sizeZ;k++) {
if( values[i][j][k]==-1 ) free++;
}
if( free>bestFree ) {
posX = i;
posY = j;
bestFree = free;
}
}
}
// did we find a slot?
if( bestFree<count ) return false;
// found slot, insert data
for(int k=sizeZ-bestFree;(k<sizeZ) && (count>0);k++) {
values[posX][posY][k] = value;
count--;
}
return true;
}
public String toString() {
StringBuilder buf = new StringBuilder();
for(int j=0;j<sizeY;j++) {
for(int i=0;i<sizeX;i++) {
if( i>0 ) buf.append(' ');
for(int k=0;k<sizeZ;k++) {
if( values[i][j][k]==-1 ) {
buf.append('-');
} else {
buf.append(Character.forDigit(values[i][j][k], 36));
}
}
}
buf.append('\n');
}
return buf.toString();
}
public static void main(String[] args) {
ArrayDemo array = new ArrayDemo();
Console cons = System.console();
while( true ) {
String in = cons.readLine();
in = in.trim();
if( in.length() != 2 ) {
cons.printf("Please supply two digits: value and number\n");
continue;
}
int inputVal = Character.digit(in.charAt(0),36);
int inputNum = Character.digit(in.charAt(1),36);
if( inputVal==-1 || inputNum==-1 ) {
cons.printf("Please supply two digits: value and number\n");
continue;
}
if( array.insert(inputVal,inputNum) ) {
cons.printf("Data inserted OK\n%s\n", array.toString());
} else {
cons.printf("Data could not be inserted. Finished. Final array is:\n\n%s\n",array.toString());
return;
}
}
}
}
#1
0
It is simply a matter of searching for the best free slot and inserting there. You may also find it easier to think of what you have as a three dimensional array, not a 2 dimensional one.
这只是寻找最好的免费插槽并插入那里的问题。你也可能会发现更容易想到你所拥有的三维数组,而不是二维数组。
import java.io.Console;
public class ArrayDemo {
private final int sizeX, sizeY, sizeZ;
private final int[][][] values;
public ArrayDemo() {
this(2,3,4);
}
public ArrayDemo(int x, int y, int z) {
values = new int[x][y][z];
sizeX = x;
sizeY = y;
sizeZ = z;
for(int i=0;i<x;i++) {
for(int j=0;j<y;j++) {
for(int k=0;k<z;k++) {
values[i][j][k]=-1;
}
}
}
}
public boolean insert(int value, int count) {
// find first slot with enough room
int posX = -1;
int posY = -1;
int bestFree = 0;
// locate largest available slot
for(int j=0;j<sizeY;j++) {
for(int i=0;i<sizeX;i++) {
int free=0;
for(int k=0;k<sizeZ;k++) {
if( values[i][j][k]==-1 ) free++;
}
if( free>bestFree ) {
posX = i;
posY = j;
bestFree = free;
}
}
}
// did we find a slot?
if( bestFree<count ) return false;
// found slot, insert data
for(int k=sizeZ-bestFree;(k<sizeZ) && (count>0);k++) {
values[posX][posY][k] = value;
count--;
}
return true;
}
public String toString() {
StringBuilder buf = new StringBuilder();
for(int j=0;j<sizeY;j++) {
for(int i=0;i<sizeX;i++) {
if( i>0 ) buf.append(' ');
for(int k=0;k<sizeZ;k++) {
if( values[i][j][k]==-1 ) {
buf.append('-');
} else {
buf.append(Character.forDigit(values[i][j][k], 36));
}
}
}
buf.append('\n');
}
return buf.toString();
}
public static void main(String[] args) {
ArrayDemo array = new ArrayDemo();
Console cons = System.console();
while( true ) {
String in = cons.readLine();
in = in.trim();
if( in.length() != 2 ) {
cons.printf("Please supply two digits: value and number\n");
continue;
}
int inputVal = Character.digit(in.charAt(0),36);
int inputNum = Character.digit(in.charAt(1),36);
if( inputVal==-1 || inputNum==-1 ) {
cons.printf("Please supply two digits: value and number\n");
continue;
}
if( array.insert(inputVal,inputNum) ) {
cons.printf("Data inserted OK\n%s\n", array.toString());
} else {
cons.printf("Data could not be inserted. Finished. Final array is:\n\n%s\n",array.toString());
return;
}
}
}
}