Google中国2014校园招聘笔试Round A China New Grad Test Problem A. Read Phone Number

13 篇文章 0 订阅
12 篇文章 0 订阅

Problem

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, TT lines|test cases follow. Each line contains a phone number N 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.

Limits

1 ≤ T ≤ 100.

Small dataset

1 ≤ length of N ≤ 10.

Large dataset

1 ≤ length of N ≤ 100.

Sample

Input 
  

3
15012233444 3-4-4
15012233444 3-3-5
12223 2-3



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

 

类型:其他 难度:1.5

题意:给出一个纯数字串和它的分割方式,按照分割方式求这串数字的读法,连续2个到10个分别读作"double","triple","quadruple","quintuple","sextuple","septuple","octuple","nonuple","decuple",连续11个及以上分开读

分析:主要考察基础字符串处理,先从类似"3-4-5-6”字符串中提取数字,然后按照分割方式遍历数字串,记录一个数重复的次数,依次生成结果即可。感觉google笔试都比较基础,但是十分注重细节,思维的严谨,逻辑的严密很重要。

 

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<iostream>
#define MIN(x,y) (x)<(y)?(x):(y)
using namespace std;

const int N=110;
string single[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
string many[11] = {"","","double","triple","quadruple","quintuple","sextuple","septuple","octuple","nonuple","decuple"};

int main()
{
	freopen("A-large.in","r",stdin);
	freopen("A-large.out","w",stdout);
	
	int t;
	scanf("%d",&t);
	for(int cnt=1;cnt<=t;cnt++)
	{
		char num[N];
		char format_s[3*N];
		int format[N];
		string ans = "";
		
		scanf("%s%s",num,format_s);
		
		memset(format,0,sizeof(format));
		
		char *tmp = strtok(format_s,"-");
		int i,j,k;
		for(i=0; tmp; i++)
		{
			format[i] = atoi(tmp);
			//cout<<format[i]<<endl;
			tmp = strtok(NULL,"-");
		}
		
		int n = i;
		int sum = 0;
		for(i=0; i<n; i++)
		{
			char now = 0;
			int ct = 0;
			
			for(j=0; j<format[i]; j++)
			{
				if(now==0 || num[sum+j-1] != num[sum+j])
				{
					if(now>0)
					{
						if(ct>1 && ct<11)
						{
							ans += many[ct];
							ans += " ";
							ans += single[now-'0'];
							ans += " ";
						}
						else
						{
							for(k=0; k<ct; k++)
							{
								ans += single[now-'0'];
								ans += " ";
							}
						}
					}
					now = num[sum+j];
					ct = 1;
				}
				else
				{
					ct++;
				}
			}
			if(now>0)
			{
				if(ct>1 && ct<11)
				{
					ans += many[ct];
					ans += " ";
					ans += single[now-'0'];
					ans += " ";
				}
				else
				{
					for(k=0; k<ct; k++)
					{
						ans += single[now-'0'];
						ans += " ";
					}
				}
			}
			
			sum += format[i];
		}
		if(ans[ans.length()-1] == ' ')
		{
			ans = ans.substr(0,ans.length()-1);
		}
		
		printf("Case #%d: %s\n",cnt,ans.c_str());
	} 
} 


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值