public class Binary
{
public static void main(String[] args)
{ // Print binary representation of N.
int N = Integer.parseInt(args[0]);
int v = 1;
while(v <= N/2)
v = 2*v;
// Now v is the largest power of 2 <= N.
int n = N; // current excess
while (v > 0)
{ //Cast out the power of 2 in decreasing order.
if (n < v) { System.out.print(0); }
else { System.out.print(1); n-=v;}
v = v/2;
}
System.out.println();
}
}

打印10进制数字(decimal number)的二进制表示。将数字拆成2的幂次的和的形式。例如 19 = 16 + 2 + 1. 所以 19 的二进制表示为 10011. 

  

[Introduction to programming in Java 笔记] 1.3.7 Converting to binary 十进制到二进制的转换的更多相关文章

  1. [Introduction to programming in Java 笔记] 1.3.9 Factoring integers 素因子分解

    素数 A prime is an integer greater than one whose only positive divisors are one and itself.整数的素因子分解是乘 ...

  2. [Introduction to programming in Java 笔记] 1.3.8 Gambler's ruin simulation 赌徒破产模拟

    赌徒赢得机会有多大? public class Gambler { public static void main(String[] args) { // Run T experiments that ...

  3. Thinking in Java——笔记(1)

    Introduction To Obejct The progress of abstraction But their primary abstraction still requires you ...

  4. java笔记整理

    Java 笔记整理 包含内容     Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...

  5. Effective Java笔记一 创建和销毁对象

    Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...

  6. Java笔记(一)

    Java语言特征 Java之父:詹姆斯,格斯林 跨平台:一次编译,到处运行write once,run everywhere! Java是一种面向对象的编程语言(OOP)面向对象(OO -- Orie ...

  7. java笔记00-目录

    --2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:

  8. 每天一本电子书 - JavaScript for Kids: A Playful Introduction to Programming

    JavaScript for Kids: A Playful Introduction to Programming 作者: Nick Morgan  出版社: No Starch Press 副标题 ...

  9. 2018-12-09 疑似bug_中文代码示例之Programming in Scala笔记第九十章

    续前文: 中文代码示例之Programming in Scala笔记第七八章 源文档库: program-in-chinese/Programming_in_Scala_study_notes_zh ...

随机推荐

  1. Ubuntu全新系统一些配置

    0.安装JDK,http://www.oracle.com/technetwork/java/javase/downloads/index.html 1.安装Intellij IDEA,https:/ ...

  2. [App]Android Studio First App

    准备着看Android Studio的体验如何. 通过Android Studio构建一个默认的项目,添加一些元素 <RelativeLayout xmlns:android="htt ...

  3. JavaScript浏览器本地数据存储

    浏览器本地存储主要使用的是sessionStorage和localStorage.两者都支持,sessionStorage保存的是浏览器和服务器的一次对话信息,只在一次回话中有效.当在新标签页或新窗口 ...

  4. JAVA Web项目的编码过滤器

    首先写一个EncodeFilter的过滤类: package com.djtu.wy.common; import java.io.IOException;import javax.servlet.F ...

  5. How to effectively work with multiple files in Vim?

    Why not use tabs (introduced in Vim 7)? You can switch between tabs with :tabn and :tabp, With :tabe ...

  6. weekend110(Hadoop)的 第五天笔记

    (2015年1月24日) 课程目录 01-zookeeper1 02-zookeeper2 03-NN高可用方案的要点1 04-hadoop-HA机制的配置文件 05-hadoop分布式集群HA模式部 ...

  7. 如何运用同余定理求余数【hdoj 1212 Big Number【大数求余数】】

    Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  8. Echarts双轴图的配置.

    在series中的数据组加个属性,yAxisIndex: 1,表示第二个数据用的是第二个y轴,不加这个属性的话都是默认0,即第一个属性,再配置yAxis的值第一个为左侧坐标轴,右册坐标轴为第二个配置参 ...

  9. myEclipse和eclipse修改或复制项目名称后-更新部署名称

    一.myEclipse 复制后修改名称,访问不到项目 这是因为,你只是改了项目的名称,而没有改 下面是解决方法: 方法 1.右击你的项目,选择“properties”,在“type filter te ...

  10. web配置nagios工具

    Nagios是一款开源的免费网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等.在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员, ...