primitive calculator
来源:互联网 发布:ff14国服数据库 编辑:程序博客网 时间:2024/06/05 00:14
Problem Description
Task.
Given an integer n, compute the minimum number of operations needed to obtain the number n
starting from the number 1.
Input Format.
The input consists of a single integer 1 n 106.
Output Format.
In the rst line, output the minimum number k of operations needed to get n from 1.
In the second line output a sequence of intermediate numbers. That is, the second line should contain
positive integers a0; a2; : : : ; ak-1�� such that a0 = 1, ak��1 = n and for all 0 i < k �� 1, ai+1 is equal to
either ai + 1, 2ai, or 3ai. If there are many such sequences, output any one of them.
Sample 1.
Input:
1
Output:
0
1
Sample 2.
Input:
5
Output:
3
1 2 4 5
Sample 3.
Input:
96234
Output:
14
1 3 9 10 11 22 66 198 594 1782 5346 16038 16039 32078 96234
a=int(input())i=0b=[]#c=[]b.append(0)b.append(0)#print(b)for i in range(2,a+1): c=[] if i%3==0: c.append(int(i/3)) if i%2== 0: c.append(int(i/2)) c.append(i-1) #print(c) if i%3==0 and i%2==0: b.append(min(b[c[0]],b[c[1]],b[c[2]])+1) elif i%3==0 or i%2==0: b.append(min(b[c[0]],b[c[1]])+1) else: b.append(b[c[0]]+1) #print(b)#print(min(c))print(b[a])
用一个数组存储每一步的最优解,有三种一步到达i的方法,*3 和 *2 和 +1,对应每一个i,算出对应的i/3和i/2和i-1的最优解如果都为整数的话。取三个最优解的最小值,则到i的最优解+1即可。
- primitive calculator
- Calculator
- Calculator
- Calculator
- Calculator
- Calculator
- Calculator
- calculator
- Calculator
- Calculator
- calculator
- Calculator
- Calculator
- Calculator| ||
- Calculator
- Calculator
- Calculator
- Primitive roots
- StringBuffer类和String类的效率对比
- 今日BBC
- 电子入门基础知识之:USB接口大搜罗
- ucenter api注册Discuz免激活登录
- oracle trunc()函数的使用
- primitive calculator
- 浏览器设置网络代理
- cocos2d-x 大图创建帧动画
- 将一个字符串,左旋N个字符
- 阶乘的一些规律(编程之美)
- Problem A-1000
- GraphicsMagic安装
- 初步了解ReactJS
- A strange lift BNUOJ 5746