zoukankan      html  css  js  c++  java
  • java实现人员排日程

    某保密单位机要人员 A,B,C,D,E 每周需要工作5天,休息2天。

    上级要求每个人每周的工作日和休息日安排必须是固定的,不能在周间变更。
    此外,由于工作需要,还有如下要求:

    1. 所有人的连续工作日不能多于3天(注意:周日连到下周一也是连续)。

    2. 一周中,至少有3天所有人都是上班的。

    3. 任何一天,必须保证 A B C D 中至少有2人上班。

    4. B D E 在周日那天必须休息。

    5. A E 周三必须上班。

    6. A C 一周中必须至少有4天能见面(即同时上班)。

    你的任务是:编写程序,列出ABCDE所有可能的一周排班情况。工作日记为1,休息日记为0

    A B C D E 每人占用1行记录,从星期一开始。

    【输入、输出格式要求】
    程序没有输入,要求输出所有可能的方案。

    每个方案是7x5的矩阵。只有1和0组成。

    矩阵中的列表示星期几,从星期一开始。

    矩阵的行分别表示A,B,C,D,E的作息时间表。

    多个矩阵间用空行分隔开。

    例如,如下的矩阵就是一个合格的解。请编程输出所有解(多个解的前后顺序不重要)。

    0110111
    1101110
    0110111
    1101110
    1110110

    【注意】

    请仔细调试!您的程序只有能运行出正确结果的时候才有机会得分!

    在评卷时使用的输入数据与试卷中给出的实例数据可能是不同的。

    package com.liu.ex8;
    
    import java.util.ArrayList;
    
    
    public class Main {
        public static int[] A = {1,1,1,1,1,1,1};
        public static ArrayList<Integer> list = new ArrayList<Integer>();
        public static ArrayList<String> planList = new ArrayList<String>();
        
        public boolean check() {
            int count1 = 0;
            for(int i = 0;i < 10;i++) {
                if(A[i % 7] == 1)
                    count1++;
                else if(A[i % 7] == 0)
                    count1 = 0;
                if(count1 >= 4)
                    return false;
            }
            return true;
        }
        
        public void dfs(int step) {
            while(step < 7) {
                list.add(step);
                if(list.size() == 2) {
                    A[list.get(0)] = 0;
                    A[list.get(1)] = 0;
                    if(check()) {
                        StringBuilder s = new StringBuilder("");
                        for(int i = 0;i < 7;i++)
                            s.append(A[i]);
                        planList.add(s.toString());
                    }
                    A[list.get(0)] = 1;
                    A[list.get(1)] = 1;
                }
                step++;
                dfs(step);
                list.remove(list.size() - 1);
            }
        }
        
        public void getValue(int[][] value, int[] A) {
            for(int i = 0;i < A.length;i++) {
                String temp = planList.get(A[i]);
                for(int j = 0;j < temp.length();j++)
                    value[i][j] = temp.charAt(j) - '0';
            }
        }
        
        public boolean judge(int[][] value) {
            //判断要求2
            int count = 0;
            for(int j = 0;j < value[0].length;j++) {
                int sum = 0;
                for(int i = 0;i < value.length;i++) {
                    if(value[i][j] == 1)
                        sum = sum + 1;
                }
                if(sum == value.length)
                    count++;
            }
            if(count < 3)
                return false;
            //判断要求3
            for(int j = 0;j < value[0].length;j++) {
                int sum = 0;
                for(int i = 0;i < value.length - 1;i++) {
                    if(value[i][j] == 1)
                        sum = sum + 1;
                }
                if(sum < 2)
                    return false;
            }
            //判断要求4
            if(value[1][6] != 0 || value[3][6] != 0 || value[4][6] != 0)
                return false;
            //判断要求5
            if(value[0][2] != 1 || value[4][2] != 1)
                return false;
            //判断要求6
            count = 0;
            for(int j = 0;j < value[0].length;j++) {
                if(value[0][j] == 1 && value[2][j] == 1)
                    count++;
            }
            if(count < 4)
                return false;
            return true;
        }
        
        
        public void getResult() {
            int len = planList.size();
            int[][] value = new int[5][7];
            int[] A = new int[5];
            for(A[0] = 0;A[0] < len;A[0]++) {
                for(A[1] = 0;A[1] < len;A[1]++) {
                    for(A[2] = 0;A[2] < len;A[2]++) {
                        for(A[3] = 0;A[3] < len;A[3]++) {
                            for(A[4] = 0;A[4] < len;A[4]++) {
                                getValue(value, A);
                                if(judge(value))
                                    printResult(value);
                            }
                        }
                    }
                }
            }
            return;
        }
        
        public void printResult(int[][] value) {
            for(int i = 0;i < value.length;i++) {
                for(int j = 0;j < value[0].length;j++)
                    System.out.print(value[i][j]);
                System.out.println();
            }
            System.out.println();
        }
        
        public static void main(String[] args) {
            Main test = new Main();
            test.dfs(0); //获取满足题意要求1的一周工作日安排所有方案
            test.getResult();
        }
    }
    
  • 相关阅读:
    UV有问题?
    利用GPU实现翻页效果(分享自知乎网)
    Directx 9 VS2015环境搭建
    DirectX 读书笔记(14) Cube mapping之SkyBox[转]
    vertex shader must minimally write all four components of POSITION
    unity shader 内置变量
    Real-Time Rendering (2)
    矩阵变换:沿任意轴旋转及其推导
    Python爬虫教程-33-scrapy shell 的使用
    Python爬虫教程-32-Scrapy 爬虫框架项目 Settings.py 介绍
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13077822.html
Copyright © 2011-2022 走看看