Cracking the coding interview--Q5.2

Google面试之快乐智力题(Cracking the Coding Interview) 问:有20瓶药,其中19瓶装的都是1.0克的药片,只有1瓶装了1.1克的药。给你一个能称出具体克数的电子秤,只允许你称一次,怎么找出那瓶不一样的? 答:如果药片足够,从第i(1<i<20)个瓶子里取出数量为i的药片称重为Xg,[X-(21*20/2)]/0.1即为装了1.1g药的瓶子的编号。 Q:A bunch of men are on an island. A genie ... 阅读详情

原文:

Given a (decimal - e.g. 3.72) number that is passed in as a string, print the binary representation.If the number can not be represented accurately in binary, print “ERROR”.

译文:

给定一个字符串类型(string)表示的小数,打印出它的二进制表示。 如果这个数无法精确地表示为二进制形式,输出"ERROR"。

整数部分通过不断地对2取余然后除以2来得到其二进制表示, 或是不断地和1按位与然后除以2得到其二进制表示。 小数部分则通过不断地乘以2然后与1比较来得到其二进制表示。 小数部分转化为二进制,通过乘以2然后与1比较,大于等于1则该位为1,并且该值减去1; 否则该位为0。不断地通过这种操作最终能使该小数部分的值变为0的,即可精确表示。 否则将无法用有限的位数来表示这个小数部分。我们可以设定一个长度,比如说32, 在32位之内还无法精确地表示这个小数部分的,我们就认为它无法精确表示了。

package chapter_5_BitManipulation;

import java.util.Scanner;

/**
 * 
 * 给定一个字符串类型(string)表示的小数,打印出它的二进制表示。 如果这个数无法精确地表示为二进制形式,输出"ERROR"。
 * 
 */
public class Question_5_2 {
	// 计算小数的二进制表示
	public static String change(String strs[]) {
		int len = 0;
		int intpart = Integer.parseInt(strs[0]);
		double decpart = Double.parseDouble("0." + strs[1]);
		String intString = "";
		StringBuilder decString = new StringBuilder();
		while(intpart > 0) {
			// intString.append(intpart % 2);
			intString = intpart % 2 + intString;
			intpart /= 2;
		}
		
		while(decpart != 0) {
			decpart *= 2;
			if(decpart >= 1) {
				decString.append(1);
				decpart -= 1;
			} else {
				decString.append(0);
			}
			len ++;
			if(len == 30) {
				return "ERROR";
			}
		}
		// 如果是0.0的情形
		if(intString.length() == 0) {
			intString = "0";
		}
		if(decString.length() == 0) {
			decString = decString.append("0");
		}
		return intString + "." + decString.toString();
	}
	
	public static void main(String args[]) {
		Scanner scanner = new Scanner(System.in);
		String string = scanner.nextLine();
		boolean isPositive = true;
		// 如果小数是负数,则截取第一位之后的数据
		if(string.charAt(0) == '-') {
			isPositive = false;
			string = string.substring(1);
		} 
		// 以小数点作为截取点分开
		String strs[] = string.split("\\.");
		
		String result = change(strs);
		
		if(isPositive) {
			System.out.println("二进制:" + result);
		} else {
			System.out.println("二进制:-" + result);
		}
	}
}


参考自:http://www.hawstein.com/posts/5.2.html

Rosetta基础2:preparing ligand Rosetta是基于残基,也就是说Rosetta不把原子单独对待,而是把它们看作一个完整的化学实体。为了实现这个目标,Rosetta必须知道键长、键角以及其它性质。对标准残基和小分子残基模型都是必须的。Rosetta在params文件中对残基的性质进行编码。Rosetta数据库包含常见氨基酸残基和小分子残基的parmas文件。当我们添加新的配体时,需要生成新的parms文件。 What is a Params File params文件包含残基的几何形状和化学特征的信息,为了让Rosetta理解怎样运行这个 阅读详情

相关推荐

告别配置烦恼!保姆级教程:在Windows 10/11上为QT5.14.2配置MSVC2017编译器(附VS2022组件避坑指南)

本文提供了一份详细的保姆级教程,指导如何在Windows 10/11上为QT5.14.2配置MSVC2017编译器,包括VS2022组件避坑指南。从环境准备到关键配置,再到调试工具验证和Qt Creator中的编译器设置,一步步解决常见问题,帮助开发者快速搭建稳定可靠的开发环境。

weixin_30615767的博客 501

Cracking the coding interview--Q2.5

Cracking the coding interview--Q2.5 December 17, 2012 作者:Hawstein 出处:http://hawstein.com/posts/2.5.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注明作者及出处。 题目

pi9nc的专栏 817

卸载Office 16 Click-to-Run Extensibility Component详细步骤

卸载Office 16 Click-to-Run Extensibility Component详细步骤 第一步:按住键盘上的“Win+R”,打开运行窗口 在运行窗口输入“Installer”回车,打开Installer文件夹 第二步:在Installer文件夹红框最右侧,即“大小”后面,右击选择“其他”,找到“主题”,勾选,点击“确定”,添加主题列成功 第三步:在“主题”列找到“Office 16 Click-to-Run Extensibility Component

M_try的博客 3万+

Cracking the Coding Interview Q2.5

You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1’s digit is at the head of the list. Write a function...

weixin_30568715的博客 101

AICodeing:AI驱动的全栈开发革命

作为阿里/字节跳动的资深Java工程师,我们正站在研发效能革命的临界点上。本文将深入探讨如何利用AI技术重构全栈开发流程,实现从需求到上线的量子级提速。

weixin_43290370的博客 1134

C2000 CLA协处理器:嵌入式实时控制性能优化与实战指南

在嵌入式实时控制系统中,主CPU常因高频率、低延迟的控制算法而面临性能瓶颈。为解决此问题,协处理器技术应运而生,它通过硬件并行架构分担计算密集型任务,从而提升系统整体性能。其核心原理在于独立的计算单元与总线设计,能够实现近乎零延迟的响应,这对于电机驱动、数字电源等闭环控制场景具有极高的技术价值。本文聚焦于德州仪器C2000系列微控制器中的控制律加速器(CLA),深入解析其作为独立浮点协处理器如何通过任务调度与消息RAM通信机制,有效实现性能倍增,并提供了从内存配置到代码调试的完整工程实践方案。

weixin_29192211的博客 302

Cracking the coding interview--Q4

Chapter 4 | Trees and Graphs 4.1 Implement a function to check if a tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that no two leaf nodes differ

763

BFD+OSPF+FRR:构建高可用网络的核心铁三角

摘要:VRRP、OSPF、BFD和FRR协同实现网络高可用。VRRP负责网关冗余,OSPF确保路由可达,BFD提供毫秒级故障检测,FRR实现快速路径切换。故障时,BFD立即检测并通知OSPF,FRR在OSPF收敛前切换流量,VRRP完成网关切换(如需)。BFD采用异步模式动态建立,效率最高。该机制确保业务流量无缝切换,提升网络可靠性。

A516988的博客 753

cracking-the-coding-interview的核心秘密:如何用单元测试“定义”面试题?C++ Catch+Python unittest的TDD完整教程

cracking-the-coding-interview 是一个为《Cracking the Coding Interview》(CTCI)第 6 版提供 **C++ 与 Python 双语言解法** 的开源项目,其最大特色是:**每一道面试题的解法都配有完整的自动化单元测试**(C++ 端用 Catch 框架,Python 端用 unittest 框架),自动化测试覆盖率超过 90%。更妙的是

gitblog_00501的博客 317

WiFi信道状态信息 基于Intel 5300的CSI Tool 安装及使用(一)

经过半个月的坎坷安装过程,连Windows系统都没装过的本人终于颤颤巍巍把CSI TOOL装好了,下面记录一下适于我所使用设备的安装过程。设备:一台THINKPAD X201,一台师兄借的华硕主板的台式机首先,如果你和我一样对linux其实一窍不通的话,建议先反复看教程里的安装代码,照猫画虎很难一步实现的,中途一定会有这或那的问题需要解决,所以该了解的基础linux指令还是要了解,起码安装过程中的那些指令可以去查一查。.........

Silver_777的博客 2万+

【人工智能】广度优先搜索(BFS)在人工智能中的应用(Python源码)

本文系统阐述了广度优先搜索(BFS)算法在人工智能领域的核心原理与应用。作为无信息搜索算法,BFS通过FIFO队列实现节点遍历,保证在无权图中的最优性。文章详细解析了BFS的伪代码实现和Python编程示例,并通过机器人路径规划案例展示了其实际应用场景。测试结果表明,BFS能有效解决包含障碍物的网格寻路问题,验证了算法在AI领域的实用价值。该研究为理解基础搜索算法及其工程应用提供了重要参考。

梦想起飞的地方 1142

Cracking the coding interview--Q9

Chapter 9 | Sorting and Searching 9.1 You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order. 插入排序,需要调整数

1020

三极管、MOS管和晶体管综合对比——《器件手册--三极管、MOS管和晶体管》

IGBT(Insulated Gate Bipolar Transistor,绝缘栅双极型晶体管)是一种复合全控型电压驱动式功率半导体器件,结合了MOSFET的高输入阻抗、开关速度快的优点和双极型晶体管(BJT)的低导通压降、高电流密度的优点。例如,在NPN型三极管中,当基极电流增加时,集电极电流会按照一定的比例(放大倍数β)增加,这个比例通常在几十到几百之间。在NMOS管中,当栅极相对于源极的电压(Vgs)小于开启电压(Vth)时,漏极和源极之间没有导电沟道,电流无法通过,处于截止状态。

XU157303764的博客 2878

Cracking the coding interview--Q17

Chapter 17 | Networking 17.1 Explain what happens, step by step, after you type a URL into a browser. Use as much detail as possible.  先进行DNS的namequery,得到对方的cname或者a记录(当然也可能是IPV6),反正就最后得到对方的IP

630

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

渣基础:比照Hawstein学Cracking the coding interview(1)

《C++ Primer 第五版》书实在是太长,太厚了。总是看了十几页就看累了,坚持不了多久,想了想还是别勉强自己,jued

低端玩家的记录地 949

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

Cracking the coding interview--Q3.5

题目 原文: Implement a MyQueue class which implements a queue using two stacks. 译文: 用两个栈实现一个队列MyQueue。 解答 方法

cfanr 1448

Cracking the coding interview--Q12.5

题目 原文: If you were designing a web crawler, how would you avoid getting into infinite loops? 译文: 如果你正在设计一个网络爬虫,何如避免进入无限循环? 解答

cfanr 1630

Cracking the coding interview--Q5

Chapter 5 | Bit Manipulation 5.1 You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e.g., M becomes a subst

951

Cracking the coding interview--Q3.6

原文: Write a program to sort a stack in ascending order. You should not make any assumptions about how the stack is implemented. The following are the only functions that should be used to write thi

执子之手 560

Cracking the coding interview--Q5.7

题目 原文: An array A[1... n] contains all the integers from 0 to n except for one number which is missing. In this problem, we cannot access an entire integer in A with a single operation. The elements

cfanr 1649

Cracking the coding interview--Q4.7

题目 原文: You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1. 译文: 有两个非常大的二叉树:T1,有上百万个结点,T2,有上百个

cfanr 1452

Cracking the coding interview--Q1.5

Cracking the coding interview--Q1.5 原文 Implement a method to perform basic string compression using the countsof repeated characters. For example, the string aabcccccaaa w...

weixin_34378045的博客 115

Cracking the coding interview--Q20.2

题目 原文:

cfanr 1525
上一篇: Cracking the coding interview--Q5.1
下一篇: Cracking the coding interview--Q4.1
java_wliang
博客等级 码龄15年 19粉丝 82原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值