设为首页 加入收藏

TOP

提交无数次WA的一道模拟题
2014-02-14 12:51:57 来源: 作者: 【 】 浏览:153
Tags:提交 无数 一道 模拟题
    分析:
    模拟题,提交无数次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
    2 #include
    3 #include
    4 #include
    5 #include
    6 #include
    7 #include
    8 #include
    9 #include
    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

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++之构造函数与析造函数 下一篇c++ 复数控制向量旋转

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Libevent C++ 高并发 (2025-12-26 00:49:30)
·C++ dll 设计接口时 (2025-12-26 00:49:28)
·透彻理解 C 语言指针 (2025-12-26 00:22:52)
·C语言指针详解 (经典 (2025-12-26 00:22:49)
·C 指针 | 菜鸟教程 (2025-12-26 00:22:46)