我偶然發現了一個我想用Java解決的問題。用戶輸入較大的矩形尺寸(即L_width和L_height)和較小的矩形尺寸(即S_width和S_height)。我想將更多的小矩形放在較大的矩形內並以圖形方式顯示。如何使用Java將較小的矩形放置在較大的矩形內?
例如:當較大的矩形尺寸是4 x 5,較小的矩形尺寸是2 x 2時,那麼我可以將其放置在較大的矩形內的較小矩形的最大數量爲4。以圖形方式向他們展示。
由於即時通訊新的Java,我想知道我可以從程序的角度來看待這個問題,我必須使用什麼概念來實現相同。
用於計算最大矩形數的初始代碼。可以ANY1幫我顯示此結果以圖形方式使用Java
// Code Starts
import java.awt.Graphics;
import java.util.Scanner;
import javax.swing.JComponent;
import javax.swing.JFrame;
//Class to store the output of layout
class layout{
private int Cnt_BW_CW=0; // BoardWidth and CardWidth are arranged together
private int Cnt_BW_CH=0;
private int option=0; // Option 1: width-width Option 2: width-height
public int getCnt_BW_CW(){
return Cnt_BW_CW;
}
public int getCnt_BW_CH(){
return Cnt_BW_CH;
}
public int getoption(){
return option;
}
public void setCnt_BW_CW (int newValue){
Cnt_BW_CW = newValue;
}
public void setCnt_BW_CH (int newValue){
Cnt_BW_CH = newValue;
}
public void setoption (int newValue){
option = newValue;
}
}
// Stores the Dimension
class Dimension{
private float w,h;
Scanner input = new Scanner(System.in);
public Dimension(){
System.out.print("Enter Width: ");
w = input.nextInt();
System.out.print("Enter Height: ");
h = input.nextInt();
}
public Dimension(float width, float height){
w = width;
h = height;
}
public float getWidth(){
return w;
}
public float getHeight(){
return h;
}
public void setWidth (float newWidth){
w = newWidth;
}
public void setHeight (float newHeight){
h = newHeight;
}
}
class MyCanvas extends JComponent {
private static final long serialVersionUID = 1L;
public void paint(Graphics g) {
g.drawRect (10, 10, 200, 200);
}
}
public class boundedRect {
@SuppressWarnings("unused")
public static void main(String[] a) {
Dimension Board = new Dimension();
Dimension Card = new Dimension();
int Cnt =0;
Cnt = NumOfRect(Board, Card);
System.out.printf("Number of Cards:%d",Cnt);
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 300,300);
window.getContentPane().add(new MyCanvas());
window.setVisible(true);
}
public static int NumOfRect(Dimension b,Dimension c){
float bw,bh,cw,ch;
int bw_cw,bh_ch,bw_ch,bh_cw;
int SameDimensionCnt,DiffDimensionCnt;
int count;
layout Result = new layout();
bw =b.getWidth(); bh = b.getHeight();
cw =c.getWidth(); ch = c.getHeight();
if (bw < cw || bh < ch){
System.out.println("Board and Card Dimension mismatch");
System.exit(0);
}
bw_cw = (int)Math.floor(bw/cw);
bh_ch = (int)Math.floor(bh/ch);
SameDimensionCnt = bw_cw * bh_ch;
Result.setCnt_BW_CW(SameDimensionCnt);
bw_ch = (int)Math.floor(bw/ch);
bh_cw = (int)Math.floor(bh/cw);
DiffDimensionCnt = bw_ch * bh_cw;
Result.setCnt_BW_CH(DiffDimensionCnt);
System.out.printf("Matching BW x CW: %d\n",SameDimensionCnt);
System.out.printf("Matching BW x CH: %d\n",DiffDimensionCnt);
if (SameDimensionCnt < DiffDimensionCnt){
count = DiffDimensionCnt;
System.out.println("Align Board Width and Card Height");
Result.setoption(2);
}else {
count = SameDimensionCnt;
System.out.println("Align Board Width and Card Width");
Result.setoption(1);
}
return count;
}
}
*「我想知道如何從程序的角度來處理這個問題」*您如何使用鉛筆和紙張來處理這個問題? – 2012-07-25 04:43:59