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 ...
随机推荐
- ubuntu 14.04 安装截图工具 Shutter及使用
一.安装截图工具 Shutter 1. 添加安装包软件源 sudo add-apt-repository ppa:shutter/ppa 1 2. 更新源并安装 shutter sudo apt-ge ...
- X230 安装win7 sp1
早上起床发现win10歇菜了,死活启动不了只好重装系统 用稳定版本win7比较靠谱. 去msdn上下载 一个win7系统 win7旗舰版本64位 ed2k://|file|cn_windows_7_u ...
- [流媒体]VLC主要模块
libvlccore vlcthread: vlc线程是libvlccore的重要组成部分,我们在src文件夹下面android.os2.posix.win32等文件夹下包含thread.c文件,说明 ...
- MapReduce 重要组件——Recordreader组件
(1)以怎样的方式从分片中读取一条记录,每读取一条记录都会调用RecordReader类: (2)系统默认的RecordReader是LineRecordReader,如TextInputFormat ...
- Google https服务被屏蔽
根据Google透明度报告显示,从上周(5月27日)开始,Google的部分服务开始被屏蔽,其中最主要的是HTTPS搜索服务和Google登录服务,所有版本的Google都受到影响,包括Google. ...
- 提交自己的插件包(package)
安装 把下列命令粘贴到终端上: mkdir -p ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins; curl -L htt ...
- 本周实验的PSP0过程文档
项目计划总结: 日期/任务 听课 编写程序 阅读相关书籍 日总计 周一 110 60 ...
- 一个很好介绍js的例子
function UpdateInit(opt){ this.init(opt);} UpdateInit.prototype={ loadUrl:null, loadParam:null, befo ...
- Java三大主流开源工作流引擎技术分析
首先,这个评论是我从网上,书中,搜索和整理出来的,也许有技术点上的错误点,也许理解没那么深入.但是我是秉着学习的态度加以评论,学习,希望对大家有用,进入正题! 三大主流工作流引擎:Shark,oswo ...
- 在windows上搭建ftp服务
在控制面板->程序和功能->打开或关闭Windows功能中开启ftp和IIS信息服务管理器 在控制面板->管理工具中打开Internet信息服务管理器->添加ftp站点 建好之 ...