算法分析
模拟
- 将给定的字符串的每位数字进行相加得到
res
,并将res
赋值为字符串形式,从左往右枚举字符串的每个元素,输出该数字对应的映射值,如1
输出one
时间复杂度 $O(n)$
参考文献
pat
Java 代码
import java.util.Scanner;
public class Main {
static String[] word = new String[] {"zero","one","two","three","four","five","six","seven","eight","nine"};
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.next();
int res = 0;
for(int i = 0;i < s.length();i ++)
{
res += s.charAt(i) - '0';
}
s = "" + res;
for(int i = 0;i < s.length();i ++)
{
if(i != s.length() - 1) System.out.print(word[s.charAt(i) - '0'] + " ");
else System.out.print(word[s.charAt(i) - '0']);
}
}
}