careercup5.2
来源:互联网 发布:唐小僧 以数据和科技 编辑:程序博客网 时间:2024/06/15 10:01
判错搞麻烦了。。
/*Given a (decimal - e g 3 72) number that is passed in as a string, print the binary rep-resentationIf the number can not be represented accurately in binary, print “ERROR” */#include <iostream>#include <string>#include <bitset>using namespace std;string n;bool numtobit(string k){bool dec = true;string int_part;string float_part ="0.";for(int i =0; i<k.length(); i++){if((!dec && k[i] == '.') || ((k[i]>'9' || k[i]<'0')&& k[i]!='.'))return 0;if(k[i] == '.'){ dec = false; continue;}if(dec) int_part += k[i];else float_part +=k[i];}int a = atoi(int_part.c_str());float b = atof(float_part.c_str());int i = 0;while(a>0){ n = char(a%2+48) + n; a /= 2;}if(b>0){n = n + '.';while(b!= 0){ b *=2; if( b >=1){ n += '1'; b -= 1; }else n += '0';}}return 1;}int main(){string k = "113.8125";if(!numtobit(k))cout<<"ERROR!"<<endl;else cout<<n<<endl;}
- careercup5.2
- careercup5
- careercup5.1
- careercup5.3
- careercup5.5
- careercup5.6
- careercup5.7
- careercup5.1
- careercup5.5求解转换两数所需修改的位数
- 2
- 2
- 2
- 2
- 2
- 2
- 2
- 2
- 2
- 敏捷开发一千零一问系列之十九:提问帖
- iPhone开发网站、论坛、博客
- 效率提升之需求评审会议篇
- 静态成员函数总结
- 使用ubuntu命令行安装
- careercup5.2
- lvm的管理和维护
- cocos2d/box2d技术问题汇总
- VC2005使用GDI+库报错的解决方法
- LINUX驱动Makefile模板
- Transplanting OpenCV2.2.0 to ARM cortex-A9 Platform
- 不能发吗
- 10篇值得一读的计算机文献
- iOS: 去除gloss effect