核心内容摘要
《泡我家的辣妹同意》第二季:当心动信号遇上心动行动,甜蜜预警!
题目分析本题是一道电话计费模拟题要求根据拨打的号码和通话时长结合给定的区号或国家代码与费率表计算出每一通电话的费用。
输入格式输入分为两部分费率表每行格式为代码 地区名称$每分钟费用美分地区名称不超过252525个字符费率以美分为单位。
此部分以一行000000结束。
通话记录每行格式为号码 时长分钟号码与时长之间至少有一个空格。
此部分以一行#结束。
号码分类国际长途IDD\texttt{IDD}IDD以00开头后接111至333位国家代码再后接444至101010位用户号码。
国内长途STD\texttt{STD}STD以0开头但非00后接111至555位区号再后接444至777位用户号码。
本地通话不以0开头免费。
无效号码若号码以0或00开头但未在费率表中找到匹配的代码或用户号码长度不符合规定则视为Unknown费用为−
00-
00−
00。
输出格式输出每行包含拨打的号码左对齐宽度161616地区名称用户号码通话时长右对齐宽度555每分钟费用右对齐宽度666保留两位小数总费用右对齐宽度777保留两位小数若为未知号码则每分钟费用处留空。
解题思路步骤分解解析费率表将每行的代码、地区名称、费率分别提取出来存储在一个结构体数组中。
处理通话记录对每条记录提取号码和时长。
根据号码前缀判断类型若以00开头尝试匹配国际代码。
若以0开头非00尝试匹配国内区号。
否则为本地通话。
匹配时需检查用户号码长度是否符合规定。
若未匹配成功则为Unknown。
计算费用费率以美分存储计算时需除以
100.
0100.
0
0转换为美元。
格式化输出使用cout的格式化输出功能如setw、setprecision控制对齐与精度。
关键点匹配代码时使用find判断前缀是否一致。
用户号码长度需在匹配后验证。
输出格式需严格对齐包括空格数量。
代码实现// Telephone Tangles// UVa ID: 139// Verdict: Accepted// Submission Date:
// UVa Run Time:
085s//// 版权所有C2016邱秋。
metaphysis # yeah dot net#includebits/stdc.husingnamespacestd;structprice{string code;string area;intcost;};intmain(){string line;vectorpriceprices;// 处理
分输入while(getline(cin,line),line.find(
!
{string code,area,fee;intindex0;while(indexline.length()isdigit(line[index]))codeline[index];// 跳过空格直到 $ 都属于国家或地区名称index;while(indexline.length()line[index]!$)arealine[index];// 跳过 $index;while(indexline.length()){if(isdigit(line[index]))feeline[index];index;}// cout code area fee endl;prices.push_back((price){code,area,stoi(fee)});}// 处理
分输入并输出while(getline(cin,line),line!#){intindex0;string calledNumber,duration;while(indexline.length()isdigit(line[index]))calledNumberline[index];while(indexline.length()isblank(line[index]))index;while(indexline.length()isdigit(line[index]))durationline[index];string area,subscriber;doublecost,totalCost;boolunknowntrue;for(inti0;iprices.size();i){if(calledNumber.find(prices[i].code)
{areaprices[i].area;subscribercalledNumber.substr(prices[i].code.length());if(calledNumber.find(
00)
{if(subscriber.length()4||subscriber.length()
continue;}elseif(calledNumber.find(
0)
{if(subscriber.length()4||subscriber.length()
continue;}costprices[i].cost/
1
00;totalCoststoi(duration)*cost;unknownfalse;break;}}if(unknown){if(calledNumber.find(
0||calledNumber.find(
0)
{areaUnknown;subscriber.clear();totalCost-
0;}else{areaLocal;subscribercalledNumber;cost
00;totalCost
0;unknownfalse;}}cout.setf(ios::fixed);coutsetw(
leftcalledNumber;coutarea;coutstring(35-area.length()-subscriber.length(), );coutsubscriber;coutsetw(
rightduration;if(unknown)coutstring(6, );elsecoutsetw(
rightsetprecision(
cost;coutsetw(
rightsetprecision(
totalCost;coutendl;}return0;}
总结本题主要考察字符串处理与格式化输出能力同时需要注意细节处理如号码长度验证、费用单位转换。
通过合理设计数据结构并逐步匹配即可正确计算并输出每通电话的费用。