CCF CSP 201609-2:火车购票

CCF CSP 201609-2:火车购票_第1张图片

代码如下:

#include 

using namespace std;

//火车购票 
int main(){
	int n;
	cin >> n;
	vector< vector > v(20, vector(5, 0));  //初始化火车票,0代表未被购买 
	for(int i = 0; i < n; i++){
		int p;   //要购买的票数 
		cin >> p;
		int diff = 1; 
		vector tickets(p);  //将被购买的火车票座位号 
		for(int j = 0; j < 20; j++){   //检查能否安排在一排 
			if(count(v[j].begin(), v[j].end(), 0) < p) continue;
			else{    //能被分在同一排 
				int num = 0;
				for(int k = 0; k < 5; k++){
					if(v[j][k] == 0 && num < p){
						tickets[num] = j*5+k+1;
						num++;
						v[j][k] = 1;
					}
				}
				diff = 0;
				break;
			}
		}
		if(diff == 1){   //不能分在同一排 
			int num = 0;
			for(int m = 0; m < 20; m++){   //从头开始找空位 
				for(int k = 0; k < 5; k++){
					if(v[m][k] == 0 && num < p){
						tickets[num] = m*5+k+1;
						num++;
						v[m][k] = 1;
					}
				}
			}
		}
		for(int k = 0; k < p; k++){
			cout << tickets[k] << " ";
		}
		cout << endl;	
	}
	return 0;		
}

你可能感兴趣的:(CCF,CSP)