2236: Balance and Poise
来源:互联网 发布:mysql insert 中文列 编辑:程序博客网 时间:2024/05/19 05:04
Result
工匠大师Hill打造出一个精致的天平, 对他的徒弟Oren说:“你帮我做一些砝码配套,为了方便携带,砝码数目要尽量少”。
Oren:“好。我将按照2进制的规则打造,也就是1,2,4,8,16等等,这样在称量同等重量下使用的砝码数量最少,只使用7个砝码就可以称量1-127的重量。”Hill:“No, you are wrong”。
Oren:“难道还有更好的方法?2进制是最优的,这一点是可以证明的”
Hill:“如果只允许砝码放在天平的一端,那么你说的是对的,可是并没有人限制我们可以在天平的两端放置砝码。所以可以有更好的方法。如可以设计砝码重量为1,3,9,27,81,只用5个砝码就可以称量1-121”。
Oren:“原来是3进制,那怎么称重呢? 如100克重量?”
Hill:“在天平的左端放上重物,再放上砝码9,右端放砝码1, 27, 81”。
Oren:“嗯,可这是怎么算出来的呢?”
Hill:“It’s your business”。
现在,作为Oren的朋友,请你设计一个程序来计算在这种砝码下应如何称量。
Input:
输入包括多个整数,每个数n占据一行,代表要称重的重量。0 < n <= 1000000000。Output:
对于每个输入的整数,计算当此重量放置在天平左端时,应如何安排3进制的砝码,使天平平衡。每个结果输出一行,输出格式如例子所示,每个数字后面一个空格,如某一侧没有砝码,则输出0,多个砝码按重量升序排列。Sample Input:
10030
Sample Output:
9 left, 1 27 81 right.0 left, 3 27 right./*
3进制数,列几个数不难发现与该数模3的余数有关,余2的话,砝码加入左端,
并且给n+1后在继续模3,余1的时候加入右端,余0的时候不操作;
*/
#include <cstdio>#include <iostream>using namespace std;int main (){ int n,c[20]; c[0]=1; for (int i=1; i<20 ; i++) { c[i]=c[i-1]*3; } while (scanf("%d",&n)!=EOF) { int a[20],l[20],r[20],k=0,ll=0,rr=0;//l存左端,r存右端 while (n) { int t=n%3; if(t==2) {n=(n+1)/3;l[ll++]=c[k];}//余2时,砝码加入左端,并且给n+1后在继续模3 if(t==1) {n=n/3;r[rr++]=c[k];}//余1的时候加入右端 if(t==0)n=n/3; k++; } if(!ll)printf("0 left, "); else { for (int i=0 ; i<ll ; i++) printf("%d ",l[i]); printf("left, "); } if(!rr)printf("0 right./n"); else { for (int i=0 ; i< rr ; i++) printf("%d ",r[i]); printf("right./n"); } } return 0;}
- 2236: Balance and Poise
- JOJ2236:Balance and Poise
- Balance Continuity and Change
- apache + tomcat load balance and cluster
- Vysper cluster, load balance, and LVS
- Balance between code efficiency and code readability
- Balance
- balance
- Balance
- Balance between coverage more function and clear spec.
- Work and Life: 15 Ways to Strike a Balance
- install mongrel for rails and apache http for load balance
- Tomcat load balance and clustering - tomcat负载均衡和集群
- poj1028 Ignatius and the Princess III hdu1709The Balance
- linux irq balance and RT linux(中断线程化)
- Load Balance Tomcat with Nginx and Store Sessions in Redis
- mode manual load-balance and mode lacp-static
- The balance between the number of specs and the rate of coverage
- JSP中如何使用JQuery?
- 3D引擎
- Sql FAQ
- 内存管理2
- JSP、Servlet基础
- 2236: Balance and Poise
- 自定义Theme 与 使用
- 用GVIM替代Val(a)IDE!
- 内存管理1
- js技巧 和 C#读文件最后一个字符是回车
- 内存的基本概念
- Struts2中的值栈
- Directx11的warning:Resource View Is Already Bound To An OutputSlot
- 关于委托