24h購物| | PChome| 登入
2013-08-11 14:18:57| 人氣1,494| 回應0 | 上一篇 | 下一篇

[UVA] 139 - Telephone Tangles

推薦 0 收藏 0 轉貼0 訂閱站台


 Telephone Tangles 

A large company wishes to monitor the cost of phone calls made by its personnel. To achieve this the PABX logs, for each call, the number called (a string of up to 15 digits) and the duration in minutes. Write a program to process this data and produce a report specifying each call and its cost, based on standard Telecom charges.

International (IDD) numbers start with two zeroes (00) followed by a country code (1-3 digits) followed by a subscriber's number (4-10 digits). National (STD) calls start with one zero (0) followed by an area code (1-5 digits) followed by the subscriber's number (4-7 digits). The price of a call is determined by its destination and its duration. Local calls start with any digit other than 0 and are free.

Input

Input will be in two parts. The first part will be a table of IDD and STD codes, localities and prices as follows:

Code tex2html_wrap_inline45 Locality name$price in cents per minute

where tex2html_wrap_inline45 represents a space. Locality names are 25 characters or less. This section is terminated by a line containing 6 zeroes (000000).

The second part contains the log and will consist of a series of lines, one for each call, containing the number dialled and the duration. The file will be terminated a line containing a single #. The numbers will not necessarily be tabulated, although there will be at least one space between them. Telephone numbers will not be ambiguous.

Output

Output will consist of the called number, the country or area called, the subscriber's number, the duration, the cost per minute and the total cost of the call, as shown below. Local calls are costed at zero. If the number has an invalid code, list the area as ``Unknown'' and the cost as -1.00.

Sample input

088925 Broadwood$81
03 Arrowtown$38
0061 Australia$140
000000
031526        22
0061853279  3
0889256287213   122
779760 1
002832769 5
#

Sample output

tabular28


題目要特別小心 Unknown,
因為有模稜兩可的情況、客戶號碼過長過短都是 UNKNOWN。

接著比較麻煩的地方就是輸出格式。



#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
struct E {
    string code, name;
    int price;
};
E D[10005];
int main() {
    char s[1005], code[1005];
    int n = 0, mm;
    int i, j, k;
    while(gets(s)) {
        if(!strcmp(s, "000000"))
            break;
        for(i = 0; s[i] != ' '; i++);
        s[i] = '\0';
        D[n].code = s;
        for(j = ++i; s[i] != '$'; i++);
        s[i] = '\0';
        D[n].name = s+j;
        sscanf(s+i+1, "%d", &D[n].price);
        n++;
    }
    while(scanf("%s %d", &code, &mm) == 2) {
        if(!strcmp(code, "#"))  break;
        if(code[0] != '0') {
            printf("%-16s%-5s%30s%5d%6.2lf%7.2lf\n", code, "Local", code, mm, 0.00, 0.00);
            continue;
        }
        int len = strlen(code);
        int found = 0;
        string cc;
        for(i = 0; i < n; i++) {
            if(D[i].code.length() >= len)   continue;
            cc = code;
            cc = cc.substr(0, D[i].code.length());
            if(cc == D[i].code) {
                cc = code;
                cc = cc.substr(D[i].code.length());
                if(D[i].code[0] == '0' && D[i].code[1] == '0') {
                    if(cc.length() < 4 || cc.length() > 10)
                        continue;
                } else {
                    if(cc.length() < 4 || cc.length() > 7)
                        continue;
                }
                found++, j = i;
                break;
            }
        }
        if(found != 1) {
            printf("%-16s%-25s%10s%5d%6s%7.2lf\n", code, "Unknown", "", mm, "", -1.0);
        } else {
            i = j;
            cc = code;
            cc = cc.substr(D[i].code.length());
            printf("%-16s%-25s%10s%5d%6.2lf%7.2lf\n", code, D[i].name.c_str(), cc.c_str(), mm, D[i].price/100.0, mm*D[i].price/100.0);
        }
    }
    return 0;
}
/*
0061234         Los Angeles                   61234    5  0.10   0.50
779760          Local                        779760    1  0.00   0.00
*/

台長: Morris
人氣(1,494) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][樂理] 346 - Getting Chorded
此分類上一篇:[UVA] 11945 - Financial Management

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文