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 ...
随机推荐
- 2014鞍山现场赛C题HDU5072(素筛+容斥原理)
Coprime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total ...
- 前端编程提高之旅(六)----backbone实现todoMVC
乐帝当年学习backbone时.最開始是看官网todoMVC的实现.后来了解到requireJS便于管理JS代码.就对官网代码做了requireJS管理.但此时乐帝感觉此时的t ...
- Android之查看外部依赖jar的源代码_android private libralies does not allow modifications to source
在Eclipse中开发android的应用程序时,有时想查看函数的内部实现.可是当在函数上点击ctrl和鼠标左键的时候. 往往出现例如以下提示: Class File Editor Source no ...
- abstract修饰方法总结
abstract这种方法修饰,主要用在抽象类和抽象方法. 抽象的类是不可实例化的比如 public abstract class Test{ } 他能够含有抽象的方法 public abstract ...
- android平台TextView使用ImageSpan画廊GIF图像
android-gif-drawable(https://github.com/koral--/android-gif-drawable/releases)开源项目---是一个蛮不错的android ...
- paip.关于动画效果的原则 html js 框架总结
paip.关于动画效果的原则 html js 框架总结 1. 动画框架的来源:flex,jqueryui 3 2. 特效的分类 3 2.1. Property effects 动态改变一个或多个目标对 ...
- C++ Primer高速学习 第一章 获得二:输入和输出 (IO)
什么是输入输出.即Input-Output,缩写是非常装B的IO?请看经典民间解释: C++语言的输入输出是指信息从外部输入设备(如键盘.磁盘等)向计算机内部(内存)输入(即Input)和从内存向外单 ...
- 【夸QT十一】外来物品:通用脚本帮助Web运行基础Linux命令
需求分析: 需要注意的是在这里第一次,这个人是不是QT系列文章,它是关于Web的,之所以写这篇文章.这是因为碍着Web相关开发时间,而且往往涉及linux与底层指令处理.例如,创建一个文件夹,删除一个 ...
- Linux Kernel(Android) 加密算法汇总(四)-应用程序调用OpenSSL加密演算法
Linux Kernel(Android) 加密算法总结(三)-应用程序调用内核加密算法接口 讲到了怎样调用内核中的接口的方法. 本节主要是介绍怎样Android C/C++应用程序调用Openssl ...
- Java乔晓松-android中获取图片的缩略图(解决OutOfMemoryError)内存溢出的Bug
由于android获取图片过大是会出现内存溢出的Bug 07-02 05:10:13.792: E/AndroidRuntime(6016): java.lang.OutOfMemoryError 解 ...