1016. Phone Bills (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;

  1 #include <iostream>

  2 #include <stdio.h>

  3 #include <algorithm>

  4 #include <cstring>

  5 #include <string>

  6 #include <vector>

  7 #include <cctype>

  8 #include <stack>

  9 #include <map>

 10 

 11 using namespace std;

 12 

 13 int rate_structure[25];

 14 

 15 struct Person

 16 {

 17     string name;

 18     int month;

 19     int dd, hh, mm;

 20     int total;

 21     bool is_on_line;

 22 }person[1005];

 23 

 24 

 25 int cmp(const Person &a, const Person &b)

 26 {

 27     if (a.name != b.name)

 28         return a.name < b.name;

 29     else return a.total < b.total;

 30 }

 31 

 32 double get_money(int idx)                // 得到钱

 33 {

 34     double money = 0;

 35 

 36     money += person[idx].dd * 60 * rate_structure[24];

 37     for (int i = 0; i < person[idx].hh; i++)

 38         money += 60 * rate_structure[i];

 39     money += person[idx].mm * rate_structure[person[idx].hh];

 40 

 41     return money / 100;

 42 }

 43 

 44 bool should_output(int idx, int n)            //判断某人的记录是否有有效记录

 45 {

 46     int pre = -1;

 47     for (int i = idx; i < n; i++)

 48     {

 49         if (person[i].name == person[idx].name)

 50         {

 51             if (person[i].is_on_line == 1)

 52                 pre = i;

 53             else if (person[i].is_on_line == 0)

 54             {

 55                 if (pre != -1) return 1;

 56             }

 57         }

 58         else return 0;

 59     }

 60     return 0;

 61 }

 62 

 63 void work(int n)

 64 {

 65     string tmp = person[0].name;

 66     double sum = 0;

 67     int pre = -1;                        //记录off_line前一个的on_line

 68     bool flag = 0;                        

 69 

 70     if (should_output(0, n))

 71     {

 72         cout << person[0].name;

 73         printf(" %02d\n", person[0].month);

 74         flag = 1;

 75     }

 76     for (int i = 0; i < n; i++)

 77     {

 78         if (person[i].name != tmp)

 79         {

 80             if (flag == 1)

 81             {

 82                 printf("Total amount: $%.2lf\n", sum);

 83                 flag = 0;

 84             }

 85 

 86             if (should_output(i, n))

 87             {

 88                 cout << person[i].name;

 89                 printf(" %02d\n", person[i].month);

 90                 flag = 1;

 91             }

 92             pre = -1;

 93             sum = 0;

 94             tmp = person[i].name;

 95         }

 96 

 97         if (person[i].is_on_line == 1)

 98             pre = i;

 99         else if (person[i].is_on_line == 0)

100         {

101             if (pre != -1)

102             {

103                 printf("%02d:%02d:%02d ", person[pre].dd, person[pre].hh, person[pre].mm);

104                 printf("%02d:%02d:%02d ", person[i].dd, person[i].hh, person[i].mm);

105                 printf("%d ", person[i].total - person[pre].total);

106 

107 

108                 double money = get_money(i) - get_money(pre);

109                 printf("$%.2lf\n", money);

110                 sum += money;

111                 pre = -1;

112             }

113         }

114     }

115     if (flag == 1)

116         printf("Total amount: $%.2lf\n", sum);

117 }

118 

119 int main()

120 {

121     int n;

122     string status;

123     while (scanf("%d", &rate_structure[0]) != EOF)

124     {

125         rate_structure[24] = rate_structure[0];   //用rate_structure[24]存储0-23之和

126         for (int i = 1; i < 24; i++)

127         {

128             scanf("%d", &rate_structure[i]);

129             rate_structure[24] += rate_structure[i];

130         }

131         scanf("%d", &n);

132 

133         for (int i = 0; i < n; i++)

134         {

135             cin >> person[i].name;

136             scanf("%d:%d:%d:%d", &person[i].month, &person[i].dd, 

137                 &person[i].hh, &person[i].mm);

138             cin >> status;

139 

140             person[i].total = person[i].dd * 1440 + person[i].hh * 60 + person[i].mm;

141             person[i].is_on_line = (status == "on-line"? 1: 0);

142         }

143 

144         sort(person, person + n, cmp);

145 

146         work(n);

147 

148         //print();

149 

150     }

151     return 0;

152 }
View Code

 

  

你可能感兴趣的:(one)