题目链接: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. 2017 Multi-University Training Contest - Team 2 Puzzle

    题目大意: 给定n, m, p.然后按照一个规则往n*m的方格里填数,最后一个方格是空格,然后玩拼图游戏,问能否复原 规则是:把1~n*m-1的排列中的第1,p+1,2*p+1.....个数依次取出来 ...

  2. 【题解】POI2014FAR-FarmCraft

    这题首先手玩一下一下数据,写出每个节点修建软件所需要的时间和到达它的时间戳(第一次到达它的时间),不难发现实际上就是要最小化这两者之和.然后就想到:一棵子树内,时间戳必然是连续的一段区间,而如果将访问 ...

  3. [AT2558]Many Moves

    题目大意:有$n$个位置$1,2,\dots n$:你有两个棋子$A$和$B$,你要进行$q$次操作,第$i$次操作给定一个$x_i$,你要选择一个棋子移动到$x_i$:求两个棋子最小移动的步数之和. ...

  4. 2018牛客多校第一场 A.Monotonic Matrix

    题意: 给一个n*m的矩阵赋值(0,1,2).使得每个数都不小于它左面和上面的数. 题解: 构建0和1的轮廓线.对于单独的轮廓线,共需要往上走n步,往右走m步.有C(n+m,n)种方式. 两个轮廓线的 ...

  5. Flink 任务打包、提交

    一.Flink版本 flink-1.6.1-bin-hadoop26-scala_2.11 二.Flink任务打包 笔者将写好的flink计算任务代码发到服务器(ubuntu16.04),在服务器端进 ...

  6. cdq分治入门学习 cogs 1752 Mokia nwerc 2015-2016 G 二维偏序

    /* CDQ分治的对象是时间. 即对于一个时间段[L, R],我们取mid = (L + R) / 2. 分治的每层只考虑mid之前的修改对mid之后的查询的贡献,然后递归到[L,mid],(mid, ...

  7. 分享一些JavaScript简易小技巧

    特性检测而非浏览器检测 因为某某特性某浏览器不支持,我们经常的做法是在代码中直接先做浏览器判断如: 1 if(Broswer.isFirfox){ 2     //do something 3 } 其 ...

  8. C# 序列化原因 (转)

         1.什么是序列化 序列化是将对象状态转换为可保持或传输的格式的过程,在序列化过程中,对象的公共字段和私有字段以及类的名称(包括包含该类的程序集)都被转换为字节流,然后写入数据流.与序列化相对 ...

  9. bzoj4430 [Nwerc2015]Guessing Camels赌骆驼

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4430 [题解] 把每只骆驼在第一个人.第二个人.第三个人的位置找出来,然后做三维偏序即可. ...

  10. python3 基础概念

    一.3.x新特性 1.print (),打印,3.x必须加括号 2.raw_input,3.x改为input   二.简介   Python是著名的“龟叔”Guido van Rossum在1989年 ...