欧拉项目 第17题 Number letter counts
2016-03-17 14:45
211 查看
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.1-1000换成one,two。。。,one thousand 这种新式,共有多少字符?
static String hund = "hundred"; static String and = "and"; static String[] one = {"one","two","three","four","five","six","seven","eight","nine"}; static String[] ten = {"ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}; static String[] t_2_0 = {"one","two","three","four","five","six","seven","eight","nine", "ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
static Integer hund1 = 7; static Integer and1 = 3; static Integer[] one1 = {3,3,5,4,4,3,5,5,4};//36 static Integer[] ten1 = {3,6,6,5,5,5,7,6,6};//49,去掉ten 46 static Integer[] t_2_01 = {3,3,5,4,4,3,5,5,4,3,6,6,8,8,7,7,9,8,8}; //106
long length = 11; for(int i=1;i<1000;i++) { length += tt1(i); }
public static Integer tt1(int i) { if(i < 100) { if(i < 20) { return t_2_01[i - 1]; } else if(i%10==0) { return ten1[i/10 - 1]; } else { return ten1[i/10 - 1] + one1[i%10 - 1]; } } else { if(i%100 == 0) { return one1[i/100 - 1] + hund1; } else if(i%100 < 20) { return one1[i/100 - 1] + hund1 + and1 + t_2_01[i%100 - 1]; } else if(i%10==0) { return one1[i/100 - 1] + hund1 + and1 + ten1[(i%100)/10 - 1]; } else { return one1[i/100 - 1] + hund1 + and1 + ten1[(i%100)/10 - 1] + one1[(i%100)%10 - 1]; } } }
public static String tt(int i) { //得出数字字符 if(i < 100) { if(i < 20) { return t_2_0[i - 1]; } else if(i%10==0) { return ten[i/10 - 1]; } else { return ten[i/10 - 1] + one[i%10 - 1]; } } else { if(i%100 == 0) { return one[i/100 - 1] + hund; } else if(i%100 < 20) { return one[i/100 - 1] + hund + and + t_2_0[i%100 - 1]; } else if(i%10==0) { return one[i/100 - 1] + hund + and + ten[(i%100)/10 - 1]; } else { return one[i/100 - 1] + hund + and + ten[(i%100)/10 - 1] + one[(i%100)%10 - 1]; } } }可以循环上面方法运算可以归纳下,1-19 有 10个 , 20-90 有 100个, 1-9 有 80个(1-9算在1-19里面),100-900有100个,and有99 * 9个,1000一个结果就是106 * 10 + 46 * 100 + 36 * 80 + 99 * 100 + 3 * 99 * 9 + 11
相关文章推荐
- MySQL重装复原教程
- Linux查找含有某字符串的所有文件
- <div> <Span>
- @Temporal 注解使用
- tomcat java.lang.OutOfMemoryError: PermGen space
- Ping用法大全
- 设计模式:访问者模式
- 如何让Maven生成JAR包时打包资源文件
- 关于tableView点击状态栏列表回到顶部的说明
- JavaScript Array(数组) 对象
- ubuntu 修改文件目录所有者/权限
- Docker 基于已有镜像的容器创建镜像
- 安卓文本样式-Spannable的使用
- Git傻瓜教程
- C#中out和ref之间的区别
- java: final
- 石油化工行业需要怎样的工作流平台?
- 在项目开发过程中的版本控制
- 1001WA
- CSS3学习笔记之径向展开菜单