第三届蓝桥杯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
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
相关文章推荐
- 蓝桥杯 五角填数 2015年JavaB组决赛试题
- 蓝桥杯java第三届决赛第一题--星期日
- 蓝桥杯java第三届决赛第四题--DNA比对
- 2013第四届蓝桥杯决赛Java高职高专组题目以及解法答案
- 第六届蓝桥杯省赛(java)C组试题及参考答案
- 2018 蓝桥杯Java B组试题及答案
- 第三届蓝桥杯Java高职组决赛第三题
- 蓝桥杯第六届决赛试题JavaB
- 第三届全国ITAT教育工程就业技能大赛决赛试题----Java程序设
- 蓝桥杯 六角幻方 2014年JavaB组决赛试题
- 黄金队列,汉诺塔,奇怪的比赛,第一个数字,割圆,最大数,矩形的交集和并集,趣味算式(第三届蓝桥杯Java本科预赛试题)
- 第三届蓝桥杯Java高职组决赛第一题
- 第三届全国ITAT教育工程就业技能大赛复赛试题 Java程序设计(B卷)答案
- 2011蓝桥杯java试题及部分参考答案
- 分机号 2015蓝桥杯JavaB组决赛试题
- 第三届蓝桥杯预选赛试题及答案
- 第三届软件大赛决赛试题及答案
- 2011年 蓝桥杯 预赛 Java本科A组 试题和答案
- [蓝桥杯]第三届Java高职组决赛第四题
- 第五届蓝桥杯java程序设计 c组决赛试题 年龄巧合