1016. Phone Bills (25)

时间:2024-12-02 15:07:25

分析:

  模拟题,提交无数次WA,注意几点:

  1.如果某人没有有效通话记录,则不输出该人的信息,在此WA15次,题目看了N遍也没出现啊。

  2.通话时间钱的计算:假设我们计算time1到time2的账单;

            (1)我们可以采用从起点(即00:00:00)开始计算,结果就是get_money(time2) - get_money(time1), 这样计算方便。

            (2)我们也可以采用从time1开始递增直到time2, 这样比较烦。

  3.有效的通话记录是指:如果某人的通话记录为1.on;2.on;3.off;,则其中1.on将被抛弃,匹配到2.on;3.off;

 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <cctype>
#include <stack>
#include <map> using namespace std; int rate_structure[]; struct Person
{
string name;
int month;
int dd, hh, mm;
int total;
bool is_on_line;
}person[]; int cmp(const Person &a, const Person &b)
{
if (a.name != b.name)
return a.name < b.name;
else return a.total < b.total;
} double get_money(int idx) // 得到钱
{
double money = ; money += person[idx].dd * * rate_structure[];
for (int i = ; i < person[idx].hh; i++)
money += * rate_structure[i];
money += person[idx].mm * rate_structure[person[idx].hh]; return money / ;
} bool should_output(int idx, int n) //判断某人的记录是否有有效记录
{
int pre = -;
for (int i = idx; i < n; i++)
{
if (person[i].name == person[idx].name)
{
if (person[i].is_on_line == )
pre = i;
else if (person[i].is_on_line == )
{
if (pre != -) return ;
}
}
else return ;
}
return ;
} void work(int n)
{
string tmp = person[].name;
double sum = ;
int pre = -; //记录off_line前一个的on_line
bool flag = ; if (should_output(, n))
{
cout << person[].name;
printf(" %02d\n", person[].month);
flag = ;
}
for (int i = ; i < n; i++)
{
if (person[i].name != tmp)
{
if (flag == )
{
printf("Total amount: $%.2lf\n", sum);
flag = ;
} if (should_output(i, n))
{
cout << person[i].name;
printf(" %02d\n", person[i].month);
flag = ;
}
pre = -;
sum = ;
tmp = person[i].name;
} if (person[i].is_on_line == )
pre = i;
else if (person[i].is_on_line == )
{
if (pre != -)
{
printf("%02d:%02d:%02d ", person[pre].dd, person[pre].hh, person[pre].mm);
printf("%02d:%02d:%02d ", person[i].dd, person[i].hh, person[i].mm);
printf("%d ", person[i].total - person[pre].total); double money = get_money(i) - get_money(pre);
printf("$%.2lf\n", money);
sum += money;
pre = -;
}
}
}
if (flag == )
printf("Total amount: $%.2lf\n", sum);
} int main()
{
int n;
string status;
while (scanf("%d", &rate_structure[]) != EOF)
{
rate_structure[] = rate_structure[]; //用rate_structure[24]存储0-23之和
for (int i = ; i < ; i++)
{
scanf("%d", &rate_structure[i]);
rate_structure[] += rate_structure[i];
}
scanf("%d", &n); for (int i = ; i < n; i++)
{
cin >> person[i].name;
scanf("%d:%d:%d:%d", &person[i].month, &person[i].dd,
&person[i].hh, &person[i].mm);
cin >> status; person[i].total = person[i].dd * + person[i].hh * + person[i].mm;
person[i].is_on_line = (status == "on-line"? : );
} sort(person, person + n, cmp); work(n); //print(); }
return ;
}