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 ...
随机推荐
- Hadoop云计算大数据书籍分享
1. 推荐书名 大数据云计算利器: Hadoop, The Definitive Guide, 1Ed.pdf(第1版) 大数据云计算利器: Hadoop, The Definitive ...
- SE 2014年5月25日
如图配置 两实验 R1模拟总部,R2 与R3模拟分部 实验一 要求使用 IPSec VPN 主模式,使得总部与两分部内网可相互通讯 步骤: 1. 配置默认路由 [RT1]ip route-stat ...
- Jersey框架二:Jersey对JSON的支持
Jersey系列文章: Jersey框架一:Jersey RESTful WebService框架简介 Jersey框架二:Jersey对JSON的支持 Jersey框架三:Jersey对HTTPS的 ...
- ARMv8 Linux内核异常处理过程分析
NOTE:为了方便大家阅读,制作了PDF版文档.下载请猛戳这里 老样子,为了赚点积分下载其它人的文件,下载以上资料须要资源分2分. 假设没有积分请留言全部文档,留下邮箱就可以. 看了Linaro提供的 ...
- java的提取与替换操作
public class Demo02 { public static void main(String args[]){ String str = "java 技术学习班 2007032 ...
- ok6410 u-boot-2012.04.01移植七完善u-boot移植(u-boot移植结束)
继ok6410 u-boot-2012.04.01移植六后,开发板已支持MLC NAND.DM9000等.但还需要完善比如环境变量.mtdpart分区.裁剪.制作补丁等.下面的工作就是完善移植的u-b ...
- OCP读书笔记(19) - 数据库空间管理
传输表空间:将linux下的数据库中的test表空间传输到windows平台下的数据库 在传输表空间前,先确定一下源库与目标数据库字符集一致: select * from nls_database_p ...
- JSP生成word文件
1.jsp生成word文件,直接改动jsp格式: <%@ page contentType="application/vnd.ms-word;charset=GB2312"% ...
- 线段树(单点更新and成段更新)
线段树需要的空间. 区间为1-->n 假设是一棵完全二叉树,且树高为i. 完全二叉树性质:第i层最多有2^(i-1)个结点. 那么 2^(i-1) = n; i = log2(n) + ...
- JavaScript 内存
JavaScript 中对内存的一些了解 在使用JavaScript进行开发的过程中,了解JavaScript内存机制有助于开发人员能够清晰的认识到自己写的代码在执行的过程中发生过什么,也能够提高项目 ...