PAT A1016求测试点例子

#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;

struct customer {
	char name[21];
	char on_off[10];
	int tagon;//1为on 0为off
	int month;
	int day;
	int hour;
	int minute;
	bool tag;
}cus[1005];
int cost[24];
bool cmp1(struct customer a, struct customer b) {//按姓名排序从小到大
	return strcmp(a.name, b.name) < 0;
}
bool cmp(struct customer a,struct customer b) {
	if (a.day == b.day) {
		if (a.hour == b.hour) {
			return a.minute < b.minute;
		}
		else {
			return a.hour < b.hour;
		}
	}
	else {
		return a.day < b.day;
	}
}
double charge(int fday,int fhour, int fmin, int eday,int ehour, int emin,int &total) {//计算费用
	int i,j;
	double cos = 0.0;
	if (fday != eday) {//不在同一天
		while (fday != eday || fhour != ehour || fmin != emin) {
			cos += 1.0 *cost[fhour] / 100;
			fmin++;total++;
			if (fmin == 60) {
				fmin = 0;fhour++;
			}
			if (fhour == 24) {
				fhour = 0;
				fday++;
			}
		}
	}
	else {
		if (fday == eday && fhour != ehour) {//同一天
			if (ehour - fhour == 1) {//相差不超过两个小时
				total = total + (60 - fmin);
				cos = cos + 1.0 * (60 - fmin) * cost[fhour] / 100;
				total += emin;
				cos = cos + 1.0 * emin * cost[ehour] / 100;
			}
			else {//相差两个小时以上
				total += (60 - fmin);
				total += emin;
				cos = cos + 1.0 * (60 - fmin) * cost[fhour] / 100;
				cos = cos + 1.0 * emin * cost[ehour] / 100;
				i = fhour + 1;
				j = ehour - 1;
				for (int k = i;k <= j;k++) {
					total += 60;
					cos += 1.0 * 60 * cost[k] / 100;
				}
			}
		}
		else {
			cos = 1.0 * (emin - fmin) * cost[fhour] / 100;
			total = emin - fmin;
		}
	}
	return cos;
}
void process(vector<customer>cus) {//处理每个人的信息
	double sum = 0.0,cos=0.0;
	int total=0;
	int d1, h1, m1, d2, h2,m2,count=0;
	if (cus.size() != 0) {
		sort(cus.begin(), cus.end(), cmp);
		while (cus.size()!=0&&cus[0].on_off == 0) {//开头是off状态,无效,删去
			cus.erase(cus.begin());
		}
		if (cus.size() == 0) {
			return;
		}
		
		while (!cus.empty()) {
			total = 0;
			d1 = cus[0].day;h1 = cus[0].hour;m1 = cus[0].minute;cus.erase(cus.begin());//第一个是on
			if (cus.empty()) {//判断vector是否已经空了
				if (sum != 0) {
					printf("Total amount: $%.2lf\n", sum);
				}
				return;
			}
			while (!cus.empty() &&cus[0].tagon == 1) {//如果下一个还是on,则上一个on无效
					d1 = cus[0].day;h1 = cus[0].hour;m1 = cus[0].minute;cus.erase(cus.begin());//第一个是on
				}
            if (cus.empty()) {//判断vector是否已经空了
				if (sum != 0) {
					printf("Total amount: $%.2lf\n", sum);
				}
				return;
			}
			d2 = cus[0].day;h2 = cus[0].hour;m2 = cus[0].minute;cus.erase(cus.begin());//找到对应的off
			cos=charge(d1, h1, m1, d2, h2, m2, total);
			count++;
			if (count == 1) {
				printf("%s %02d\n", cus[0].name, cus[0].month);
			}
			printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n", d1, h1, m1, d2, h2, m2, total, cos);
			sum = sum + cos;
			while (!cus.empty() && cus[0].on_off == 0) {//开头是off状态,无效,删去
				cus.erase(cus.begin());
			}
			if (cus.empty()) {
				if (sum != 0) {
					printf("Total amount: $%.2lf\n", sum);
				}
				return;
			}
		}
	}
	else {
		return;
	}
}
int main() {
	double cos = 0.0;
	int day1, hour1, minute1,day2,hour2,minute2,total=0;
	int i, j, n;
	for (i = 0;i < 24;i++) {
		scanf("%d", &cost[i]);
	}
	scanf("%d", &n);
	for (i = 0;i < n;i++) {
		scanf("%s%d:%d:%d:%d%s", cus[i].name, &cus[i].month, &cus[i].day, &cus[i].hour, &cus[i].minute, cus[i].on_off);
		if (strcmp(cus[i].on_off, "on-line") == 0) {
			cus[i].tagon = 1;
		}
		else {
			cus[i].tagon = 0;
		}
	}

	sort(cus, cus + n, cmp1);//先按姓名排序
	int pair = 0, len, index = 0;
	char name1[21];
	len = 0;
	vector<struct customer>vec;
	vec.push_back(cus[0]);
	strcpy(name1, vec[0].name);

	while (index < n) {
		index++;
		if (strcmp(name1, cus[index].name) == 0) {
			vec.push_back(cus[index]);
		}
		else {
			process(vec);
			vec.clear();//
			strcpy(name1, cus[index].name);
			vec.push_back(cus[index]);//新结点重新入队
		}
	}


	return 0;
}## PAT A1016 测试点123错了 不解原因,求例子!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值