高手请进:经典的智力题,用java怎么解啊..... 五部电梯里的人都要坐电梯,五部电梯分别在17、26、20、19、31层。但是必须得五部电梯全都在21和25层之间门才会打开。而电梯只有两种升降方式,一是其中任意两部电梯上升8层,二是其中任意两部电梯下降13层。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /* * To change this template, choose Tools | Templates * and open the template in the editor. *//* * Main.java * * Created on 2010-3-16, 16:28:45 */package lift;/** * * @author mickyfeng */public class Main extends javax.swing.JFrame { /** Creates new form Main */ public Main() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1); jButton1.setText("Start"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(46, 46, 46) .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(39, 39, 39) .addComponent(jButton1)) .addGroup(layout.createSequentialGroup() .addGap(25, 25, 25) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(22, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: Integer[] liftN ={17,26,20,19,31}; while(notInCondition(liftN)){ JustDown(liftN); JustUp(liftN); } jTextArea1.append(ArrToStr(liftN)+"\n"); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Main().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; // End of variables declaration private boolean notInCondition(Integer[] liftN) { String ShowStr ="Check Num :"; for(int i=0;i<5;i++){ ShowStr = ShowStr+ liftN[i]+"; "; if (( liftN[i]<21)|(liftN[i]>25 )) { jTextArea1.append(ShowStr+"\n"); return true; } } jTextArea1.append(ShowStr+"\n"); return false; } private void JustDown(Integer[] liftN) { int Max1=0,Max2=0; jTextArea1.append("Just Down Before :"+ArrToStr(liftN)+"\n"); for(int i= 1;i<5;i++){ if (liftN[Max1] <liftN[i]){ Max1 =i; }else{ if (liftN[Max2] <liftN[i]){ Max2 = i; } } } if ((liftN[Max1]>25)|(liftN[Max2]>25)) { liftN[Max1]-=13; liftN[Max2]-=13; } jTextArea1.append("Just Down After :"+ArrToStr(liftN)+"\n"); } private void JustUp(Integer[] liftN) { int Min1=0,Min2=0; jTextArea1.append("Just Up Before :"+ArrToStr(liftN)+"\n"); for(int i= 1;i<5;i++){ if (liftN[Min1] >liftN[i]){ Min1 =i; }else{ if (liftN[Min2] >liftN[i]){ Min2 = i; } } } if((liftN[Min1]<21)|liftN[Min2]<21){ liftN[Min1]+=8; liftN[Min2]+=8; } jTextArea1.append("Just Up After :"+ArrToStr(liftN)+"\n"); } private String ArrToStr(Integer[] liftN) { String s=""; for (int i=0 ;i<5 ;i++){ s += liftN[i].toString()+"; "; } return s; }} /* * To change this template, choose Tools | Templates * and open the template in the editor. *//* * Main.java * * Created on 2010-3-16, 16:28:45 */package lift;/** * * @author mickyfeng */public class Main extends javax.swing.JFrame { /** Creates new form Main */ public Main() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1); jButton1.setText("Start"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(46, 46, 46) .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(39, 39, 39) .addComponent(jButton1)) .addGroup(layout.createSequentialGroup() .addGap(25, 25, 25) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(22, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: Integer[] liftN ={17,26,20,19,31}; while(notInCondition(liftN)){ JustDown(liftN); JustUp(liftN); } jTextArea1.append(ArrToStr(liftN)+"\n"); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Main().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; // End of variables declaration private boolean notInCondition(Integer[] liftN) { String ShowStr ="Check Num :"; for(int i=0;i<5;i++){ ShowStr = ShowStr+ liftN[i]+"; "; if (( liftN[i]<21)|(liftN[i]>25 )) { jTextArea1.append(ShowStr+"\n"); return true; } } jTextArea1.append(ShowStr+"\n"); return false; } private void JustDown(Integer[] liftN) { int Max1=0,Max2=0; jTextArea1.append("Just Down Before :"+ArrToStr(liftN)+"\n"); for(int i= 1;i<5;i++){ if (liftN[Max1] <liftN[i]){ Max2 = Max1; Max1 =i; }else{ if (liftN[Max2] <liftN[i]){ Max2 = i; } } } if ((liftN[Max1]>25)|(liftN[Max2]>25)) { liftN[Max1]-=13; liftN[Max2]-=13; } jTextArea1.append("Just Down After :"+ArrToStr(liftN)+"\n"); } private void JustUp(Integer[] liftN) { int Min1=0,Min2=0; jTextArea1.append("Just Up Before :"+ArrToStr(liftN)+"\n"); for(int i= 1;i<5;i++){ if (liftN[Min1] >liftN[i]){ Min2 = Min1; Min1 =i; }else{ if (liftN[Min2] >liftN[i]){ Min2 = i; } } } if((liftN[Min1]<21)|liftN[Min2]<21){ liftN[Min1]+=8; liftN[Min2]+=8; } jTextArea1.append("Just Up After :"+ArrToStr(liftN)+"\n"); } private String ArrToStr(Integer[] liftN) { String s=""; for (int i=0 ;i<5 ;i++){ s += liftN[i].toString()+"; "; } return s; }}修改了一下 那么长的代码,放眼过去都是gui部分的代码... ... spring连接池的问题 怎么根据list一个值对这个list重新排序 Linux(Fedora7)自带的JDK为何不支持Java泛型编程? String... args 含义是什么呢 超级求助 渥太华大学一年级java问题 加油 加油 问一个较难的问题,高分!!! 大家开发java程序都用什么工具啊? 一个不是问题的问题:请各位楼主帮忙,送分: 有关方法的复盖与重载的问题 请教:运行时候建立控件的问题 最近编程遇到的JDBC很怪异问题,不奇怪我给你打钱。 循环练习题,想不出右边部分怎么实现,发帖求指点。
* To change this template, choose Tools | Templates
* and open the template in the editor.
*//*
* Main.java
*
* Created on 2010-3-16, 16:28:45
*/package lift;/**
*
* @author mickyfeng
*/
public class Main extends javax.swing.JFrame { /** Creates new form Main */
public Main() {
initComponents();
} /** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1); jButton1.setText("Start");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
}); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(39, 39, 39)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(22, Short.MAX_VALUE))
); pack();
}// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Integer[] liftN ={17,26,20,19,31};
while(notInCondition(liftN)){
JustDown(liftN);
JustUp(liftN);
}
jTextArea1.append(ArrToStr(liftN)+"\n");
} /**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
} // Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration private boolean notInCondition(Integer[] liftN) {
String ShowStr ="Check Num :";
for(int i=0;i<5;i++){
ShowStr = ShowStr+ liftN[i]+"; ";
if (( liftN[i]<21)|(liftN[i]>25 )) {
jTextArea1.append(ShowStr+"\n");
return true;
}
}
jTextArea1.append(ShowStr+"\n");
return false;
} private void JustDown(Integer[] liftN) {
int Max1=0,Max2=0;
jTextArea1.append("Just Down Before :"+ArrToStr(liftN)+"\n");
for(int i= 1;i<5;i++){
if (liftN[Max1] <liftN[i]){
Max1 =i;
}else{
if (liftN[Max2] <liftN[i]){
Max2 = i;
}
}
}
if ((liftN[Max1]>25)|(liftN[Max2]>25)) {
liftN[Max1]-=13;
liftN[Max2]-=13;
}
jTextArea1.append("Just Down After :"+ArrToStr(liftN)+"\n");
} private void JustUp(Integer[] liftN) {
int Min1=0,Min2=0;
jTextArea1.append("Just Up Before :"+ArrToStr(liftN)+"\n");
for(int i= 1;i<5;i++){
if (liftN[Min1] >liftN[i]){
Min1 =i;
}else{
if (liftN[Min2] >liftN[i]){
Min2 = i;
}
}
}
if((liftN[Min1]<21)|liftN[Min2]<21){
liftN[Min1]+=8;
liftN[Min2]+=8;
}
jTextArea1.append("Just Up After :"+ArrToStr(liftN)+"\n");
} private String ArrToStr(Integer[] liftN) {
String s="";
for (int i=0 ;i<5 ;i++){
s += liftN[i].toString()+"; ";
}
return s;
}}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*//*
* Main.java
*
* Created on 2010-3-16, 16:28:45
*/package lift;/**
*
* @author mickyfeng
*/
public class Main extends javax.swing.JFrame { /** Creates new form Main */
public Main() {
initComponents();
} /** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1); jButton1.setText("Start");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
}); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(39, 39, 39)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(22, Short.MAX_VALUE))
); pack();
}// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Integer[] liftN ={17,26,20,19,31};
while(notInCondition(liftN)){
JustDown(liftN);
JustUp(liftN);
}
jTextArea1.append(ArrToStr(liftN)+"\n");
} /**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
} // Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration private boolean notInCondition(Integer[] liftN) {
String ShowStr ="Check Num :";
for(int i=0;i<5;i++){
ShowStr = ShowStr+ liftN[i]+"; ";
if (( liftN[i]<21)|(liftN[i]>25 )) {
jTextArea1.append(ShowStr+"\n");
return true;
}
}
jTextArea1.append(ShowStr+"\n");
return false;
} private void JustDown(Integer[] liftN) {
int Max1=0,Max2=0;
jTextArea1.append("Just Down Before :"+ArrToStr(liftN)+"\n");
for(int i= 1;i<5;i++){
if (liftN[Max1] <liftN[i]){
Max2 = Max1;
Max1 =i;
}else{
if (liftN[Max2] <liftN[i]){
Max2 = i;
}
}
}
if ((liftN[Max1]>25)|(liftN[Max2]>25)) {
liftN[Max1]-=13;
liftN[Max2]-=13;
}
jTextArea1.append("Just Down After :"+ArrToStr(liftN)+"\n");
} private void JustUp(Integer[] liftN) {
int Min1=0,Min2=0;
jTextArea1.append("Just Up Before :"+ArrToStr(liftN)+"\n");
for(int i= 1;i<5;i++){
if (liftN[Min1] >liftN[i]){
Min2 = Min1;
Min1 =i;
}else{
if (liftN[Min2] >liftN[i]){
Min2 = i;
}
}
}
if((liftN[Min1]<21)|liftN[Min2]<21){
liftN[Min1]+=8;
liftN[Min2]+=8;
}
jTextArea1.append("Just Up After :"+ArrToStr(liftN)+"\n");
} private String ArrToStr(Integer[] liftN) {
String s="";
for (int i=0 ;i<5 ;i++){
s += liftN[i].toString()+"; ";
}
return s;
}}修改了一下