A1016 Phone Bills [排序]

在这里插入图片描述

题目大意:

  1. 查话费记录和账单,我理解完就码了还花了挺长时间的,自己做搞不出来唉。到时候再做一遍吧,看看理解能深入点不。

思路:

  1. 先全部排序,排序方法如题意思(见代码),然后先扫描相同名字的记录,如果有接通和挂断的就是一个有效通话记录,进行标记,然后再在这个名字里面的查询输出每一条有效通话信息,如果没有标记就不输出,进入下一个名字的人循环。
  2. 难点还有算钱和时间的计算(一起计算,每加一分钟就加相应的钱。)
    ————————————————————————
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct record
{
	char name[25];
	int MM, dd, hh,mm;
	bool status;
}rec[1010],temp;
int toll[24];
bool cmp(record a, record b)
{
	int s = strcmp(a.name, b.name);
	if (s != 0)return s < 0;
	else 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;
}
void cost(record a, record b,int& time,int& money)
{
	temp = a;
	while (temp.dd < b.dd || temp.hh < b.hh || temp.mm < b.mm)
	{
		time++;
		money += toll[temp.hh];// 每分钟增加的钱
		temp.mm++;
		if (temp.mm >= 60)
		{
			temp.mm = 0;
			temp.hh++;
		}
		if (temp.hh >= 24)
		{
			temp.hh = 0;
			temp.dd++;
		}
	}
}
int main()
{
	for (int i = 0; i < 24; i++)
	{
		cin >> toll[i];
	}
	int n;
	cin >> n;
	char line[10];
	for (int i = 0; i < n; i++)
	{
		cin >> rec[i].name;
		scanf_s("%d:%d:%d:%d", &rec[i].MM, &rec[i].dd, &rec[i].hh, &rec[i].mm);
		cin >> line;
		if (strcmp(line, "on-line") == 0)
		{
			rec[i].status = true;
		}
		else
		{
			rec[i].status = false;
		}
	}
	sort(rec, rec + n, cmp);
	int on=0,off, next;
	while (on < n)
	{
		int needprint = 0;
		next = on;
		while (next < n && strcmp(rec[next].name, rec[on].name) == 0)
		{
			if (needprint==0 && rec[next].status==true)
			{
				needprint = 1;
			}
			else if (needprint == 1 && rec[next].status == false)
			{
				needprint = 2;
			}
			next++;
		}
		if (needprint < 2)
		{
			on = next;
			continue;
		}
		int allmoney = 0;
		printf("%s %02d
", rec[on].name, rec[on].MM);
		while (on < next)
		{
			while (on < next - 1 && !(rec[on].status==true && rec[on + 1].status==false))
			{
				on++;
			}
			off = on + 1;
			if (off == next)
			{
				on = off;
				break;	
			}
			printf ("%02d:%02d:%02d ", rec[on].dd, rec[on].hh, rec[on].mm);
			printf("%02d:%02d:%02d ", rec[off].dd, rec[off].hh, rec[off].mm);
			int time = 0, money = 0;
			cost(rec[on], rec[off], time, money);
			allmoney += money;
			printf("%d $%.2f
", time, money / 100.0);
			on = off + 1; 
		}
		printf("Total amount: $%.2f
", allmoney/ 100.0);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/Hsiung123/p/13812082.html