日期:2014-05-20 浏览次数:20722 次
package com; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; public class PassRiver { /** * set 1 is replace of human 0 is replace of ghost */ String a = "1", b = "1", c = "1", d = "0", e = "0", f = "0"; static Map map = getMap(); public void topassriver() { String[] a = getData(); /** * 这种嵌套循环有点象排序算法不符合要求继续 */ for (int i = 0; i < a.length - 1; i++) { for (int j = i; j < a.length; j++) { takeBoat(a[i], a[j]); } } } /** * 类似递归 */ int i = 0; public void topassriver2() { String[] a = getData(); for (int j = i; j < a.length; j++) { takeBoat(a[i], a[j]); } if (i++ < a.length - 1) { topassriver2(); } } public static void main(String[] args) { PassRiver passRiver = new PassRiver(); passRiver.topassriver2(); } /** * tabe boat to pass river * * @param a * human or ghost * @param b * human or ghost */ public static void takeBoat(String a, String b) { if (map.get(a).equals(map.get(b))) { if (map.get(a).equals("1")) { // System.out.println(a + " and " + b + " is man" + ""); } if (map.get(a).equals("0")) { // System.out.println(a + " and " + b + " is ghost"); } } else { // a or b one is human and the other is ghost System.out.println(a + " and " + b + " is can take boat" + ""); } } public static String[] getData() { String[] a = new String[6]; int k = 0; // if two human or two ghost is boat Iterator iterator = map.keySet().iterator(); while (iterator.hasNext()) { a[k] = (String) iterator.next(); k++; } return a; } /** * * @return */ public Map toPass() { return null; } public xt(lin
------解决方案--------------------
我的思路和想法是这样的啊
package com;
import java.util.*;
public class PassRiver {
/**
* set 1 is replace of human 0 is replace of ghost
*/
String a = "1 ", b = "1 ", c = "1 ", d = "0 ", e = "0 ", f = "0 ";
static Map map = getMap();
public void topassriver() {
String[] a = getData();
/**
* 这种嵌套循环有点象排序算法不符合要求继续
*/
for (int i = 0; i < a.length - 1; i++) {
for (int j = i; j < a.length; j++) {
takeBoat(a[i], a[j]);
}
}
}
/**
* 类似递归
*/
int i = 0;
public void topassriver2() {
String[] a = getData();
for (int j = i; j < a.length; j++) {
takeBoat(a[i], a[j]);
}
if (i++ < a.length - 1) {
topassriver2();
}