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. 用cocos2d-x 3.2 实现的FlappyBird

    近期才開始学cocos2dx,买了几本书还有看大神(主要是 笨木头)的博客.然后就自己尝试用cocos2d-x实现了一下... (新手,勿喷...) 先看执行效果 http://pan.baidu.c ...

  2. java 线程 ProducerAndConsumer

    package j2se.thread.demo; /** * <p>Project:J2SE 的基础知识</p> * <p>Tile:多线程模拟 生产者 和 消费 ...

  3. Android于fragment_main.xml文件问题组件收购

    package com.dhy.phonedial; import android.app.Activity; import android.app.Fragment; import android. ...

  4. zimbra启用SMTP认证并绑定认证登录和发件人

    1. smtp认证    1.1 修改mynetworks        登录zimbra后台-->全局配置-->MTA-->信任网络-->127.0.0.0/8        ...

  5. LB 负载均衡的层次结构(转)

    作为后端应用的开发者,我们经常开发.调试.测试完我们的应用并发布到生产环境,用户就可以直接访问到我们的应用了.但对于互联网应用,在你的应用和用户之间还隔着一层低调的或厚或薄的负载均衡层软件,它们不显山 ...

  6. bootstrap在 刷新页面,tab选择页面不会改变。

    您可以直接复制代码 注意在同级别文件夹中引用 相应js 和 css. 实现tab影响 关键看bootstrap的 data-toggle= tab <html lang="en&quo ...

  7. Android在发送带有附件的邮件

    准备好工作了-下载最新的版本号JMail https://java.net/projects/javamail/pages/Home#Download_JavaMail_1.5.2_Release h ...

  8. Red Gate系列之八 SQL Connect 1.1.1.19 Edition 数据库连接及操作工具 完全破解+使用教程

    原文:Red Gate系列之八 SQL Connect 1.1.1.19 Edition 数据库连接及操作工具 完全破解+使用教程 Red Gate系列之八 SQL Connect 1.1.1.19 ...

  9. 【原创】leetCodeOj --- Largest Number 解题报告

    原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, ...

  10. LinearLayout具体解释一:LinearLayout的简单介绍

    LinearLayout,中文意思是线性布局.假设你是初学android的,肯定会非常困惑"啥叫布局",啥又叫"线性布局"呢. 有的时候,我尝试用官方的语言去解 ...