PAT甲级1016 Phone Bills (25分) 只拿到了15分

在时间差额和费用的计算上有些问题,既繁琐还容易出错,可以参考柳婼PAT1016,也算是为以后类似的计算提供了一种通法

#pragma warning(disable:4996)
#include
#include
#include
#include
using namespace std;

struct information
{
     
	char name[22];
	int MM, dd, hh, mm;
	char line[10];
}info[1010];

bool cmp(struct information a, struct information b)
{
     
	int s = strcmp(a.name, b.name);
	if (s != 0)
		return s < 0;
	if (a.MM != b.MM)
		return a.MM < b.MM;
	else if (a.dd != b.dd)
		return a.dd < b.dd;
	else if (a.hh != b.hh)
		return a.hh < b.hh;
	else
		return a.mm < b.mm;
}

int havecout = 0;
double personsum = 0;

int main()
{
     
	int fee[24], daysum = 0;
	for (int i = 0; i < 24; i++)
	{
     
		scanf("%d", &fee[i]);
		daysum = daysum + fee[i];
	}
	
	int N;
	scanf("%d", &N);
	for (int i = 0; i < N; i++)
	{
     
		scanf("%s %d:%d:%d:%d %s", info[i].name, &info[i].MM, &info[i].dd, &info[i].hh, &info[i].mm, info[i].line);
	}
	sort(info, info + N, cmp);

	char on[10] = {
      "on-line" };
	char off[10] = {
      "off-line" };
	for (int i = 0; i < N - 1; i++)
	{
     
		if (strcmp(on, info[i].line) == 0)
		{
     
			if (strcmp(info[i + 1].name, info[i].name) == 0 && (strcmp(off, info[i + 1].line) == 0))
			{
     
				if (havecout == 0)
				{
     
					printf("%s %02d\n", info[i + 1].name, info[i + 1].MM);
					havecout++;
				}
				printf("%02d:%02d:%02d ", info[i].dd, info[i].hh, info[i].mm);
				printf("%02d:%02d:%02d ", info[i + 1].dd, info[i + 1].hh, info[i + 1].mm);
				double sum = 0;
				int time = 0;
				sum = (info[i + 1].dd - info[i].dd)*daysum*60;
				
				time = (info[i + 1].dd - info[i].dd) * 24 * 60;
				sum = sum + fee[info[i].hh] * (60 - info[i].mm) + fee[info[i + 1].hh] * info[i + 1].mm;
				time = time + 60 - info[i].mm + info[i + 1].mm;
				if (info[i].hh == info[i + 1].hh)
				{
     
					time = time - 60;
					sum = sum - 60 * fee[info[i].hh];
				}
				for (int j = info[i].hh + 1; j < info[i + 1].hh; j++)
				{
     
					sum = sum + fee[j] * 60;
					time = time + 60;
				}
				
				printf("%d $%.2f\n", time, sum / 100);
				personsum = personsum + sum / 100;

				int flag = 1;
				
				for (int j = i + 2; j < N - 1; j++)
				{
     
					if (strcmp(info[i].name, info[j+1].name) == 0)
						if (strcmp(info[j].line, on) == 0 && strcmp(info[j + 1].line, off) == 0)
						{
     
							flag = 0;
							break;
						}
					if (strcmp(info[i].name, info[j + 1].name) != 0)
					{
     
						break;
					}
				}
				if (flag==1)
				{
     
					havecout = 0;
					printf("Total amount: $%.2f\n", personsum);
					personsum = 0;
				}
			}
		}
	}
}

你可能感兴趣的:(PAT)