题目链接:http://codeforces.com/problemset/problem/691/C

题意:
  给你一个浮点数,让你把这个数转化为 aEb 的形式,含义为 a * 10b, 其中 a 只能为一个不小于 1.0 且不大于等于10.0的小数, b 为一个不为0 的整数.具体样例参考原题的输入输出.

思路:

  直接模拟就好,感觉写的好复杂,分了许多情况,需要注意许多特殊情况,注意不输出小数点 (".")的情况还有多余的 “0” 的情况 .

代码:

 #include <bits/stdc++.h>

 using namespace std;

 const int MAXN = ;
typedef long long LL; void Formatst(int &st, char str[]) { while(str[st] == '' ) st++; } //格式化前导 “0”
void Formated(int &ed, char str[]) { while(str[ed] == '' ) ed--; } // 格式化小数后面的 “0” int main() {
ios_base::sync_with_stdio(); cin.tie();
char str[MAXN + ] = {}; cin >> str;
int len = strlen(str);
int st = , ed = len - ;
int scale = -;
for(int i = st; i <= ed; i++) {
if(str[i] == '.') {
scale = i;
break;
}
}
if(scale == - || scale == len - || scale == ) { // 处理没有小数点或者 小数点在最后一位 或者小数点在第一位
if(scale == len - ) ed--;
if(scale == ) st++;
Formatst(st, str);
char zh = str[st];
if(zh == '\0' || zh == '.') {
cout << "" << endl;
return ;
}
int sc = st + ;
int EE = ed - st; // 结果为 10EE  
Formated(ed, str);
if(scale == ) EE = -st;
cout << zh;
if(st != ed) {
cout << ".";
for(int i = sc; i <= ed; i++) cout << str[i];
}
if(EE != ) cout << "E" << EE;
cout << endl;
}
else {
Formatst(st, str);
Formated(ed, str);
if(str[st] == '.' && str[ed] == '.') { // 处理小数点两端都是 0 的情况
cout << "" << endl;
return ;
}
else if (str[st] == '.' && str[ed] != '.') { // 处理小数点前面全部都是 0, 后面存在数字的情况
int EE = ;
int i;
for(i = st + ; i <= ed; i++) {
EE--;
if(str[i] == '') continue;
else break;
}
char zh = str[i];//整数部分第一个数
if(i == ed) {
cout << zh << 'E' << EE << endl;
}
else {
cout << zh << '.';
for(int j = i + ; j <= ed; j++) cout << str[ed];
cout << 'E' << EE;
}
}
else if(str[st] != '.' && str[ed] == '.'){ // 处理小数点前面有数字, 后面都是 0 的情况
--ed;
if(ed == st) {
cout << str[st] << endl;
return ;
}
char zh = str[st];
int EE = ed - st;
while(str[ed] == '') ed--;
cout << zh;
for(int i = st + ; i <= ed; i++) cout << (i == st + ? ".":"")<< str[i];
cout << 'E' << EE << endl;
}
else { // 处理小数点前面和后面都有数字的情况
char zh = str[st];
int EE = scale - st - ;
cout << zh << '.';
for(int i = st + ; i <= ed; i++) if(str[i] != '.') cout << str[i];
if(EE != )cout << 'E' << EE;
cout << endl;
}
}
return ;
}

Codeforces 691C. Exponential notation的更多相关文章

  1. Codeforces 691C. Exponential notation 模拟题

    C. Exponential notation time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...

  2. 【模拟】Codeforces 691C Exponential notation

    题目链接: http://codeforces.com/problemset/problem/691/C 题目大意: 输入一个数,把它表示成a·10b形式(aEb).输出aEb,1<=a< ...

  3. CF-697B Barnicle与691C Exponential notation

    无聊写两个题解吧,上午做比赛拉的,感触很多! B. Barnicle time limit per test 1 second memory limit per test 256 megabytes ...

  4. codeforces 691C C. Exponential notation(科学计数法)

    题目链接: C. Exponential notation time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  5. Educational Codeforces Round 14 C. Exponential notation 数字转科学计数法

    C. Exponential notation 题目连接: http://www.codeforces.com/contest/691/problem/C Description You are gi ...

  6. Exponential notation

    Exponential notation You are given a positive decimal number x. Your task is to convert it to the &q ...

  7. Educational Codeforces Round 14

    A - Fashion in Berland 水 // #pragma comment(linker, "/STACK:102c000000,102c000000") #inclu ...

  8. D3中动画(transition函数)的使用

    关于transition的几个基本点: 1. transition()是针对与每个DOM element的,每个DOM element的transition并不会影响其他DOM element的tra ...

  9. jQuery静态方法isFunction,isArray,isWindow,isNumeric使用和源码分析

    上一篇随笔中总结了js数据类型检测的几个方法和jQuery的工具方法type方法,本篇要分析几个方法都依赖type方法,所以不了解type方法的请先参看http://www.cnblogs.com/y ...

随机推荐

  1. THUSC2014酱油记

    Day0: 坐飞机到北京,然后报到...跟jason_yu分到一个房间,刚好可以蹭点RP.发现房间460RMB/晚,但再带一份早餐就500RMB,难道早餐是40RMB么...在一家川菜馆吃的午晚餐,感 ...

  2. iOS 屏幕适配,autoResizing autoLayout和sizeClass图文详解

    === 1. autoResizing autoresizing是苹果早期的ui布局适配的解决办法,iOS6之前完全可以胜任了,因为苹果手机只有3.5寸的屏幕,在加上手机app很少支持横屏,所以iOS ...

  3. JQuery选择器$()的工作原理浅析

    每次申明一个jQuery对象的时候,返回的是jQuery.prototype.init对象,很多人就会不明白,init明明是jQuery.fn的方法啊,实际上这里不是方法,而是init的构造函数,因为 ...

  4. js 日期获去及格式化

    原文地址:http://www.cnblogs.com/qinpengming/archive/2012/12/03/2800002.html Js获取当前日期时间及格式化操作 var myDate ...

  5. 线程 ManualResetEvent 类

    Reset(): 当一个线程开始一个活动(此活动必须完成后,其他线程才能开始)时, 它调用 Reset 以将 ManualResetEvent 置于非终止状态.此线程可被视为控制 ManualRese ...

  6. vue文件使用stylus报错问题

    先npm install stylus --save然后安装你少的page.json中依赖:npm install stylus-loader css-loader style-loader --sa ...

  7. js点亮星星评分并获取参数的js代码

    点亮星星评分后,点击按钮,立即获得分数参数值,方便不想使用ajax传参的朋友 http://demo.jb51.net/js/2014/jsxxdf/demo2.html <!DOCTYPE h ...

  8. jquery中lhgdialog插件(一)

    一:前言 最近在使用jquery的控件,其实以前也写但是突然之间遇到了需要从弹出窗口传值到父窗口,突然觉得这种传值的方式其实也是需要javascript的基础的,但是我自己还没有去真正的做过,所以还是 ...

  9. java JDK动态代理的机制

    一:前言 自己在稳固spring的一些特性的时候在网上看到了遮掩的一句话“利用接口的方式,spring aop将默认通过JDK的动态代理来实现代理类,不适用接口时spring aop将使用通过cgli ...

  10. Topcoder SRM 608 div1 题解

    Easy(300pts): 题目大意:有n个盒子,一共有S个苹果,每个盒子有多少个苹果不知道,但是知道每个盒子的苹果下限和上限.现在要至少选择X个苹果,问如果要保证无论如何都能获得至少X个苹果,至少需 ...