ZOJ 1125 Floating Point Numbers
题目大意:给一个16位的数字,表示一个浮点数,按照规则转换成科学计数法表示。
解法:注释比较清楚了,注意浮点运算的四舍五入问题。
参考代码:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<iomanip>
#include<string.h>
using namespace std; int main(){
char in[16],out[16];
int i,j,n,exp,man,zero;
double num; cout<<"Program 6 by team X"<<endl; //废话不能少,否则WA
while(scanf("%s",in)!=EOF){
man=0;
exp=0;
zero=1;
if(in[0]=='1')out[0]='-';
else out[0]=' ';
for(i=1;i<8;i++){
exp=exp<<1; //注意,移位的时候后面不能再跟算式,否则出错
exp+=(in[i]-'0'); //处理的是字符,要用ASCii码比较
if(in[i]!='0')zero=0;
}
exp=exp-63;
for(i=8;i<16;i++){
man=man<<1;
man+=(in[i]-'0');
if(in[i]!='0')zero=0;
}
num=(1+double(man)/256)*pow(2.0,exp);
exp=0;
if(num>10-1e-6){
while(num>10-1e-6){
num/=10;
exp++;
}
}
else if(num<1.0+1e-6){
while(num<1.0+1e-6){
num*=10;
exp--;
}
}
if(zero)cout<<" 0.000000e+000";
else{
cout<<out[0];
printf("%.6fe",num);
if(exp>=0){
cout<<"+0";
if(exp<10)cout<<'0';
cout<<exp;
}
else{
cout<<"-0";
if(exp>-10)cout<<'0';
cout<<-exp;
}
}
cout<<endl;
}
cout<<"End of program 6 by team X";
return 0;
}
ZOJ 1125 Floating Point Numbers的更多相关文章
- comparison of floating point numbers with equality operator. possible loss of precision while rounding values
double值由外部传入 private void Compare(double value) { string text; ) //小数位后保留2位 { //小数点后保留2位小数 text = st ...
- zoj 2001 Adding Reversed Numbers
Adding Reversed Numbers Time Limit: 2 Seconds Memory Limit: 65536 KB The Antique Comedians of M ...
- ZOJ 2405 Specialized Four-Digit Numbers
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1405 要求找出4位数所有10进制.12进制.16进制他们各位数字之和相等. # ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- ZOJ 1860:Dog & Gopher
Dog & Gopher Time Limit: 2 Seconds Memory Limit: 65536 KB A large field has a dog and a gop ...
- Floating Point Math
Floating Point Math Your language isn't broken, it's doing floating point math. Computers can only n ...
- Python基础(二)
本章内容: Python 运算符(算术运算.比较运算.赋值运算.逻辑运算.成员运算) 基本数据类型(数字.布尔值.字符串.列表.元组.字典.set集合) for 循环 enumrate range和x ...
- TileJSON
TileJSON TileJSON is an open standard for representing map metadata. License The text of this specif ...
随机推荐
- 如何整治那些敢偷用你Wi-Fi的人
我的邻居正在盗用我的WiFi,唔,对此我可以直接选择加密口令,或者…作为一名极客我也可以耍耍他.那么,我就从划分网络开始吧.我把网络划分成两部分,受信任部分和非受信任部分.受信任部分组成一个子网,而非 ...
- Validform自定义提示效果-使用自定义弹出框
$(function(){ $.Tipmsg.r=null; $("#add").Validform({ tiptype:function(msg){ layer.msg(msg) ...
- html中offsetTop、clientTop、scrollTop、offsetTop
HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...
- 从协议VersionedProtocol开始3——ClientProtocol、DatanodeProtocol、NamenodeProtocol、RefreshAuthorizationPolicyProtocol、RefreshUserMappingsProtocol
1.ClientProtocol这个玩意的版本号是61L:DatanodeProtocol 是26L:NamenodeProtocol是 3L;RefreshAuthorizationPolicyPr ...
- C++本质:类的赋值运算符=的重载,以及深拷贝和浅拷贝
关键词:构造函数,浅拷贝,深拷贝,堆栈(stack),堆heap,赋值运算符摘要: 在面向对象程序设计中,对象间的相互拷贝和赋值是经常进行的操作. 如果对象在申明的同时马上进行的初始化操作 ...
- Android 自定义属性
values新建一个attrs.xml<resource> <declare-styleable name = "MyTextView"> &l ...
- HDFS的可靠性
HDFS的可靠性 1.冗余副本策略 2.机架策略 3.心跳机制 4.安全模式 5.校验和 6.回收站 7.元数据保护 8.快照机制 1.冗余副本策 ...
- SharePoint 2013 Nintex Workflow 工作流帮助(十一)
博客地址 http://blog.csdn.net/foxdave 工作流动作 27. Create item in another site(Libraries and lists分组) 该操作用于 ...
- 介绍NSURLSession网络请求套件
昨天翻译了一篇<NSURLSession的使用>的文章,地址:http://www.cnblogs.com/JackieHoo/p/4995733.html,原文是来自苹果官方介绍NSUR ...
- C++学习之类的构造函数、析构函数
在C++的类中,都会有一个或多个构造函数.一个析构函数.一个赋值运算操作符.即使我们自己定义的类中,没有显示定义它们,编译器也会声明一个默认构造函数.一个析构函数和一个赋值运算操作符.例如: //声明 ...