PAT甲级真题 1017 Queueing at Bank

一、题目
PAT甲级真题 1017 Queueing at Bank_第1张图片
PAT甲级真题 1017 Queueing at Bank_第2张图片

二、
还是要搞清楚思路,运用技巧
注意细节!!!

三、代码

v1.0:

#include 
#include 
#define START 28800
#define STOP 61200

typedef struct {
	int at; int pt;
	int st; int et;
}customer;
customer wait[10001];

typedef struct node{
	customer data;
	struct node* next;
}line;
line* h;
int vn, k, linelen, wf, wr;
float sum;

int cmp(const void* a, const void* b) {
	customer* tmpa = (customer*)a;
	customer* tmpb = (customer*)b;
	return tmpa->at - tmpb->at;
}

int process() {
	int fast, i;
	line* s, *p;
	s = h->next;
	fast = s->data.et;
	while (s != NULL) {
		if (fast > s->data.et)
			fast = s->data.et;
		s = s->next;
	}
	s = h; p = h->next;
	while (p != NULL) {
		if (p->data.et == fast) {
			sum += p->data.st - p->data.at;
			s->next = p->next;
			p = p->next;
			linelen--;
		}
		else {
			s = s->next; p = p->next;
		}
	}
	while ((wf!=wr)&&(linelen<k)) {
		s= (line*)malloc(sizeof(line));
		s->data = wait[++wf];
		if (s->data.at > fast)
			s->data.st = s->data.at;
		else
			s->data.st = fast;
		s->data.et = s->data.st + s->data.pt;
		s->next = h->next;
		h->next = s;
		linelen++;
	}
	return 0;
}

int main() {
	int n, i, tmph, tmpm, tmps, tmppt, tmpt, f=0;
	line* s;
	scanf("%d %d", &n, &k);
	for (i = 0; i < n; i++) {
		scanf("%d:%d:%d %d", &tmph, &tmpm, &tmps, &tmppt);
		tmpt = tmps + tmpm * 60 + tmph * 3600;
		if (tmpt > STOP)
			continue;
		wr++;
		wait[wr].at = tmpt; 
		if(tmppt<=60)
			wait[wr].pt = tmppt*60;
		else
			wait[wr].pt = 60 * 60;
	}
	vn = wr;
	if (vn == 0) {
		printf("0.0");
		return 0;
	}
	qsort(&wait[1], vn, sizeof(customer), cmp);//坑
	h = NULL;
	while (linelen < k) {
		if (wf == wr)
			break;
		s = (line*)malloc(sizeof(line));
		s->data = wait[++wf];
		if (s->data.at < START)
			s->data.st = START;
		else
			s->data.st = s->data.at;
		s->data.et = s->data.st + s->data.pt;
		s->next = h;
		h = s;
		linelen++;
	}
	s = (line*)malloc(sizeof(line));
	s->data.at = 0; s->data.pt = 0; s->data.st = 0; s->data.et = 0; 
	s->next = h; h = s;//head

	while (linelen > 0) {
		f = process();
	}
	sum = sum / (60.0 * vn);
	printf("%.1lf", sum);
	return 0;
}

v1.0

#include 
#include 
#define START 28800
#define STOP 61200

typedef struct {
	int at; int pt;
	int st; int et;
}customer;
customer wait[10001];
customer line[100];

int cmp_at(const void* a, const void* b) {
	customer* tmpa = (customer*)a;
	customer* tmpb = (customer*)b;
	return tmpa->at - tmpb->at;
}

int cmp_et(const void* a, const void* b) {
	customer* tmpa = (customer*)a;
	customer* tmpb = (customer*)b;
	return tmpb->et - tmpa->et;
}

int main() {
	int n, k, vn, i, tmph, tmpm, tmps, tmpt, tmppt, fast;
	int wf=0, wr=0, linelen=0;
	float sum = 0.0;
	scanf("%d %d", &n, &k);
	for (i = 0; i < n; i++) {
		scanf("%d:%d:%d %d", &tmph, &tmpm, &tmps, &tmppt);
		tmpt = tmps + tmpm * 60 + tmph * 3600;
		if (tmpt > STOP)
			continue;
		wr++;
		wait[wr].at = tmpt; wait[wr].pt = tmppt*60;
	}
	vn = wr;
	if (vn == 0) {
		printf("0.0");
		return 0;
	}
	qsort(&wait[1], vn, sizeof(customer), cmp_at);
	while (linelen < k && wf != wr) {
		line[linelen] = wait[++wf];
		if (line[linelen].at < START)
			line[linelen].st = START;
		else
			line[linelen].st = line[linelen].at;
		line[linelen].et = line[linelen].st + line[linelen].pt;
		linelen++;
	}
	while (linelen > 0) {
		qsort(line, linelen, sizeof(customer), cmp_et);
		fast = line[linelen - 1].et;
		while (line[linelen - 1].et == fast && linelen>0) {
			sum += line[linelen - 1].st - line[linelen - 1].at;
			linelen--; 
		}
		while (linelen < k && wr != wf) {
			line[linelen] = wait[++wf];
			if (line[linelen].at < fast)
				line[linelen].st = fast;
			else
				line[linelen].st = line[linelen].at;
			line[linelen].et = line[linelen].st + line[linelen].pt;
			linelen++;
		}	
	}
	printf("%.1lf", sum / (60.0 * vn));
	return 0;
}

你可能感兴趣的:(PAT甲级真题)