Read Phone Number

Description
Do you know how to read the phone numbers in English? Now let me tell you.

For example, In China, the phone numbers are 11 digits, like: 15012233444. Someone divides the numbers into 3-4-4 format, i.e. 150 1223 3444. While someone divides the numbers into 3-3-5 format, i.e. 150 122 33444. Different formats lead to different ways to read these numbers:

150 1223 3444 reads one five zero one double two three three triple four.

150 122 33444 reads one five zero one double two double three triple four.

Here comes the problem:

Given a list of phone numbers and the dividing formats, output the right ways to read these numbers.

Rules:

Single numbers just read them separately.

2 successive numbers use double.

3 successive numbers use triple.

4 successive numbers use quadruple.

5 successive numbers use quintuple.

6 successive numbers use sextuple.

7 successive numbers use septuple.

8 successive numbers use octuple.

9 successive numbers use nonuple.

10 successive numbers use decuple.

More than 10 successive numbers read them all separately.

Input
The first line of the input gives the number of test cases, T(1 ≤ T ≤ 100).

T test cases follow. Each line contains a phone number N(1 ≤ length of N ≤ 100) and the dividing format F, one or more positive integers separated by dashes (-), without leading zeros and whose sum always equals the number of digits in the phone number.

Output
For each test case, output one line containing “Case #x: y”, where x is the case number (starting from 1) and y is the reading sentence in English whose words are separated by a space.
Sample Input
3
15012233444 3-4-4
15012233444 3-3-5
12223 2-3
Sample Output
Case #1: one five zero one double two three three triple four
Case #2: one five zero one double two double three triple four
Case #3: one two double two three

意思就是将号码分块读,12 223分开读就是一个1 和两个2 一个3
15555 3-2 就是分成155 55 读就是一个1 两个5和两个5

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main()
{
    int n;
    cin>>n;
    char num[9][10]= {"double","triple","quadruple","quintuple","sextuple","septuple","octuple","nonuple","decuple"};
    char aa[10][6]= {"zero","one","two","three","four","five","six","seven","eight","nine"};
    for(int k=1; k<=n; k++)
    {
        char a[101];//号码数组
        scanf("%s",a);
        int b[200]= {0};//每个分区的长度
        char c[205];//3-4-4字符串的读入
        scanf("%s",c);
        int l=0;
        for(int i=0; i<strlen(c); i++)
        {
            if(c[i]>='0'&&c[i]<='9')
                b[l]=b[l]*10+c[i]-'0';
            else
                l++;
        }
        printf("Case #%d:",k);
        int s=0;
        for(int i=0; i<=l; i++)//操作输出
        {
            int sum=1;
            if(b[i]==1)//一个时特例,直接输出
                cout<<" "<<aa[a[s]-'0'];
            else
                for(int j=s; j<s+b[i]; j++)
                {
                    if(j!=s)
                    {
                        if(a[j]==a[j-1])
                        {
                            sum++;
                        }
                        else
                        {
                            if(sum>10)//超过十个也是一个个输出
                            {
                                for(int q=0;q<sum;q++)
                                    cout<<" "<<aa[a[j-1]-'0'];
                            }
                            else if(sum>=2)//两个到十个得用double等输出
                                cout<<" "<<num[sum-2]<<" "<<aa[a[j-1]-'0'];
                            else//一个的输出
                                cout<<" "<<aa[a[j-1]-'0'];
                            sum=1;
                        }
                        if(j==s+b[i]-1)//每个分区的最后一个的输出
                        {
                            if(sum>10)
                            {
                                for(int q=0;q<sum;q++)
                                    cout<<" "<<aa[a[j]-'0'];
                            }
                            else if(sum>=2)
                                cout<<" "<<num[sum-2]<<" "<<aa[a[j]-'0'];
                            else
                                cout<<" "<<aa[a[j]-'0'];
                            sum=1;
                        }
                    }
                }
            s+=b[i];
        }
        cout<<endl;
    }
    return 0;
}
首先,你的代码有一些语法错误,我假设你的意思是: ``` set successCount to 0 set errorCount to 0 tell application "Messages" with timeout of 8 * 3600 seconds set phonefilePath to my current folder path() & "phoneNumber.txt" set phoneData to read phonefilePath set phoneEntries to paragraphs of phoneData repeat with i from 1 to count phoneEntries set msgText to (my AppendFace("Hello!")) set msgText to (my AppendFace("How are you?")) set phone to item i of phoneEntries set num to the length of phone set isEmail to false if num = 11 and (my isSendPhone(phone)) = false then set isEmail to false else set isEmail to true end if if not isEmail then set theBuddy to buddy phone of service "SMS" try send msgText to theBuddy set successCount to successCount + 1 on error set errorCount to errorCount + 1 end try end if end repeat end timeout end tell ``` 现在,让我们来看看如何优化这个脚本: 1. 变量命名:建议遵循变量命名规则,例如,变量名是名词且首字母小写;包括变量类型,例如,如果是列表,则使用复数形式。 2. 代码缩进:建议使用四个空格进行缩进,这样可以提高代码的可读性。 3. 减少重复代码:在每个循环迭代中,你都定义了相同的消息文本。建议在循环之前定义它们,以便可以重复使用。 4. 使用逻辑运算符:可以使用逻辑运算符来简化代码,并使其更简洁易懂。 5. 错误处理:在发送消息时,应该使用 try...on error 语句来处理可能出现的错误。 下面是优化后的代码: ``` set successCount to 0 set errorCount to 0 tell application "Messages" with timeout of 8 * 3600 seconds set phoneFilePath to my current folder path() & "phoneNumber.txt" set phoneData to read phoneFilePath set phoneEntries to paragraphs of phoneData set msgText1 to (my AppendFace("Hello!")) set msgText2 to (my AppendFace("How are you?")) repeat with phone in phoneEntries set num to the length of phone set isEmail to false if num = 11 and (my isSendPhone(phone)) = false then set isEmail to false else set isEmail to true end if if not isEmail then set theBuddy to buddy phone of service "SMS" try send msgText1 to theBuddy send msgText2 to theBuddy set successCount to successCount + 1 on error set errorCount to errorCount + 1 end try end if end repeat end timeout end tell ``` 这样,我们就成功地对脚本进行了优化,使其更加清晰、易读和易于维护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值