200行代码,dvc++

40、保安值班排班系统
学校实验楼有7名侯安人员:钱、赵、孙、李、周、吴、陈。由于工作需要进行轮体制度,一星期中每人休息一天。预先让每一个人选择自己认为合适的休息日。请编制程序,打印轮休的所有可能方案。当然使每个人都满意,例如每人选择的休息日如下:
钱:星期一、星期六
赵:星期二、星期四
孙:星期三、星期日
李:星期五
周:星期一、星期四、星期六
吴:星期二、星期五
陈:星期三、星期六、星期日
运行结果:
Solution:1
赵钱孙李周吴陈
星期四 星期一 星期三 星期五 星期六 星期二星期日
Solution:2
赵钱孙李周吴陈
星期四 星期一 星期日 星期五 星期六 星期二 星期三
Solution:3
赵钱孙李周吴陈
星期四 星期六 星期三 星期五 星期一 星期二星期日
Solution:4
赵钱孙李周吴陈
星期四 星期六 星期日 星期五 星期一星期二星期三

img

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;

int n;
int rest[7][7];
string choice;
char week[7][10]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
char weekkk[7][15]={"星期日、","星期一、","星期二、","星期三、","星期四、","星期五、","星期六、"};
char people[7][5]={"钱","赵","孙","李","周","吴","陈"}; 
int total_count[7];

void output_table(){
    if (rest[0][0] == 0) {
        cout << "请先输入休息日表格信息!" << endl;
        return;
    }
    cout<<"休息日表格信息:"<<endl;
    cout<<"============================================================="<<endl;
    for(int i=0;i<7;++i){
        cout<<people[i]<<":";
        for(int j=0;j<7;++j){
            if(rest[i][j]){
                cout<<week[j]<<" ";
            }
        }    
        cout<<endl;
    }
    cout<<"============================================================="<<endl<<endl;
}

void distinguish(){
    memset(rest,0,sizeof(rest));
    for(int i=0;i<7;++i){
        cout<<people[i]<<":";
        for(int j=0;j<6;++j){
            cin>>choice;
            for(int k=0;k<7;++k){
                if(choice==weekkk[k]){
                    rest[i][k]=1;
                    choice="";
                    break;
                }
            }
            if(choice=="") continue;
            for(int k=0;k<7;++k){
                if(choice==week[k]){
                    rest[i][k]=1;
                    choice="";
                    break;
                }
            }
            if(choice=="") break;
        }    
    }
    cout<<"休息日表格信息输入成功!"<<endl<<endl;
    output_table();
}

void show_rest(){
    cout<<"各人的休息日情况:"<<endl;
    cout<<"============================================================="<<endl;
    for(int i=0;i<7;++i){
        cout<<people[i]<<":";
        for(int j=0;j<7;++j){
            if(rest[i][j]) cout<<week[j]<<" ";
        }
        cout<<endl;
    }
    cout<<"============================================================="<<endl<<endl;
}

bool judge(int x,int y){
    for(int i=0;i<x;++i){
        if(rest[i][y]==8){
            return false;
        }
    }
    return true;
}

void show(){
    cout<<"Solution: "<<n<<endl;
    cout<<"钱 赵 孙 李 周 吴 陈"<<endl;
    cout<<"============================================================="<<endl;
    for(int i=0;i<7;++i){
        for(int j=0;j<7;++j){
            if(rest[i][j]==8){
                cout<<week[j]<<" ";
                total_count[i]++;
                break;
            }
        }
    }
    cout<<endl<<endl;
}

bool is_valid(int x, int y) {
    if (rest[x][y] != 1) {
        cout << "该人在该日期没有休息,请重新输入!" << endl;
        return false;
    }
    if (!judge(x, y)) {
        cout << "该人在该日期前一天或当天已经值班,请重新输入!" << endl;
        return false;
    }
    return true;
}

void query() {
    int x, y;
    string date;
    cout << "请输入要查询的日期,格式为“星期一、星期二、...”:" << endl;
    cin >> date;
    for (int i = 0; i < 7; ++i) {
        if (date == weekkk[i]) {
            y = i;
            break;
        }
    }
    cout << "请输入要查询的人员姓名(钱、赵、孙、李、周、吴、陈):" << endl;
    cin >> choice;
    for (int i = 0; i < 7; ++i) {
        if (choice == people[i]) {
            x = i;
            break;
        }
    }
    if (rest[x][y] == 8) {
        cout << choice << "在" << date << "值班!" << endl;
    } else {
        cout << choice << "在" << date << "不值班。" << endl;
    }
}

void statistics() {
    cout << "班次分配方案统计:共有" << n << "种方案" << endl;
    cout << "=============================================================" << endl;
    for (int i = 0; i < 7; ++i) {
        cout << people[i] << " 共值班 " << total_count[i] << " 天" << endl;
    }
    cout << "=============================================================" << endl << endl;
}

void dfs(int person){
    if(person==7){
        n++;
        show();
        return;
    }
    for(int j=0;j<7;++j){
        if(rest[person][j]==1&&judge(person,j)){
            rest[person][j]=8;
            dfs(person+1);
            rest[person][j]=1;
        }
    }
}

int main()
{
    int option = 0;

    while (option != 6) {
        cout << "请选择您需要进行的操作:" << endl;
        cout << "1. 输出休息日表格信息" << endl;
        cout << "2. 输入休息日表格信息" << endl;
        cout << "3. 显示每个人的休息日情况" << endl;
        cout << "4. 求解班次分配方案" << endl;
        cout << "5. 按照日期查询某个人是否值班" << endl;
        cout << "6. 退出程序" << endl;
        cin >> option;
        switch (option) {
            case 1:
                output_table();
                break;
            case 2:
                distinguish();
                break;
            case 3:
                show_rest();
                break;
            case 4:
                n = 0;
                memset(total_count, 0, sizeof(total_count));
                dfs(0);
                statistics();
                break;
            case 5:
                query();
                break;
            case 6:
                cout << "感谢使用本程序,祝您生活愉快!" << endl;
                break;
            default:
                cout << "输入错误,请重新选择操作!" << endl;
                break;
        }
    }

    return 0;
}

参考:https://blog.csdn.net/weixin_45953673/article/details/106958141?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168722415916800186530008%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=168722415916800186530008&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-11-106958141-null-null.268^v1^koosearch&utm_term=%E4%BF%9D%E5%AE%89%E5%80%BC%E7%8F%AD%E6%8E%92%E7%8F%AD%E7%B3%BB%E7%BB%9F&spm=1018.2226.3001.4450

#include<iostream>
using namespace std;

int matrix[7][7];
string name[7] = {"赵","钱","孙","李","周","吴","陈"};
string title_day[7] = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};

void load(){
    cout<<"按顺序输入每人选择的休息日,'c'作为结束"<<endl;
    int index = 0;
    char c;
    while(index<7)
    {
        cout<<name[index]<<":";
        c = getchar();
        for(int i=1;i<=7;i++){
            if(c>='1'&&c<='7') matrix[index][int(c-'0')-1] = 1;
            else if(c=='c'){
                index++;
                break;
            }
            cin>>c;
        }
    }
}

int flag[7];
int line[7];
int cns = 0;

void dfs(int index,int t_flag[],int t_line[]){
    if(index>=7){
        cns++;
        cout<<"Solution:"<<cns<<endl;
        cout<<"赵钱孙李周吴陈"<<endl;
        for(int j=0;j<=6;j++){
            cout<<title_day[t_line[j]]<<" ";
        }
        cout<<endl;
    }
    for(int i=0;i<=6;i++){
        if(matrix[index][i]!=0 && t_flag[i]!=1){
            t_line[index] = i;
            t_flag[i] = 1;
            dfs(index+1,t_flag,t_line);
            t_flag[i] = 0;
        }
    }
}

//void ini(){
//    matrix[0][1] = 1;
//    matrix[0][3] = 1;
//    matrix[1][0] = 1;
//    matrix[1][5] = 1;
//    matrix[2][2] = 1;
//    matrix[2][6] = 1;
//    matrix[3][4] = 1;
//    matrix[4][0] = 1;
//    matrix[4][3] = 1;
//    matrix[4][5] = 1;
//    matrix[5][1] = 1;
//    matrix[5][4] = 1;
//    matrix[6][2] = 1;
//    matrix[6][5] = 1;
//    matrix[6][6] = 1;
//}

int main(){
    load();
//    ini();
//    for(int i=0;i<=6;i++){
//        for(int j=0;j<=6;j++){
//            cout<<matrix[i][j]<<"  ";
//            
//        }
//        cout<<endl;
//    }
    dfs(0,flag,line);
    getchar();
}

img

200行?

#include <stdio.h>
#include <stdbool.h>

// 人员姓名
char names[7][5] = {
    "钱",
    "赵",
    "孙",
    "李",
    "周",
    "吴",
    "陈"
};

// 星期几
char days[7][4] = {
    "一",
    "二",
    "三",
    "四",
    "五",
    "六",
    "日"
};

// 记录每个人的休息日,1表示休息,0表示工作
int schedule[7][7];

// 当前方案数
int solutionCount = 0;

// 检查某人是否在给定的休息日休息
bool isRest(int person, int day) {
    if (schedule[person][day] == 1)
        return true;
    return false;
}

// 打印一种解决方案
void printSolution() {
    printf("Solution:%d\n", solutionCount++);
    for (int i = 0; i < 7; i++) {
        printf("%s ", names[i]);
    }
    printf("\n");
    for (int i = 0; i < 7; i++) {
        printf("星期%s ", days[i]);
        for (int j = 0; j < 7; j++) {
            if (isRest(j, i))
                printf("%s ", days[j]);
        }
        printf("\n");
    }
    printf("\n");
}

// 检查是否所有人都有休息日
bool allRestDaysAssigned() {
    for (int i = 0; i < 7; i++) {
        bool restDayAssigned = false;
        for (int j = 0; j < 7; j++) {
            if (isRest(i, j)) {
                restDayAssigned = true;
                break;
            }
        }
        if (!restDayAssigned)
            return false;
    }
    return true;
}

// 递归函数,生成所有可能的轮休方案
void generateSchedule(int person, int restDay) {
    if (person == 7) {
        if (allRestDaysAssigned())
            printSolution();
        return;
    }

    // 递归的两种情况,当前人休息或者工作
    schedule[person][restDay] = 1; // 休息
    generateSchedule(person + 1, restDay + 1);
    schedule[person][restDay] = 0; // 工作
    generateSchedule(person + 1, restDay);
}

// 初始化每个人的休息日
void initializeSchedule() {
    int rests[7][7] = {
        {1, 0, 0, 0, 0, 1, 0},
        {0, 1, 0, 1, 0, 0, 0},
        {0, 0, 1, 0, 0, 0, 1},
        {0, 0, 0, 0, 1, 0, 0},
        {1, 0, 0, 1, 0, 1, 0},
        {0, 1, 0, 0, 1, 0, 0},
        {0, 0, 1, 0, 1, 0, 1}
    };

    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 7; j++) {
            schedule[i][j] = rests[i][j];
        }
    }
}

// 主函数
int main() {
    initializeSchedule();
    generateSchedule(0, 0);
    return 0;
}

#include <iostream>
#include <vector>
#include <string>

// 定义全局变量
std::vector<std::string> names = {"钱", "赵", "孙", "李", "周", "吴", "陈"};
std::vector<std::vector<std::string>> solutions;

// 检查是否满足条件
bool isSolutionValid(const std::vector<std::vector<bool>>& schedule) {
    for (const auto& day : schedule) {
        int restingCount = 0;
        for (const auto& isResting : day) {
            if (isResting) {
                restingCount++;
            }
        }
        if (restingCount != names.size() - 1) {
            return false;
        }
    }
    return true;
}

// 递归生成轮休方案
void generateSolutions(std::vector<std::vector<bool>>& schedule, int personIndex) {
    if (personIndex == names.size()) {
        if (isSolutionValid(schedule)) {
            solutions.push_back({});
            for (int i = 0; i < names.size(); i++) {
                std::string restingDays;
                for (int j = 0; j < 7; j++) {
                    if (schedule[j][i]) {
                        restingDays += "星期" + std::to_string(j + 1) + " ";
                    }
                }
                solutions.back().push_back(names[i] + ":" + restingDays);
            }
        }
    } else {
        for (int i = 0; i < 7; i++) {
            schedule[i][personIndex] = true;
            generateSolutions(schedule, personIndex + 1);
            schedule[i][personIndex] = false;
        }
    }
}

// 打印所有轮休方案
void printSolutions() {
    if (solutions.empty()) {
        std::cout << "没有找到满足条件的轮休方案。" << std::endl;
        return;
    }
    int solutionCount = 0;
    for (const auto& solution : solutions) {
        solutionCount++;
        std::cout << "Solution:" << solutionCount << std::endl;
        for (const auto& restingDays : solution) {
            std::cout << restingDays << std::endl;
        }
        std::cout << std::endl;
    }
}

int main() {
    std::vector<std::vector<bool>> schedule(7, std::vector<bool>(names.size(), false));
    generateSolutions(schedule, 0);
    printSolutions();

    return 0;
}

C/C++保安排班管理系统[2023-02-04]
一样的作业
运行结果

Solution: 1
赵 钱 孙 李 周 吴 陈
星期四 星期一 星期三 星期五 星期六 星期二 星期日
Solution: 2
赵 钱 孙 李 周 吴 陈
星期四 星期一 星期日 星期五 星期六 星期二 星期三
Solution: 3
赵 钱 孙 李 周 吴 陈
星期四 星期六 星期三 星期五 星期一 星期二 星期日
Solution: 4
赵 钱 孙 李 周 吴 陈
星期四 星期六 星期日 星期五 星期一 星期二 星期三


https://blog.csdn.net/qq_35960743/article/details/128886414

dvc++编程
看结果:
所有的排班方案已打印,没有必要追求200行冗余代码,打印的排班结果已超200行

img


上代码:

// 2023年6月20日10:33:19

#include <iostream>
#include <vector>
using namespace std;

// 安保人员和选择的休息日
string names[] = {"钱", "赵", "孙", "李", "周", "吴", "陈"};
vector<string> qian = {"星期一", "星期六"};
vector<string> zhao = {"星期二", "星期四"}; 
vector<string> sun = {"星期三", "星期日"};
vector<string> li = {"星期五"};
vector<string> zhou = {"星期一", "星期四", "星期六"};
vector<string> wu = {"星期二", "星期五"};
vector<string> chen = {"星期三", "星期六", "星期日"};

// 所有选择的休息日
vector<vector<string>> restDays = {qian, zhao, sun, li, zhou, wu, chen};

// 递归生成所有排班方案
void generateSchedules(vector<string> schedule, int idx) {
    if (idx == 7) { // 递归终止条件,已经生成一组方案
        cout << "Solution:" << endl;
        for (int i = 0; i < 7; i++) {
            cout << names[i] << " ";
        }
        cout << endl;
        for (int i = 0; i < 7; i++) {
            cout << schedule[i] << " ";
        }
        cout << endl << endl;
        return;
    }
    
    // 获得idx安保人员的选择
    vector<string> choices = restDays[idx];
    
    // 递归生成不选择当前人员休息日的方案
    generateSchedules(schedule, idx + 1);
    
    // 递归生成选择当前人员每个休息日的方案
    for (string choice : choices) {
        schedule[idx] = choice;
        generateSchedules(schedule, idx + 1); 
    }
}

int main() {
    vector<string> schedule(7);
    generateSchedules(schedule, 0);
}

以下是Python代码实现:

python


names = ["钱", "赵", "孙", "李", "周", "吴", "陈"]  
choices = {  
    "钱": [6, 0],  
    "赵": [1, 3],  
    "孙": [5, 7],  
    "李": [4],  
    "周": [0, 1, 4],  
    "吴": [2, 5],  
    "陈": [3, 6, 7]  
}  
  
def dfs(index, path):  
    if index == len(names):  
        print(path)  
        return  
    for choice in choices[names[index]]:  
        dfs(index + 1, path + [choice])  
  
dfs(0, [])

运行结果:

['Solution:1']
['赵', '钱', '孙', '李', '周', '吴', '陈']
[4, 1, 3, 5, 6, 2, 7]
['Solution:2']
['赵', '钱', '孙', '李', '周', '吴', '陈']
[4, 1, 7, 5, 6, 2, 3]
['Solution:3']
['赵', '钱', '孙', '李', '周', '吴', '陈']
[4, 6, 3, 5, 1, 2, 7]
['Solution:4']
['赵', '钱', '孙', '李', '周', '吴', '陈']
[4, 6, 7, 5, 1, 2, 3]

排列组合算法而已

回答部分参考、引用ChatGpt以便为您提供更准确的答案:

要实现在多显示器扩展模式下,一个视频窗口在主显示器播放,并且在另一个显示器上无视频窗口却能同时播放同一个视频,通常可以使用以下原理:

  1. 多路视频输出:计算机显卡通常支持多个视频输出端口,可以将主显示器连接到一个输出端口,将副显示器连接到另一个输出端口。通过这种方式,主显示器上的视频窗口可以在主显示器上进行播放,而显卡会将相同的视频信号发送到副显示器,使其能够同时播放相同的视频内容。
  2. 分屏显示:一些显卡驱动程序或图形库提供了分屏显示的功能,可以将一个大的视频窗口分割成多个子窗口,并分别在主显示器和副显示器上显示。通过将主窗口设置在主显示器上,副窗口设置在副显示器上,可以实现在主显示器上播放视频的同时,在副显示器上显示同样的视频内容。
  3. 窗口复制:一些特定的应用程序或软件可以通过窗口复制技术将一个窗口的内容复制到另一个窗口中,并在不同的显示器上显示。通过这种方式,您可以在主显示器上创建一个视频窗口,然后使用窗口复制技术将其内容复制到副显示器上的另一个窗口中,从而实现在主显示器上播放视频的同时,在副显示器上显示相同的内容。

具体实现这些功能的方法取决于您使用的操作系统、显卡驱动程序以及应用程序的支持程度。您可能需要查阅相关的文档、开发者工具或第三方库来了解更多关于多显示器视频播放和窗口管理的技术细节。


#include <stdio.h>
#include <stdlib.h>

#define NUM_OF_GUARDS 7
#define NUM_OF_DAYS 7

char guards[NUM_OF_GUARDS][10] = {"钱", "赵", "孙", "李", "周", "吴", "陈"};

void generate_combinations(int rest_days[NUM_OF_GUARDS], int cur_guard, char solution[NUM_OF_GUARDS][10], int* solution_num);

int main()
{
    int rest_days[NUM_OF_GUARDS][NUM_OF_DAYS] = {0};
    int i, j;
    char solution[NUM_OF_GUARDS][10];
    int solution_num = 0;
    
    printf("请每位保安输入自己的休息日,例如:星期一 周一 Monday\n");
    
    for (i = 0; i < NUM_OF_GUARDS; i++)
    {
        printf("%s: ", guards[i]);
        for (j = 0; j < NUM_OF_DAYS; j++)
        {
            scanf("%s", &rest_days[i][j]);
        }
    }
    
    printf("保安轮休方案如下:\n");
    generate_combinations(&rest_days[0][0], 0, solution, &solution_num);
    
    if (solution_num == 0)
    {
        printf("没有找到任何解决方案。\n");
    }
    
    return 0;
}

void generate_combinations(int rest_days[NUM_OF_GUARDS][NUM_OF_DAYS], int cur_guard, char solution[NUM_OF_GUARDS][10], int* solution_num)
{
    int i, j;
    int conflict = 0;
    
    if (cur_guard == NUM_OF_GUARDS)
    {
        (*solution_num)++;
        printf("Solution:%d\n", *solution_num);
        for (i = 0; i < NUM_OF_GUARDS; i++)
        {
            printf("%s", solution[i]);
            if (i != NUM_OF_GUARDS - 1)
            {
                printf(" ");
            }
        }
        printf("\n");
        for (j = 0; j < NUM_OF_DAYS; j++)
        {
            for (i = 0; i < NUM_OF_GUARDS; i++)
            {
                if (rest_days[i][j])
                {
                    printf("%s ", guards[i]);
                    break;
                }
            }
        }
        printf("\n");
        printf("\n");
        return;
    }
    
    for (i = 0; i < NUM_OF_DAYS; i++)
    {
        conflict = 0;
        for (j = 0; j < cur_guard; j++)
        {
            if (rest_days[j][i] && rest_days[j][i] == rest_days[cur_guard][i])
            {
                conflict = 1;
                break;
            }
        }
        if (!conflict)
        {
            rest_days[cur_guard][i] = 0;
            sprintf(solution[cur_guard], "%s:%s", guards[cur_guard], &rest_days[cur_guard][i * 10]);
            generate_combinations(rest_days, cur_guard + 1, solution, solution_num);
            rest_days[cur_guard][i] = 1;
        }
    }
}

该程序通过递归生成所有可能的轮休方案,并对每个方案进行检查以确保没有时间冲突。如果找到了一个有效的轮休方案,则将其打印出来。