当前位置: 代码迷 >> J2SE >> javaSE小程序,该如何解决
  详细解决方案

javaSE小程序,该如何解决

热度:67   发布时间:2016-04-24 00:33:27.0
javaSE小程序
赵、钱、孙、李、周五人围着一张圆桌吃饭。饭后,周回忆说:
 * “吃饭时,赵坐在钱旁边,钱的左边是孙或李”;李回忆说:“钱坐在孙左边,我挨着孙坐”。
 * 结果他们一句也没有说对。请问,他们在怎样坐的?  
 *  
用代码怎么解决,给点算法思路

------解决方案--------------------
Java code
// 结果为:// 赵 孙 周 钱 李 // 钱 李 赵 孙 周 // 孙 周 钱 李 赵 // 李 赵 孙 周 钱 // 周 钱 李 赵 孙     public static void main(String[] args) {        //0-赵,1-钱,2-孙,3-李,4-周        int[] people = new int[5];        all_permutation(people, 0);    }        private static void all_permutation(int[] people, int level) {        if(level==5) {            boolean zhou_say = getConditionOne(people);            boolean li_say = getConditionTwo(people);            if(!zhou_say && !li_say){                print(people);            }            return;        }else {            for(int i=0; i<5; i++) {                boolean flag = true;                for(int j=0; j<level; j++) {                    if(people[j]==i) {                        flag = false;                        break;                    }                }                if(flag == false) {                    continue;                }else {                    people[level] = i;                    all_permutation(people, level+1);                }            }        }            }        //在人群中找到某个人坐的位置    private static int find_people(int[] people, String name) {        String[] names = {"赵", "钱", "孙", "李", "周"};        int i, value;        for(value=0; value<names.length; value++) {            if(names[value].equals(name)) {                break;            }        }        for(i=0; i<people.length; i++) {            if(people[i] == value) {                break;            }        }        return i;    }        //周回忆的条件    private static boolean getConditionOne(int[] people) {        int zhao_pos = find_people(people, "赵");        int qian_pos = find_people(people, "钱");        int sun_pos = find_people(people, "孙");        int li_pos = find_people(people, "李");        boolean flag = zhao_pos + 1 == qian_pos ||                        zhao_pos - 1 == qian_pos ||                        zhao_pos == 4 && qian_pos == 0 ||                        zhao_pos == 0 && qian_pos == 4 ||                        qian_pos - 1 == sun_pos ||                        qian_pos == 0 && sun_pos == 4 ||                        qian_pos - 1 == li_pos ||                        qian_pos == 0 && li_pos == 4 ;        return flag;    }        //李回忆说    private static boolean getConditionTwo(int[] people) {        int qian_pos = find_people(people, "钱");        int sun_pos = find_people(people, "孙");        int li_pos = find_people(people, "李");        boolean flag = qian_pos == sun_pos - 1 ||                        qian_pos == 4 && sun_pos == 0 ||                        li_pos + 1 == sun_pos ||                        li_pos - 1 == sun_pos ||                        li_pos == 4 && sun_pos == 0 ||                        li_pos == 0 && sun_pos == 4;        return flag;    }        //打印找到的结果    private static void print(int[] people) {        String[] names = {"赵", "钱", "孙", "李", "周"};        for(int i:people) {            System.out.print(names[i]+" ");        }        System.out.println();    }
------解决方案--------------------
厉害,佩服得五体投地