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. Hadoop云计算大数据书籍分享

    1. 推荐书名    大数据云计算利器: Hadoop, The Definitive Guide, 1Ed.pdf(第1版)    大数据云计算利器: Hadoop, The Definitive ...

  2. SE 2014年5月25日

    如图配置 两实验 R1模拟总部,R2 与R3模拟分部 实验一  要求使用 IPSec VPN 主模式,使得总部与两分部内网可相互通讯 步骤: 1.  配置默认路由 [RT1]ip route-stat ...

  3. Jersey框架二:Jersey对JSON的支持

    Jersey系列文章: Jersey框架一:Jersey RESTful WebService框架简介 Jersey框架二:Jersey对JSON的支持 Jersey框架三:Jersey对HTTPS的 ...

  4. ARMv8 Linux内核异常处理过程分析

    NOTE:为了方便大家阅读,制作了PDF版文档.下载请猛戳这里 老样子,为了赚点积分下载其它人的文件,下载以上资料须要资源分2分. 假设没有积分请留言全部文档,留下邮箱就可以. 看了Linaro提供的 ...

  5. java的提取与替换操作

    public class Demo02 { public static void main(String args[]){ String str = "java 技术学习班  2007032 ...

  6. ok6410 u-boot-2012.04.01移植七完善u-boot移植(u-boot移植结束)

    继ok6410 u-boot-2012.04.01移植六后,开发板已支持MLC NAND.DM9000等.但还需要完善比如环境变量.mtdpart分区.裁剪.制作补丁等.下面的工作就是完善移植的u-b ...

  7. OCP读书笔记(19) - 数据库空间管理

    传输表空间:将linux下的数据库中的test表空间传输到windows平台下的数据库 在传输表空间前,先确定一下源库与目标数据库字符集一致: select * from nls_database_p ...

  8. JSP生成word文件

    1.jsp生成word文件,直接改动jsp格式: <%@ page contentType="application/vnd.ms-word;charset=GB2312"% ...

  9. 线段树(单点更新and成段更新)

    线段树需要的空间. 区间为1-->n 假设是一棵完全二叉树,且树高为i. 完全二叉树性质:第i层最多有2^(i-1)个结点. 那么 2^(i-1) = n;     i = log2(n)  + ...

  10. JavaScript 内存

    JavaScript 中对内存的一些了解 在使用JavaScript进行开发的过程中,了解JavaScript内存机制有助于开发人员能够清晰的认识到自己写的代码在执行的过程中发生过什么,也能够提高项目 ...