第三届蓝桥杯java决赛试题答案

     ABCDE五人安排工作日程,每人每星期工作5天休息2

1)        必须有3天所有人都要上班

2)        每个人连续上班不超过3天,周日到周一是连续工作

3)        AC星期三必须上班

4)        BDE星期天都不上班

5)        AC一星期至少见4

6)        ABCD中每天必须至少有2人上班

输出所有从星期一到星期天可能的情况,每种情况间用空行隔开,0代表不上班,1代表上班。

例:

1 0 1 1 1 0 1

1 1 0 1 1 1 0

1 0 1 1 1 0 1

1 1 0 1 1 1 0

1 1 0 1 1 1 0

根据题目和条件2筛选出7种可能的组合:


public class Test {
 public static void main(String[] args) {
  int []a=new int [7];
  init(a);
//  print(a);
  fun(a,0);
 }
 public static void fun(int a[],int n){
  if(n==7)
   return;
  int temp[]=a.clone();
  temp[n]=0;
//  print(temp);
  if(count(a)==2&&count1(a)){
   print(a);
     }
  if(count(temp)==2&&count1(temp)){
   print(temp);
    }
  if(count(a)>2)
   return ;
  if(count(temp)>2)
   return;
  fun(a,n+1);
  fun(temp,n+1);
 }
 public static void print(int []a){
  for(int i=0;i<a.length;i++){
   System.out.print(a[i]);
  }
  System.out.println();
 }
 public static void init(int a[]){
  for(int i=0;i<a.length;i++){
   a[i]=1;
  }
 }
 public static int count(int a[]){
  int count=0;
  for(int i=0;i<a.length;i++){
   if(a[i]==0){
    count++;
   }
  }
  return count;
 }
 public static boolean count1(int a[]){
  int count=0;
  int turn=1;
  for(int i=0;i<a.length;i++){
   if(a[i]==1){
    count++;
   }
   if(a[i]==0)
    count=0;
   if(count>3)
    return false;
   if(i==6){
    i=-1;
    turn++;
   }
   if(turn>2)
    break;
  }
  return true;
 }

}
根据这7中组合再判断其余的条件


public class Test2 {

 static String []str=new String[]{
   "1110110","1101110",
   "1101101","1011101",
   "1011011","0110111",
   "0111011"
 };
 public static void main(String[] args) {
  fun();
 }
 public static void fun(){
  int k=0;
  String map[]=new String[5];
  for(int a=0;a<7;a++){
   for(int b=0;b<7;b++){
    for(int c=0;c<7;c++){
     for(int d=0;d<7;d++){
      for(int e=0;e<7;e++){
       map[0]=str[a];
       map[1]=str[b];
       map[2]=str[c];
       map[3]=str[d];
       map[4]=str[e];
       if(check(map)){
        k++;
        System.out.println("第"+k+"种方案");
        print(map);
       }
      }
     }
    }
   }
  }
  
 }
 public static void print(String map[]){
  for(int i=0;i<map.length;i++){
   System.out.println(map[i]);
  }
  System.out.println();
 }
 public static boolean check(String map[]){
  if(check1(map)&&check2(map)&&check4(map)&&check5(map)&&check6(map)){
   return true;
  }
  return false;
 }
 public static boolean check1(String map[]){
  int sum=0;
   for(int j=0;j<7;j++){
       int count=0;
       for(int i=0;i<map.length;i++){
       if(map[i].charAt(j)=='1'){
        count++;
       }
   }
       if(count==5){
        sum++;
       }
  }
   if(sum==3){
    return true;
   }
  return false;
 }
 public static boolean check2(String map[]){
  if(map[0].charAt(2)=='1'&&map[2].charAt(2)=='1'){
   return true;
  }
  return false;
 }
 public static boolean check4(String map[]){
  if(map[1].charAt(6)=='0'&&map[3].charAt(6)=='0'&&map[4].charAt(6)=='0'){
   return true;
  }
  return false;
 }public static boolean check5(String map[]){
  int count=0;
  for(int i=0;i<7;i++){
   if(map[0].charAt(i)=='1'&&map[2].charAt(i)=='1'){
    count++;
   }
  }
  if(count>=4){
   return true;
  }
  return false;
 }public static boolean check6(String map[]){
  
  for(int i=0;i<7;i++){
   int count=0;
   for(int j=0;j<4;j++){
    if(map[j].charAt(i)=='1'){
     count++;
    }
   }
   if(count<2){
    return false;
   }
  }
  return true;
 }
 
 

}

最后运行结果:

第1种方案
1011101
1110110
1011101
1110110
1110110

第2种方案
1011101
1101110
1011101
1101110
1101110

第3种方案
1011011
1110110
1011011
1110110
1110110

第4种方案
1011011
1101110
1011011
1101110
1101110

第5种方案
0110111
1101110
0110111
1101110
1110110

第6种方案
0110111
1101110
0110111
1101110
1101110

第7种方案
0111011
1110110
0111011
1110110
1110110

第8种方案
0111011
1101110
0111011
1101110
1101110

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值