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:
Here comes the problem:
Given a list of phone numbers and the dividing formats, output the right ways to read these numbers.
Rules:
Input:
The first line of the input gives the number of test cases, T. T 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
Sample:
One Solution:
基本上這像是送分的, 只要會 coding 應該都解得出來. 沒有太多的技巧與數學在裡面. 唯一要注意的是 "連續10個以上" 還是個別念, 所以 11111111111 會是 11 個 one 而不是:
另外再處理 range 的時候, 不要把前面的連續帶到後面來, 如 "111 112" 應該是:
只要夠細心分數就拿得到, 底下是我自己的範例代碼:
- package cj2014;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import flib.util.io.QSReader;
- import flib.util.io.QSWriter;
- public class PA {
- public static List
Lz = new ArrayList(); - public static Map
DigMap = new HashMap(); - static {
- // 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.
- Lz.add("double");
- Lz.add("triple");
- Lz.add("quadruple");
- Lz.add("quintuple");
- Lz.add("sextuple");
- Lz.add("septuple");
- Lz.add("octuple");
- Lz.add("nonuple");
- Lz.add("decuple");
- DigMap.put('0', "zero");
- DigMap.put('1', "one");
- DigMap.put('2', "two");
- DigMap.put('3', "three");
- DigMap.put('4', "four");
- DigMap.put('5', "five");
- DigMap.put('6', "six");
- DigMap.put('7', "seven");
- DigMap.put('8', "eight");
- DigMap.put('9', "nine");
- }
- /**
- * BD: https://code.google.com/codejam/contest/2924486/dashboard#s=p0
- * @param args
- */
- public static void main(String[] args) throws Exception{
- String fn="A-large-practice.in";
- QSReader qsr = new QSReader(String.format("data/2014/%s", fn)); // 讀入 input
- QSWriter qsw = new QSWriter(String.format("data/2014/%s.out", fn)); // 寫出 output
- qsr.open();
- Integer pc = Integer.valueOf(qsr.line()); // T
- for(int i=1; i<=pc; i++)
- {
- int sc=0;
- char pvc='-';
- StringBuffer lineBuf = new StringBuffer();
- String qs[] = qsr.line().split(" "); // N
- String fs[] = qs[1].split("-"); // F
- char[] ns = qs[0].toCharArray();
- int ci=0;
- for(int j=0; j
- {
- int r = Integer.valueOf(fs[j]);
- for(int k=0; k
- {
- if(pvc=='-')
- {
- pvc = ns[ci];
- sc++;
- }
- else if(pvc == ns[ci]) sc++;
- else
- {
- String line="";
- if(sc==1) line = String.format("%s ", DigMap.get(pvc));
- else if(sc<=10) line = String.format("%s %s ", Lz.get(sc-2), DigMap.get(pvc));
- else for(int q=0; q<(sc); q++) line = line + DigMap.get(pvc) + " ";
- lineBuf.append(line);
- pvc = ns[ci];
- sc=1;
- }
- ci++;
- }
- if(pvc!='-')
- {
- if(sc==1) lineBuf.append(String.format("%s ", DigMap.get(pvc)));
- else if(sc<=10) lineBuf.append(String.format("%s %s ", Lz.get(sc-2), DigMap.get(pvc)));
- else for(int q=0; q
"%s ", DigMap.get(pvc))); - }
- pvc='-';
- sc=0;
- }
- qsw.line(String.format("Case #%d: %s", i, lineBuf.toString().trim()));
- }
- qsr.close();
- qsw.close();
- }
- }
沒有留言:
張貼留言