数的读法-题解

基础练习 数的读法  
时间限制:1.0s   内存限制:512.0MB
问题描述
  Tom教授正在给研究生讲授一门关于基因的课程,有一件事情让他颇为头疼:一条染色体上有成千上万个碱基对,它们从0开始编号,到几百万,几千万,甚至上亿。
  比如说,在对学生讲解第1234567009号位置上的碱基时,光看着数字是很难准确的念出来的。
  所以,他迫切地需要一个系统,然后当他输入12 3456 7009时,会给出相应的念法:
  十二亿三千四百五十六万七千零九
  用汉语拼音表示为
  shi er yi san qian si bai wu shi liu wan qi qian ling jiu
  这样他只需要照着念就可以了。
  你的任务是帮他设计这样一个系统:给定一个阿拉伯数字串,你帮他按照中文读写的规范转为汉语拼音字串,相邻的两个音节用一个空格符格开。
  注意必须严格按照规范,比如说“10010”读作“yi wan ling yi shi”而不是“yi wan ling shi”,“100000”读作“shi wan”而不是“yi shi wan”,“2000”读作“er qian”而不是“liang qian”。
输入格式
  有一个数字串,数值大小不超过2,000,000,000。
输出格式
  是一个由小写英文字母,逗号和空格组成的字符串,表示该数的英文读法。
样例输入
1234567009
样例输出
shi er yi san qian si bai wu shi liu wan qi qian ling jiu
解题思路: 将输入的字符串转化成10位的字符串,对其进行统一的处理。 例如: 
Sample Input:  7009   ------  nnnnnn7009
Sample Input:  63635858 ------ nn63635858
Sample Input:  1234567009 ------- 1234567009
 这样将接收到的字符串,变成10位的字符串,n表示不存在该位,转换成拼音, 最后过滤即可。
源码(JAVA):
import java.util.Scanner;


/**
 * 
 * 
 *   12 3456 7009
 *   20 0000 0000
 */
public class Asist
{


public static String nums1[] = {"ling", "yi", "er", "san", "si", "wu","liu","qi","ba","jiu","shi"};
public static String nums2[] = {"shi","bai","qian","wan","yi"};
// 20 0000 0000 , 21 0000 0000, 20 0010 0001
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
sc.close();
        
while(str.length() < 10)
{
str = "n"+str;
}

String re = fun(str);
System.out.println(check(re));
}

// 12 3456 7009
private static String fun(String str)
{
String re = "";
int ch;

// 1     12 3456 7009
if (str.charAt(0) != 'n')
{
ch = str.charAt(0) - '0';
re += nums1[ch] + " " + nums2[0]+" ";
}
    // 2     12 3456 7009
if (str.charAt(1) != 'n')
{
ch = str.charAt(1) - '0';
if (ch == 0) re += nums2[4] + " ";
else
re += nums1[ch] + " "+nums2[4]+" ";
}
    // 3     12 3456 7009
if (str.charAt(2) != 'n')
{
ch = str.charAt(2) - '0';
if (ch == 0) re += nums1[ch] +" ";
else
re += nums1[ch] + " "+nums2[2]+" ";
}
    // 4     12 3456 7009
if (str.charAt(3) != 'n')
{
ch = str.charAt(3) - '0';
if (ch == 0) re += nums1[ch] +" ";
else
re += nums1[ch] + " "+nums2[1]+" ";
}
    // 5     12 3456 7009
if (str.charAt(4) != 'n')
{
ch = str.charAt(4) - '0';
if (ch == 0) re += nums1[ch] +" ";
else
re += nums1[ch] + " "+nums2[0]+" ";
}
    // 6     12 3456 7009
if (str.charAt(5) != 'n')
{
ch = str.charAt(5) - '0';
if (ch == 0) re += nums2[3] +" ";
else
re += nums1[ch] + " "+nums2[3]+" ";
}
    // 7     12 3456 7009
if (str.charAt(6) != 'n')
{
ch = str.charAt(6) - '0';
if (ch == 0) re += nums1[ch] +" ";
else
re += nums1[ch] + " "+nums2[2]+" ";
}
    // 8     12 3456 7009
if (str.charAt(7) != 'n')
{
ch = str.charAt(7) - '0';
if (ch == 0) re += nums1[ch] +" ";
else
re += nums1[ch] + " "+nums2[1]+" ";
}
    // 9     12 3456 7009
if (str.charAt(8) != 'n')
{
ch = str.charAt(8) - '0';
if (ch == 0) re += nums1[ch] +" ";
else
re += nums1[ch] + " "+nums2[0]+" ";
}
    // 10     12 3456 7009
if (str.charAt(9) != 'n')
{
ch = str.charAt(9)-'0';
re += nums1[ch];
}
return re;
}

public static String check(String re)
{
// 处理末尾 'ling'
    while (re.trim().endsWith("ling"))
    {
    re = re.substring(0, re.trim().length()-4);
    }
    // 处理连续的'ling'
    while (re.trim().indexOf("ling ling")!=-1)
    {
    re = re.replaceAll("ling ling", "ling");
    }
    // 处理'ling wan'
    if (re.trim().indexOf("ling wan")!=-1)
    {
    re = re.replaceAll("ling wan", "wan");
    }
    // 处理 'yi shi er wan' 去掉 'yi'
    if (re.trim().startsWith("yi shi"))
    {
    re = re.substring(3, re.length());
    }
    return re;
}
}
<think>好的,用户需要PT-1的题解或练习题解析。首先得确定PT-1具体指什么,可能是指某个编程题库的第1题,或者某个教材、竞赛中的题目。常见的PT-1可能出现在PAT(浙江大学程序设计能力考试)或者PTA平台的题目中。需要进一步确认,但用户没有提供更多信息,所以得假设是常见的可能性。 接下来,调用搜索工具查找“PT-1 题解”、“PT-1 练习题解析”等相关内容。搜索结果中发现,PT-1常与PAT乙级或甲级考试中的第一题相关,比如A+B类型的问题。例如,PAT乙级1001题是“害死人不偿命的(3n+1)猜想”,这类题目通常需要循环和条件判断。 另外,PTA平台上的题目可能涉及不同的算法,比如PT-1可能考察基础输入输出、简单学运算或模拟题。例如,PT-1可能要求处理字符串反转、字各位求和等基础操作。需要根据不同来源的题解,整理出常见的解题思路和代码示例。 还要注意用户可能需要的答案结构,需要分方法或步骤,避免使用第一人称。可能需要将不同题型的解法归类,比如学问题、字符串处理、组操作等,并给出对应的代码示例和公式。例如,对于涉及论的题目,使用学公式描述算法步骤,如$n = n / 2$(偶)和$n = (3n + 1)/2$(奇),直到n变为1,统计步。 同,要确保代码格式正确,用三个反引号包裹,并注明语言类型。比如Python或C++的实现。此外,在最后生成相关问题,需基于PT-1的可能扩展,比如具体题号的解析、不同难度题目的策略,或者常见错误分析。 最后,整合所有信息,确保回答清晰、分步骤,并符合格式要求,避免引用堆砌,每个方法间换行,正确使用学公式和代码块。相关问题部分要列出三个相关且具体的问题,帮助用户进一步查找所需内容。</think>根据网络搜索结果,PT-1通常指程序设计类考试/练习中的基础题型。以下是针对PT-1常见题型的解析方法及代码实现: **值计算类题型** 典型例题:实现输入两个整A和B,输出A+B的结果 ```python a, b = map(int, input().split()) print(a + b) ``` 此类题目重点考察输入输出格式处理,注意特殊据边界值测试 **条件判断类题型** 常见考法:给定字n,判断其是否为素 学判定条件:若n不能被$2 \leq i \leq \sqrt{n}$范围内的任意整整除则为素 ```c++ bool isPrime(int n) { if(n <= 1) return false; for(int i=2; i*i<=n; i++) if(n%i == 0) return false; return true; } ``` **循环控制类题型** 经典案例:3n+1猜想(PAT乙级1001) 处理流程: 1. 输入正整n 2. 循环执行: - 当n为偶:$n = n/2$ - 当n为奇:$n = (3n + 1)/2$ 3. 统计操作步直到n=1 ```python n = int(input()) count = 0 while n != 1: n = n//2 if n%2 ==0 else (3*n+1)//2 count +=1 print(count) ``` **字符串处理类题型** 常见形式:字符串反转/统计特定字符 ASCII码转换公式:$ch_{new} = (ch_{original} + shift) \% 128$ ```java public static String reverseString(String s) { return new StringBuilder(s).reverse().toString(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值