Cracking The Coding Interview5.2
2014-04-18 01:48
363 查看
//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”. //前面有个打印整数的了,这里只打印小数. #include <iostream> #include <vector> #include <string> #include <stdlib.h> using namespace std; void print(int p) { vector<int> v; for (int k = 0;k<32; k++) { int t = p&1; v.push_back(t); p=p>>1; } for (int i = v.size()-1;i>-1; i--) { cout<<v[i]<<" "; } cout<<endl; } void mprintf(float f) { vector <int>v; for (int k = 0; k<32; k++) { f = f *2; if (f>=1) { f = f-1.0; v.push_back(1); } else v.push_back(0); cout<<v[k]<<" "; } } int main() { string str = "0.8"; float f = atof(str.c_str()); mprintf(f); return 0; }
相关文章推荐
- Cracking the Coding Interview 5.2
- cracking the coding interview No1.5
- Cracking the coding interview--Q1.2
- Cracking the coding interview--Q1.6
- Cracking the coding interview--Q18.1
- Cracking the coding interview--Q2.1
- Cracking The Coding Interview2.4
- Cracking the coding interview--Q2.3
- Cracking the coding interview--Q1.6
- Cracking The Coding Interview 4.7_暂存
- 技术面试圣经《Cracking the Coding Interview》题解C++版
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目1
- 《Cracking the Coding Interview》——第10章:可扩展性和存储空间限制——题目6
- 《Cracking the Coding Interview》——第14章:Java——题目3
- 《Cracking the Coding Interview》——第16章:线程与锁——题目4
- Cracking the Coding Interview Q3.1
- 《Cracking the Coding Interview》——第17章:普通题——题目10
- 《Cracking the Coding Interview》——第18章:难题——题目10
- Cracking the coding interview
- Cracking the Coding Interview 150题(二)