#include<iostream>
using namespace std;
struct student {
int intime;
int plantime;
int time;
};
int main() {
student a[1000];
int i, j, k;
int T, n;
cin >> T;
for (i = 0; i < T; i++) {
int sumtime;
cin >> n;
cin >> a[0].intime;
cin >> a[0].plantime;
sumtime = a[0].intime;
a[0].time = sumtime;
for (j = 1; j < n; j++) {
cin >> a[j].intime;
cin >> a[j].plantime;
if (a[j].intime <= sumtime)sumtime += 1;
else sumtime = a[j].intime ;
if (a[j].plantime < sumtime) {
a[j].time= 0;
sumtime -= 1;
}
else a[j].time = sumtime;
}
for (k = 0; k < n; k++) {
if (k == 0)cout << a[k].time;
else cout << ' ' << a[k].time;
}
}
}
简单的食堂排队问题,例子都成功实现,为什么还是过不了,我疏漏了哪里?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- benbenli 2021-05-12 19:48关注
有两那个下标错了,改好了。
#include<iostream> using namespace std; struct student { int intime; int plantime; int time; }; int main() { student a[1000]; int n[1000]; int sumn = 0; int T; cout << "Please enter the number of data groups T: "; cin >> T; for (int i = 0; i < T; i++) { cout << "For group " << i << ", please enter the number of students n: "; int sumtime = 0; cin >> n[i]; for (int j = 0; j < n[i]; j++) { cout << "For group " << i << ", steudent " << j << ", please enter the arriving time and planned leaving time: "; cin >> a[sumn + j].intime; cin >> a[sumn + j].plantime; if (a[sumn + j].intime <= sumtime) sumtime += 1; else sumtime = a[sumn + j].intime ; if (a[sumn + j].plantime < sumtime) { a[sumn + j].time= 0; sumtime -= 1; } else a[sumn + j].time = sumtime; } sumn += n[i]; } cout << "Following are the " << T << " groups of students' service times: " << endl; sumn = 0; for (int i = 0; i < T; i++) { for (int j = 0; j < n[i]; ++j) { cout << a[sumn + j].time << " "; } cout << endl; sumn += n[i]; } return 0; } // Output Please enter the number of data groups T: 2 For group 0, please enter the number of students n: 2 For group 0, steudent 0, please enter the arriving time and planned leaving time: 1 3 For group 0, steudent 1, please enter the arriving time and planned leaving time: 1 4 For group 1, please enter the number of students n: 3 For group 1, steudent 0, please enter the arriving time and planned leaving time: 1 5 For group 1, steudent 1, please enter the arriving time and planned leaving time: 1 1 For group 1, steudent 2, please enter the arriving time and planned leaving time: 2 3 Following are the 2 groups of students' service times: 1 2 1 0 2
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 有了解d3和topogram.js库的吗?有偿请教
- ¥100 任意维数的K均值聚类
- ¥15 stamps做sbas-insar,时序沉降图怎么画
- ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
- ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
- ¥15 关于#Java#的问题,如何解决?
- ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
- ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
- ¥15 cmd cl 0x000007b
- ¥20 BAPI_PR_CHANGE how to add account assignment information for service line