Binary System
Description
= (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
in this form following these principles:
- 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;
- The powers of 2 are always sorted in descending order .
Input
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的更多相关文章
- 十进制(decimal system)转换函数说明
一,十进制(decimal system)转换函数说明 1,十进制转二进制 decbin() 函数,如下实例 echo decbin(12); //输出 1100 echo decbin(26); / ...
- System.Data.DbType 与其它DbType的映射关系
System.Data.DbType 与其它DbType的映射关系 有如下类型的映射对照: System.Data.SqlClient.SqlDbType System.Data.OleDb.OleD ...
- HDOJ(HDU) 2106 decimal system(进制相互转换问题)
Problem Description As we know , we always use the decimal system in our common life, even using the ...
- decimal system 2016
Problem Description As we know , we always use the decimal system in our common life, even using the ...
- System.Data.DbType和数据库映射关系
有如下类型的映射对照: System.Data.SqlClient.SqlDbType System.Data.OleDb.OleDbType System.Data.Odbc.OdbcType S ...
- NHibernate之映射文件配置说明
NHibernate之映射文件配置说明 1. hibernate-mapping 这个元素包括以下可选的属性.schema属性,指明了这个映射所引用的表所在的schema名称.假若指定了这个属性, 表 ...
- G-FAQ – Why is Bit Depth Important?
直接抄: https://apollomapping.com/2012/August/article15.html For this month’s Geospatial Frequently Ask ...
- Good Bye 2015B(模拟或者二进制枚举)
B. New Year and Old Property time limit per test 2 seconds memory limit per test 256 megabytes input ...
- php进制转换函数
1 十进制(decimal system)转换函数 ① 十进制转二进制 string decbin(int number). 参数为一个十进制整型数字,不是整型数字会自动转为整型数字,如'3'转为3 ...
随机推荐
- POJ 2756 Autumn is a Genius 大数加减法
Description Jiajia and Wind have a very cute daughter called Autumn. She is so clever that she can d ...
- auto_ptr and scoped_ptr
#include "boost/scoped_ptr.hpp" #include <iostream> #include <memory>//contain ...
- 用XAML做网页!!—开篇
原文:用XAML做网页!!-开篇 这几日一直没发表新文章,一来是因为事比较多,二来就是我在研究使用XAML挑战传统HTML来做网页,这很可能是在全球的首次尝试,至少我从未找到任何可供参考的相关资料. ...
- php 禁止 URL 直接访问 php文件
通过判断访问来源来实现. $fromurl="http://www.111.com/index.php"; //只能从这个地址访问 if( $_SERVER['HTTP_REFER ...
- 使用CSS3制图
参考资料:http://blog.csdn.net/fense_520/article/details/37892507 本文非转载.为个人原创,转载请先联系博主,谢谢~ 准备: <!DOCTY ...
- J2SE基础:1.类和对象基础
什么是对象 在Java语言,全部的人,事物或者模块都是一个对象. 同样的对象具有一些同样的特性. 狗,猫,蛇3个对象(动物的对象) 苹果,梨,桔子3个对象(水果的对象) 什么是类 能够将现实生活中的对 ...
- Java使用LdAP获取AD域用户
随着我们的习大大上台后,国家在网络信息安全方面就有了非常明显的改变!所以如今好多做网络信息安全产品的公司和须要网络信息安全的公司都会提到用AD域server来验证,这里就简单的研究了一下! 先简单的讲 ...
- cocos2d-x3.0数据结构
1.cocos2d::Vector 1.头报价"CCVector.h"头文件. 2.保存的数据类型必须是cocos2d::Ref的子类. 3.实现是动态加入数据集合即链表.主要的使 ...
- 前端构建工具gulp
前端构建工具gulp使用 前端自动化流程工具,用来合并文件,压缩等. Gulp官网 http://gulpjs.com/ Gulp中文网 http://www.gulpjs.com.cn/ Gul ...
- silverlight与wcf双向通讯 例子
本文将建立一个silverlight与wcf双向通讯的简单实例,以下是详细步骤: 新建Silverlight应用程序,名称WCFtest.解决方案中添加WCF服务应用程序,名称WcfServiceTe ...