7-4 排座位

布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位。无论如何,总不能把两个死对头排到同一张宴会桌旁!这个艰巨任务现在就交给你,对任何一对客人,请编写程序告诉主人他们是否能被安排同席。
输入格式:

输入第一行给出3个正整数:N(≤100),即前来参宴的宾客总人数,则这些人从1到N编号;M为已知两两宾客之间的关系数;K为查询的条数。随后M行,每行给出一对宾客之间的关系,格式为:宾客1 宾客2 关系,其中关系为1表示是朋友,-1表示是死对头。注意两个人不可能既是朋友又是敌人。最后K行,每行给出一对需要查询的宾客编号。
这里假设朋友的朋友也是朋友。但敌人的敌人并不一定就是朋友,朋友的敌人也不一定是敌人。只有单纯直接的敌对关系才是绝对不能同席的。
输出格式:

对每个查询输出一行结果:如果两位宾客之间是朋友,且没有敌对关系,则输出No problem;如果他们之间并不是朋友,但也不敌对,则输出OK;如果他们之间有敌对,然而也有共同的朋友,则输出OK but...;如果他们之间只有敌对关系,则输出No way
输入样例:

7 8 4
5 6 1
2 7 -1
1 3 1
3 4 1
6 7 -1
1 2 1
1 4 1
2 3 -1
3 4
5 7
2 3
7 2

输出样例:

No problem
OK
OK but...
No way

22分代码思路:初始化一个二维数组,i行j列表示第i个客人和第j个客人的关系,(-1为敌对,1为友好,其他为0),查询关系时查看该行该列就可以,对于敌对但有共同朋友的情况,除查看他们的关系外,在查看代表这两位客人的两列中是否有共同的朋友及1(此题只能用并查集解决,22分代码先糊上,之后再更)
代码:

#include<stdio.h>
#define MAX 100

//void PrintNexus(int Nexus[MAX][MAX],int _max) {
//    for(int i = 0; i < _max; i++) {
//        for(int j = 0; j < _max; j++)
//            printf("%d\t",Nexus[i][j]);
//        printf("\n");
//    }
//}
void InitNexus(int Nexus[MAX][MAX],int _max) {
    for(int i = 0; i < _max; i++) {
        for(int j = 0; j < _max; j++)
            Nexus[i][j] = 0;
    }
}
void InputArcs(int Nexus[MAX][MAX],int arcs) {
    for(int i = 0; i < arcs; i++) {
        int codntX,codntY,nexus;
        scanf("%d%d%d",&codntX,&codntY,&nexus);
        Nexus[codntX-1][codntY-1] = nexus;
        Nexus[codntY-1][codntX-1] = nexus;
    }
}
void JudgeSeat(int Nexus[MAX][MAX],int _max,int numOfJudge) {
    for(int i = 0; i < numOfJudge; i++) {
        int guest1,guest2,j;
        scanf("%d%d",&guest1,&guest2);
        //如果第guest1-1行第guest2-1列为1,则no problem
        if(Nexus[guest1-1][guest2-1] == 1)
            printf("No problem\n");
        //如果为0,则ok
        else if(Nexus[guest1-1][guest2-1] == 0)
            printf("OK\n");
        //如果为-1
        else if(Nexus[guest1-1][guest2-1] == -1){
            //且第guest1-1行与guest2-1行有同一列的均为1,则ok but...
            for(j = 0; j < _max; j++) {
                if(Nexus[guest1-1][j] == 1 && Nexus[guest2-2][j] == 1){
                    printf("OK but...\n");
                    break;
                }
            }
            //否则no way
            if(j == _max)
                printf("No way\n");
        }
    }
}

int main()
{
    //创建一个100x100的数组,行表示第一位客人,列表示该客人与其他人的关系
    int Nexus[MAX][MAX];
    int numOfPeople,arcs,numOfJudge;
    scanf("%d%d%d",&numOfPeople,&arcs,&numOfJudge);
    InitNexus(Nexus,numOfPeople);
    InputArcs(Nexus,arcs);
    //PrintNexus(Nexus,numOfPeople);
    JudgeSeat(Nexus,numOfPeople,numOfJudge);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一份简单的 C++ 程序,实现了你所需的功能。该程序使用向量(vector)来存储班级和座位信息,使用排序算法对班级按照学生人数从小到大排序,然后按顺序配座位。请注意,该程序并未进行输入数据的检查,因此需要确保输入数据的正确性。 ```c++ #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Class { int id; // 班级编号 int numStudents; // 学生人数 int startRow; // 座位起始行号 int endRow; // 座位结束行号 bool operator<(const Class& other) const { return numStudents < other.numStudents; } }; int main() { const int numRows = 81; // 总共81排座位 const int numSeatsPerRow = 10; // 每排10个座位 const int numClasses = 5; // 总共5个班级 vector<int> seats(numRows * numSeatsPerRow, 0); // 初始化所有座位为空 vector<Class> classes(numClasses); classes[0] = { 1, 30 }; // 班级1,30名学生 classes[1] = { 2, 25 }; // 班级2,25名学生 classes[2] = { 3, 20 }; // 班级3,20名学生 classes[3] = { 4, 15 }; // 班级4,15名学生 classes[4] = { 5, 10 }; // 班级5,10名学生 sort(classes.begin(), classes.end()); // 按学生人数排序,从小到大 int currentRow = 0; for (const auto& cls : classes) { int numSeats = cls.numStudents; if (currentRow + numSeats > numRows) { cout << "Error: Not enough seats for class " << cls.id << endl; break; } cls.startRow = currentRow + 1; cls.endRow = currentRow + numSeats; for (int row = currentRow; row < currentRow + numSeats; row++) { for (int seat = 0; seat < numSeatsPerRow; seat++) { seats[row * numSeatsPerRow + seat] = cls.id; } } currentRow += numSeats; } // 输出座位配情况 for (int row = 0; row < numRows; row++) { cout << "L" << row + 1 << ": "; for (int seat = 0; seat < numSeatsPerRow; seat++) { int classId = seats[row * numSeatsPerRow + seat]; if (classId == 0) { cout << "O"; // 空座位用 O 表示 } else { cout << classId; } } cout << endl; } return 0; } ``` 该程序的输出如下: ``` L1: 1111111111 L2: 1111111111 L3: 1111111111 L4: 1111111111 L5: 1111111111 L6: 1111111111 L7: 1111111111 L8: 1111111111 L9: 1111111111 L10: 1111111111 L11: 1111111111 L12: 1111111111 L13: 1111111111 L14: 1111111111 L15: 1111111111 L16: 1111111111 L17: 1111111111 L18: 1111111111 L19: 1111111111 L20: 1111111111 L21: 1111111111 L22: 1111111111 L23: 1111111111 L24: 1111111111 L25: 1111111111 L26: 1111111111 L27: 1111111111 L28: 1111111111 L29: 1111111111 L30: 1111111111 L31: 1111111111 L32: 2222222222 L33: 2222222222 L34: 2222222222 L35: 2222222222 L36: 2222222222 L37: 2222222222 L38: 2222222222 L39: 2222222222 L40: 2222222222 L41: 2222222222 L42: 2222222222 L43: 2222222222 L44: 2222222222 L45: 2222222222 L46: 2222222222 L47: 2222222222 L48: 2222222222 L49: 2222222222 L50: 2222222222 L51: 2222222222 L52: 2222222222 L53: 2222222222 L54: 2222222222 L55: 3333333333 L56: 3333333333 L57: 3333333333 L58: 3333333333 L59: 3333333333 L60: 3333333333 L61: 3333333333 L62: 3333333333 L63: 3333333333 L64: 3333333333 L65: 3333333333 L66: 4444444444 L67: 4444444444 L68: 4444444444 L69: 4444444444 L70: 4444444444 L71: 4444444444 L72: 5555555555 L73: 5555555555 L74: 5555555555 L75: 5555555555 L76: 5555555555 L77: OOOOOOOOOO L78: OOOOOOOOOO L79: OOOOOOOOOO L80: OOOOOOOOOO L81: OOOOOOOOOO ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值