问题简述:
议会中有n个议员,分别是两个党派,编号为奇数的是同一党派,编号为偶数的是同一党派
会议中有a排b列椅子,如果相邻的人是同党的就会交流党内事务
编写程序,给定议员的人数n和大厅的大小a*b,确定是否有办法为任何国会议员找到一个席位,使同一政党的两个成员没有相邻的席位。
代码
#include <iostream>
#include <vector>
using namespace std;
int main () {
int n, a, b, c=1, d=2, t;
cin >> n >> a >> b;
if (n>a*b) {cout << "-1" << endl; return 0;} //只有坐不下的情况输出-1;
for (int k=1; k<=a; k++) { //其他情况,排座位
for (int i=1; i<=b; i++) {
if (i%2 ^ k%2) {t=d; d+=2;}
else {t=c; c+=2;}
if (t > n) t=0;
cout << t << ' ';
}
cout << endl;
}
return 0;
}