跳到正文

题解

题解:P16469 [GKS 2013 #A] Read Phone Number

AnnoyingVillagers约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年5月16日
收录
2026年6月5日
更新
2026年11月15日
篇幅
约 3 分钟
题目
P16469

版本

  1. v1当前

题目传送门

题目思路

这道题其实很简单

  1. 切割字符串:按 - 分割,得到每段的长度,然后从电话号码中截取对应长度的子串。
  2. 处理每段子串:
    • 遍历子串,统计连续相同字符的个数 cnt1。
    • 如果 2≤cnt1≤102 \le cnt_1 \le 10:输出对应的倍数词(double/triple/…)+ 数字的英文。
    • 如果 cnt1 == 1 或 cnt1 > 10:逐个输出数字的英文。
  3. 输出:Case #x: + 读出的单词,单词间空格分隔

我有点小笨这里我用了一个函数来计算:i 指向当前段开头,j 找到第一个不同的位置,cnt=j-i 就是连续相同个数,然后按要求输出。

CODE

C++
#include<bits/stdc++.h>
using namespace std;
string num[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
string rep[11]={"","","double","triple","quadruple","quintuple","sextuple","septuple","octuple","nonuple","decuple"};
void print(string s){
    int n=s.size();
    for(int i=0;i<n;){
        int j=i;
        while(j<n and s[j]==s[i]) j++;
        int cnt1=j-i;
        if(cnt1>=2 and cnt1<=10){
            cout<<rep[cnt1]<<" "<<num[s[i]-'0']<<" ";
        }else{
            for(int k=0;k<cnt1;k++) cout<<num[s[i]-'0']<<" ";
        }
        i=j;
    }
}
int main(){
    cin.tie(0)->ios::sync_with_stdio(0);
    int t;cin>>t;
    for(int i=1;i<=t;i++){
        string s,f;cin>>s>>f;
        cout<<"Case #"<<i<<": ";
        int pos=0;
        f+="-";
        int cnt2=0;
        for(int j=0;j<f.size();j++){
            if(f[j]=='-'){
                string sub=s.substr(pos,cnt2);
                print(sub);
                pos+=cnt2;
                cnt2=0;
            }else cnt2=cnt2*10+f[j]-'0';
        }
        cout<<"\n";
    }
    return 0;
}
原文