fwkkkk 2021-05-12 11:09 采纳率: 66.7%
浏览 55
已结题

简单的食堂排队问题,例子都成功实现,为什么还是过不了,我疏漏了哪里?

#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;
		}
		
	}
 
}
  • 写回答

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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 9月26日
  • 已采纳回答 9月18日

悬赏问题

  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?