原题链接

题目大意:给一个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的更多相关文章

  1. 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 ...

  2. zoj 2001 Adding Reversed Numbers

    Adding Reversed Numbers Time Limit: 2 Seconds      Memory Limit: 65536 KB The Antique Comedians of M ...

  3. ZOJ 2405 Specialized Four-Digit Numbers

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1405 要求找出4位数所有10进制.12进制.16进制他们各位数字之和相等. # ...

  4. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  5. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  6. ZOJ 1860:Dog & Gopher

    Dog & Gopher Time Limit: 2 Seconds      Memory Limit: 65536 KB A large field has a dog and a gop ...

  7. Floating Point Math

    Floating Point Math Your language isn't broken, it's doing floating point math. Computers can only n ...

  8. Python基础(二)

    本章内容: Python 运算符(算术运算.比较运算.赋值运算.逻辑运算.成员运算) 基本数据类型(数字.布尔值.字符串.列表.元组.字典.set集合) for 循环 enumrate range和x ...

  9. TileJSON

    TileJSON TileJSON is an open standard for representing map metadata. License The text of this specif ...

随机推荐

  1. UVa 11427 - Expect the Expected

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  2. SQL 插入日期时间 变量值

    --看看吧^^ CREATE TABLE #temp ( test datetime ) go --SQL: INSERT #temp SELECT 1.1 UNION ALL GO --SQL: I ...

  3. hbase基本概念和hbase shell常用命令用法

    1. 简介 HBase是一个分布式的.面向列的开源数据库,源于google的一篇论文<bigtable:一个结构化数据的分布式存储系统>.HBase是Google Bigtable的开源实 ...

  4. python建立pip.ini

    pip 是python的包管理器工具,类似linux的apt-get.yum包管理器,主要是用来进行安装python库, pip默认从官方源pypi.python.org下载数据,国内速度相对比较慢, ...

  5. JS 代理模式

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. js对象的定义及处理

    一,概述 在Java语言中,我们可以定义自己的类,并根据这些类创建对象来使用,在Javascript中,我们也可以定义自己的类,例如定义User类.Hashtable类等等. 目前在Javascrip ...

  7. Notification通知栏

    Notification通知栏 首先实现的功能就是通知栏显示Notification,Notification是显示在系统的通知栏上面的,所以Notification 是属于进程之前的通讯.进程之间的 ...

  8. ant新建scp和sshexec任务

    1.build.xml中新建targer如下: <target name="remotecopytest" description="拷贝文件到远程服务器" ...

  9. POJ 2385 DP

    题意:在苹果树下,初始在第一棵树下,告诉你在第几秒的时候,那棵树下会落下苹果,告诉最多能移动的次数,然后来回移动,求能得到的最大的苹果数目. 思路:三维DP,d[第i秒][已经移动j次][当前在(1, ...

  10. Include and Require

    The include or require statement takes all the text/codde/markup that exists in the specified file a ...