java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE, JavaME, JavaSE)的总称。本站提供基于Java框架struts,spring,hibernate等的桌面应用、web交互及移动终端的开发技巧与资料

保持永久学习的心态,将成就一个优秀的你,来 继续搞起java知识。

医院有A、B、C、D、E、F、G七位大夫,在一星期内(星期一至星期天)每人要轮流值班一天。现在已知:

A大夫比C大夫晚一天值班;

D大夫比E大夫晚二天值班;

B大夫比G大夫早三天值班;

F大夫的值班日在B和C大夫的中间,且是星期四;

请用编程实现,确定每天究竟是哪位大夫值班?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.exp2.yunfei;
/*
 * A大夫比C大夫晚一天值班;
 * D大夫比E大夫晚二天值班;     
 * B大夫比G大夫早三天值班;
 * F大夫的值班日在B和C大夫的中间,且是星期四;
 */
public class DoctorsSchedule {
        public static void main (String args[]){
            int doctor[]=new int [7];//医生数组
            int i,j,k,m,n,p,r;
            for(i=1;i<=7;i++){
                doctor[0]=i;
                for(j=1;j<=7;j++){
                    doctor[1]=j;
                    for(k=1;k<=7;k++){
                        doctor[2]=k;
                        for(m=1;m<=7;m++){
                            doctor[3]=m;
                            for(n=1;n<=7;n++){
                                doctor[4]=n;
                                for(p=1;p<=7;p++){
                                    doctor[5]=p;
                                    for(r=1;r<=7;r++){
                                        doctor[6]=r;
                                        if(isNotRepeat(doctor)){
                                            if(isTrue(doctor)){
                                                output(doctor);
                                            }
                                        }
                                         
                                    }
                                }
                            }
                        }
                    }
                }
            }                      
     
        }
         
         
         
        //判断数组是否符合要求
        public static boolean isTrue(int arr[]){
            if(arr[5]==4){      //F,周四
                if((arr[2]-arr[5])==(arr[5]-arr[1])){   //F在B、C中间
                    if(arr[0]-arr[2]==1){       //A比C晚一天
                        if(arr[3]-arr[4]==2){       //D比E晚一天
                            if(arr[6]-arr[1]==3){       //B比G早三天
                                return true;
                            }
                        }
                    }
                }
                 
            }
            return false;
        }
         
        //判断数组元素是否有重复
        public static boolean isNotRepeat(int arr[]){
            for(int i=0;i<arr.length;i++){
                for(int j=0;j<arr.length;j++){
                    if(j!=i){
                        if(arr[i]==arr[j]){
                            return false;
                        }
                    }
                }
            }
            return true;
        }
         
        //输出值班表
                public static void output(int arr[]){          
                    for(int i=0;i<arr.length;i++){
                        System.out.println("医生"+(char)(&#39;A&#39;+i)+"的工作日是"+whichDay(arr[i]));                       
                    }
                    System.out.println();
                }
                //输出工作日
                public static String whichDay(int num){
                    String day="";
                    switch(num){
                    case 1:
                        day+="周一";
                        break;
                    case 2:
                        day+="周二";
                        break;
                    case 3:
                        day+="周三";
                        break;
                    case 4:
                        day+="周四";
                        break;
                    case 5:
                        day+="周五";
                        break;
                    case 6:
                        day+="周六";
                        break;
                    case 7:
                        day+="周日";
                        break;
                    default:
                        break;
                    }
                    return day;
                }
}

结果如图:

因为水平有限,难免有疏忽或者不准确的地方,希望大家能够直接指出来,我会及时改正。一切为了知识的分享。

后续会有更多的精彩的内容分享给大家。