Description

Usually we use number in the decimal system, for it is so convenient for us to remember and calculate.
But it is not the same in the computer world where numbers are always stored in the binary system. For example, the number 21 in decimal can be presented as (21)10
= (10101)2 = 24+22+20. It is the sum of the power of 2. Note that in the first item, the power is 4, then the number 4 can be presented as (4)10
= (100)2=22, so , and it is much more convenient for computer to display as 21=2(2(2))+2(2)+2(0). Every positive integer can be written
in this form following these principles: 
  1. Number 1 is presented as 2(0), while number 2 is presented as 2. Then other numbers must be combined by these two basic numbers;
  2. The powers of 2 are always sorted in descending order .

Input

Each line of the Input is the number n (0 < n < 1000000) in the binary system. Input file is ended with -1.

Output

For each case, you should only export the equation as the sample output. Be careful of the space before and after the equal sign. And there mustn’t be any more space in your output.

Sample Input

8
21
1315
-1

Sample Output

8 = 2(2+2(0))
21 = 2(2(2))+2(2)+2(0)
1315 = 2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)

题意:把一个十进制的数转化成以2为底的若干个整数和。

思路:递归。n=2(a)+2(b)+...+2(x)。而a,b,...,x分别又是相当于n。注意边界。

#include<stdio.h>
void work(int n)
{
int a[30],i=0;
while(n>0) {
a[i++]=n%2;
n/=2;
}
for(int j=i-1;j>=0;j--)
if(a[j]) {
if(j<i-1) printf("+");
if(j!=1 && j!=0) {
printf("2(");
work(j);
printf(")");
}
else if(j==1) printf("2");
else printf("2(0)");
}
}
int main()
{
int n;
while(~scanf("%d",&n) && n!=-1) {
printf("%d = ",n);
work(n);
printf("\n");
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

Binary System的更多相关文章

  1. 十进制(decimal system)转换函数说明

    一,十进制(decimal system)转换函数说明 1,十进制转二进制 decbin() 函数,如下实例 echo decbin(12); //输出 1100 echo decbin(26); / ...

  2. System.Data.DbType 与其它DbType的映射关系

    System.Data.DbType 与其它DbType的映射关系 有如下类型的映射对照: System.Data.SqlClient.SqlDbType System.Data.OleDb.OleD ...

  3. HDOJ(HDU) 2106 decimal system(进制相互转换问题)

    Problem Description As we know , we always use the decimal system in our common life, even using the ...

  4. decimal system 2016

    Problem Description As we know , we always use the decimal system in our common life, even using the ...

  5. System.Data.DbType和数据库映射关系

    有如下类型的映射对照: System.Data.SqlClient.SqlDbType  System.Data.OleDb.OleDbType System.Data.Odbc.OdbcType S ...

  6. NHibernate之映射文件配置说明

    NHibernate之映射文件配置说明 1. hibernate-mapping 这个元素包括以下可选的属性.schema属性,指明了这个映射所引用的表所在的schema名称.假若指定了这个属性, 表 ...

  7. G-FAQ – Why is Bit Depth Important?

    直接抄: https://apollomapping.com/2012/August/article15.html For this month’s Geospatial Frequently Ask ...

  8. Good Bye 2015B(模拟或者二进制枚举)

    B. New Year and Old Property time limit per test 2 seconds memory limit per test 256 megabytes input ...

  9. php进制转换函数

    1 十进制(decimal system)转换函数 ① 十进制转二进制 string  decbin(int number). 参数为一个十进制整型数字,不是整型数字会自动转为整型数字,如'3'转为3 ...

随机推荐

  1. 【剑指offer】旋转数组的最小值

    採用二分查找的策略,重点要考虑一些边界情况:旋转了0元素.即输入的是一个升序排列的数组.仅仅包括一个数字的数组.有非常多反复数字的数组等. AC代码: #include<stdio.h> ...

  2. hdu 4961 Boring Sum(数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4961 Problem Description Number theory is interesting ...

  3. Learning Cocos2d-x for WP8(4)——中文显示

    原文:Learning Cocos2d-x for WP8(4)--中文显示 C#(wp7)兄弟篇Learning Cocos2d-x for XNA(4)——中文显示 Cocos2d-x中文显示,似 ...

  4. SVN 的revert操作

  5. Allegro绘制PCB流程

    单位换算 1mil = 0.0254 mm 1mm = 39.3701 mil 默认情况下我们更倾向于使用mil单位绘制PCB板. 1 新建工程,File --> New... --> [ ...

  6. HUNNU11354:Is the Name of This Problem

    http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11354&courseid=0 Problem des ...

  7. 通信协议:HTTP、TCP、UDP(转)

    原文出处: 碧雪轩的博客 TCP   HTTP   UDP: 都是通信协议,也就是通信时所遵守的规则,只有双方按照这个规则“说话”,对方才能理解或为之服务. TCP   HTTP   UDP三者的关系 ...

  8. 《5》CentOS7.0+OpenStack+kvm云平台的部署—组态Horizon

    感谢朋友支持本博客,欢迎共同探讨交流,因为能力和时间有限,错误之处在所难免,欢迎指正! 假设转载.请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...

  9. 广域网佰腾玩O2O笑话——它看起来很漂亮,注定不低于

    据说 2014.8.29:中国最大的商业运营商万达在一起的互联网服务供应商百度.腾讯在深圳(属性)战略合作签约仪式举行. 从功能表面上.万达代表实体,百度代表数据.腾讯代表社区:按三个合伙人理解,是要 ...

  10. JavaScript语言基础知识10

    JavaScript中间if声明: <span style="font-size:18px;"><HTML> <HEAD> <TITLE& ...