您现在的位置是:首页 > 正文

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

2024-04-01 01:42:27阅读 4

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());
	} 
} 


 

网站文章

  • 简历有空窗期,怎么办?

    上一篇:“公司15号发工资”上热搜职业空窗期,是求职者几乎都有的时期。HR也是企业的员工,空窗期他们都能明白可能是多种原因造成的,比如家庭原因、健康状况、旅行、甚至今年特殊时期找不到工作,那为什么还会...

    2024-04-01 01:42:20
  • 使用Go语言搭建区块链基础

    使用Go语言搭建区块链基础

    通过本文的介绍,我们了解了如何使用Go语言搭建区块链的基础框架。我们学习了如何定义区块结构、实现区块链、计算区块的哈希值、添加新的区块以及验证区块链的完整性。此外,我们还介绍了三个与区块链相关的案例,从简单的加密货币系统到复杂的数字资产交易平台。希望本文能够为你搭建区块链系统提供一些帮助,也希望你能够进一步深入学习和探索区块链技术的更多细节。

    2024-04-01 01:41:53
  • http知识点整理记录

    http知识点整理记录

    TCP的三次握手四次挥手:三次握手(建立连接): 第一次握手: 建立连接时,客户端向服务器端发送TCP连接请求数据包(SYN=1,seq=x),客户端进入SYN_SEND状态 第二次握手: 服务器确认数据包,并返回自己的数据包(SYN=1,ACK=1,ack=x+1,seq=y),服务器进入SYN_RECV状态 第三次握手: 客户端确认并再次发出确认(ACK=1,...

    2024-04-01 01:41:47
  • Nodejs中when库的实例介绍

    Nodejs中when库的实例介绍

    假设一个业务场景:通过rss地址,获取rss并保存于文件,rss地址保存于文件中。完成该场景的业务需要完成3个任务:1.从文件中读取rss地址。2.获取rss。3.保存于文件。最后将这三个任务进行整合。 准备:存放rss地址的文件,address.txt。http://programmer.csdn.net/rss_p

    2024-04-01 01:41:40
  • 做支付遇到的HttpClient大坑

    做支付遇到的HttpClient大坑

    前言 HTTPClient大家应该都很熟悉,一个很好的抓网页,刷投票或者刷浏览量的工具。但是还有一项非常重要的功能就是外部接口调用,比如说发起微信支付,支付宝退款接口调用等;最近我们在这个工具上栽了一个大跟头,不怕大家笑话,拿出来跟大家分享一下; 过程描述 项目代码比较复杂,我为了直达问题,单独写了程序来说明; 我这里先重复一下导致问题的过程:程序源自于从.NET到Java的重构,开发...

    2024-04-01 01:41:16
  • 1003 我要通过! (20 分)

    #include <cstdio>#include <cstring>int main() { int n; scanf("%d", &n); // 之前一直用c++过不了,因为会有空格的关系,所以改用了scanf; while (n--) { char s[110]; int np =...

    2024-04-01 01:41:09
  • NOI-1.5(09) 奇数求和

    NOI-1.5(09) 奇数求和

    2024-04-01 01:41:03
  • 建Stable-Diffusion-Webui的AI

    建Stable-Diffusion-Webui的AI

    人工智能,丹青圣手,全平台(原生/Docker)构建Stable-Diffusion-Webui的AI绘画库教程(Python3.10/Pytorch1.13.0) - 知乎

    2024-04-01 01:40:39
  • golang panic recover return defer的逻辑顺序问题

    package main import &quot;fmt&quot; //验证golang return defer recover 之间的顺序关系 func main() { defer func...

    2024-04-01 01:40:33
  • Unity Shader中的基础光照与标准光照模型(自发光、环境光、高光反射、漫反射)、BRDF光照模型

    Unity Shader中的基础光照与标准光照模型(自发光、环境光、高光反射、漫反射)、BRDF光照模型

    我们是如何看到这个世界的 从宏观上来说,渲染包含了两大部分 决定一个像素的可见性 决定这个像素上的关照计算 而光照模型就是用于决定一个像素上进行怎样的光照计算 通常来讲,我们要模拟真实的光照环境来生成...

    2024-04-01 01:40:27