原题链接

题目大意:给一个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. Android 解压html压缩数据

    public static String unzipHTML(String s){ int endPos = s.indexOf("\r\n\r\n"); if(endPos< ...

  2. 12.Object-C--浅谈OC的内存管理机制

    昨天学习了OC的内存管理机制,今天想总结一下,所以接下来我要在这里bibi一下:OC的内存管理. 首先我要说的是,内存管理的作用范围. 内存管理的作用范围: 任何继承了NSObject的对象,对其他基 ...

  3. BindingNavigator操作DatagridView的数据

    参考 http://wenku.baidu.com/link?url=NWfEfArPZvDO_aI-xEKBHVGoZY9wQO_Oty_GCsGLiPspheCzFYLf_dytuWAqN2_0A ...

  4. web api control注册及重写DefaultHttpControllerSelector、ApiControllerActionSelector、ApiControllerActionInvoker

    namespace EWorkpal.WebApi { public class HttpNotFoundDefaultHttpControllerSelector : DefaultHttpCont ...

  5. $lookup

    db.orders.aggregate([ { $lookup: { from: "inventory", localField: "item", foreig ...

  6. 【个人使用.Net类库】(1)INI配置文件操作类

    开发接口程序时,对于接口程序配置的IP地址.端口等都需要是可配置的,而在Win Api原生实现了INI文件的读写操作,因此只需要调用Win Api中的方法即可操作INI配置文件,关键代码就是如何调用W ...

  7. iOS9/iOS8界面对比 XCode7

    Xcde7 bate 无需开发这账号(99¥)可以调试程序 目前是测试版 iOS9/iOS8界面对比 (注:左边为iOS8界面,右边为iOS9界面.) 1.新字体 苹果在 iOS9 中使用旧金山字体取 ...

  8. C++全局变量在多个源代码文件中的使用

    在比较大的项目中,如果需要使用全局变量,那么就需要注意一些全局变量声明.使用不当引起的问题了. 本篇文章主要内容有两个:普通全局变量.静态全局变量.全局常量. 1.普通全局变量:假设我们需要在多个不同 ...

  9. c++形参和实参同名时,如何单步执行观察形参的变化。

    c++形参和实参同名时,如何单步执行观察形参的变化? 方法:当程序运行到函数中时,添加变量观察即可.

  10. [Python陷阱]os.system调用shell脚本获取返回值

    当前有shell个脚本/tmp/test.sh,内容如下: #!/bin/bashexit 11 使用Python的os.system调用,获取返回值是: >>> ret=os.sy ...