放一下最新的,两个案例都对了,只能努力到30分了,为什么。。
#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
struct teacher {
int room;
int star;
int worktime;
int end;
};
int main()
{
int N, K;
cin >> N >> K;
vector<int>key;//给钥匙赋教室标号
for (int i = 0; i < N; i++) {
int tempp = 0;
tempp = i + 1;
key.push_back(tempp);
}
vector<teacher>arr;//输入教师情况
for (int i = 0; i < K; i++) {
teacher temp;
cin >> temp.room >> temp.star >> temp.worktime;
temp.end = temp.star + temp.worktime;//计算下课是什么时间
arr.push_back(temp);
}
int early = 1000;//计算最早开始的时刻
for (int i = 0; i < K; i++) {
if (arr[i].star < early) early = arr[i].star;
}
int late = 0;//记录最晚结束的时刻
for (int i = 0; i < K; i++) {
if (arr[i].end > late) late = arr[i].end;
}
for (int i = early; i <= late; i++) {//i用来记录是哪一个时刻
for (int j = 0; j < K; j++) {
if (arr[j].star == i) {
for (int k = 0; k < N; k++)
{//在该时刻哪个老师需要哪个编号的房间
if (key[k] == arr[j].room) {
key[k] = 0; break;
}//置0标志
}
}
if (arr[j].end == i)
{
for (int k = 0; k < N; k++) {
if (key[k] == 0) {
key[k] = arr[j].room; break;
}
}
}
}
}
for (int i = 0; i < N; i++)
cout << key[i] << ' ';
return 0;
}
=================================
这是第一次写的,只有十分,错误蛮多的:
break是直接结束这个循环,也就是说并没有赋值成功。
#include <iostream>
#include <vector>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
struct teacher {
int room;
int star;
int worktime;
int end;
};
bool cmp1(teacher a, teacher b) {
if (a.star != b.star)
return a.star < b.star;
}
bool cmp2(teacher a, teacher b) {
if (a.end != b.end)
return a.end > b.end;
}
int main()
{
int N, K;
cin >> N >> K;
vector<int>key;
for (int i = 0; i < N; i++) {
int tempp = 0;
tempp = i + 1;
key.push_back(tempp);
}
vector<teacher>arr;
for (int i = 0; i < K; i++) {
teacher temp;
cin >> temp.room >> temp.star >> temp.worktime;
temp.end = temp.star + temp.worktime;
arr.push_back(temp);
}
sort(arr.begin(), arr.end(), cmp1);
int early = arr[0].star;
sort(arr.begin(), arr.end(), cmp2);
int late = arr[0].end;
//记录时间
for (int i = early; i <= late; i++) {
for (int j = 0; j < N; j++) {
if (arr[j].star == i) {
for (int k = 0; k < N; k++)
{
if (key[k] == arr[j].room) key[k] == 0;
break;
}
}
else if (arr[j].end == i)
{
for (int k = 0; k < N; k++) {
if (key[k] == 0) key[k] == arr[j].room;
break;
}
}
}
}
for (int i = 0; i < N; i++)
{
cout << key[i] << ' ';
}
return 0;
}
这是第二次写的,只能满足第一个样例,20分,因为sort函数一旦里面有重复的项就会报错(vector越界报错)
#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
struct teacher {
int room;
int star;
int worktime;
int end;
};
bool cmp1(teacher a, teacher b) {
if (a.star != b.star)
return a.star < b.star;
}
bool cmp2(teacher a, teacher b) {
if (a.end != b.end)
return a.end > b.end;
}
int main()
{
int N, K;
cin >> N >> K;
vector<int>key;
for (int i = 0; i < N; i++) {
int tempp = 0;
tempp = i + 1;
key.push_back(tempp);
}
vector<teacher>arr;
for (int i = 0; i < K; i++) {
teacher temp;
cin >> temp.room >> temp.star >> temp.worktime;
temp.end = temp.star + temp.worktime;
arr.push_back(temp);
}
sort(arr.begin(), arr.end(), cmp1);
int early = arr[0].star;
sort(arr.begin(), arr.end(), cmp2);
int late = arr[0].end;
for (int i = early; i <= late; i++) {
for (int j = 0; j < K; j++) {
if (arr[j].star == i) {
for (int k = 0; k < N; k++)
{
if (key[k] == arr[j].room) {
key[k] = 0; break;
}
}
}
if (arr[j].end == i)
{
for (int k = 0; k < N; k++) {
if (key[k] == 0) {
key[k] = arr[j].room; break;
}
}
}
}
}
for (int i = 0; i < N; i++)
cout << key[i] << ' ';
return 0;
}