project euler 113
2016-01-01 00:05
453 查看
Non-bouncy numbers
Working from left-to-right if no digit is exceeded by the digit to its left it is called an increasing number; for example, 134468.
Similarly if no digit is exceeded by the digit to its right it is called a decreasing number; for example, 66420.
We shall call a positive integer that is neither increasing nor decreasing a “bouncy” number; for example, 155349.
As n increases, the proportion of bouncy numbers below n increases such that there are only 12951 numbers below one-million that are not bouncy and only 277032 non-bouncy numbers below 1010.
How many numbers below a googol (10100) are not bouncy?
非弹跳数
从左往右,如果每一位数字都大于等于其左边的数字,这样的数被称为上升数,比如134468。
同样地,如果每一位数字都大于等于其右边的数字,这样的数被称为下降数,比如66420。
如果一个正整数既不是上升数也不是下降数,我们就称之为“弹跳”数,比如155349。
随着n的增长,小于n的弹跳数的比例也随之增长;在小于一百万的数中,只有12951个非弹跳数,而小于1010的数中只有277032个非弹跳数。
在小于一古戈尔(10100)的数中有多少个非弹跳数?
Working from left-to-right if no digit is exceeded by the digit to its left it is called an increasing number; for example, 134468.
Similarly if no digit is exceeded by the digit to its right it is called a decreasing number; for example, 66420.
We shall call a positive integer that is neither increasing nor decreasing a “bouncy” number; for example, 155349.
As n increases, the proportion of bouncy numbers below n increases such that there are only 12951 numbers below one-million that are not bouncy and only 277032 non-bouncy numbers below 1010.
How many numbers below a googol (10100) are not bouncy?
非弹跳数
从左往右,如果每一位数字都大于等于其左边的数字,这样的数被称为上升数,比如134468。
同样地,如果每一位数字都大于等于其右边的数字,这样的数被称为下降数,比如66420。
如果一个正整数既不是上升数也不是下降数,我们就称之为“弹跳”数,比如155349。
随着n的增长,小于n的弹跳数的比例也随之增长;在小于一百万的数中,只有12951个非弹跳数,而小于1010的数中只有277032个非弹跳数。
在小于一古戈尔(10100)的数中有多少个非弹跳数?
package projecteuler; import java.math.BigInteger; import junit.framework.TestCase; public class Prj113 extends TestCase { /**[a1,a2,a3,....an], 1<=a1<=a2<=a3<=...an<=9 * map to ---> [d + i -1] * one to one map ===>rC(n+r-1) */ public void testNonBouncyNumbers() { assertEquals(calNumOfNonbouncy(10, 6).toString(), "12951"); assertEquals(calNumOfNonbouncy(10, 10).toString(), "277032"); System.out.println(calNumOfNonbouncy(10, 100)); } BigInteger calNumOfNonbouncy(int _n, int _r) { BigInteger ret = BigInteger.ZERO; int n = _n; int r = _r; BigInteger asc = calNr(n + r - 1, r); // zero out ret = ret.add(asc).subtract(BigInteger.ONE); // [9,8,7,6,5,4,3,2,1] n = n - 1; //倒序补上0 for (int i = 1; i <= r; i++) { BigInteger desc = calNr(n + i - 1, i).multiply( new BigInteger(Integer.toString(r + 1 - i))); ret = ret.add(desc); } ret = ret.subtract(new BigInteger(Integer.toString(_r * (_n - 1)))); return ret; } BigInteger calNr(int _n, int _r) { BigInteger ret = BigInteger.ONE; for (int i = 1; i <= _r; i++) { ret = ret.multiply(new BigInteger(Integer.toString(_n + 1 - i))); } for (int i = 1; i <= _r; i++) { ret = ret.divide(new BigInteger(Integer.toString(i))); } return ret; } }
相关文章推荐
- README
- Apache日志切割
- project euler 112
- 20.scala编程思想笔记——Vector
- project euler 111
- python自动化测试,自动化框架及工具
- 一个苦逼运维的2015结束语——写给过去和未来的自己
- 19.scala编程思想笔记——for循环
- Android自定义View实现仿GitHub的提交活跃表格
- 正则表达式的字符串替换方法
- 前端必学之PHP语法基础
- 详解Sql基础语法
- 实例详解Group by和Having子句
- 实例详解JavaScript获取链接参数的方法
- Javascript获取数组中的最大值和最小值的方法汇总
- 基于JavaScript实现定时跳转到指定页面
- 详解JavaScript 中的 replace 方法
- project euler 110
- 使用步骤
- 基于jQuery实现返回顶部实例代码