您的位置:首页 > 编程语言 > Java开发

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

2012-07-04 11:01 211 查看
ABCDE五人安排工作日程,每人每星期工作5天休息2天

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

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

3) A、C星期三必须上班

4) B、D、E星期天都不上班

5) A、C一星期至少见4次

6) A、B、C、D中每天必须至少有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
=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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: