题目链接: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. hdu 1207 汉诺塔II (DP+递推)

    汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  2. 【转】C#获取当前路径7种方法

    webformvar s = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; //C盘 IIS路径 var s1 ...

  3. axios超时重发

    axios的超时是在response中处理的,所以要在response中添加拦截器: axios.interceptors.response.use(undefined, function axios ...

  4. js金额转大写数字

    //金额转大写数字 const intToChinese = money => { //汉字的数字 let cnNums = new Array('零', '壹', '贰', '叁', '肆', ...

  5. 深入研究JavaScript的事件机制

    本篇开始将回顾下Javascript的事件机制.同时会从一个最小的函数开始写到最后一个具有完整功能的,强大的事件模块.为叙述方便将响应函数/回调函数/事件Listener/事件handler都称为事件 ...

  6. [fzu 2271]不改变任意两点最短路至多删的边数

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2271 题目中说每条边的边权都是[1,10]之间的整数,这个条件非常关键!以后一定要好好读题啊…… 做10次循环 ...

  7. MFC 对话框透明效果

    网上找的资料自己改了改,在这里记录和分享一下,主要是TransparentWnd函数. 在子类的OnShowWindow函数中调用 ShowWindowAlpha() #pragma once tem ...

  8. 自己实现的JDBC工具类

    最近做了个后台应用程序,刚开始用Spring+iBatis来做的,后来因为种种原因,不让用Spring.iBatis以及一些开源的工具包.   于是用JDBC重写了原来的Service实现,项目做完了 ...

  9. spring结合Mybatis的框架搭建(一)

    一:前沿 2015年新年上班的第二天,第一天就打了一天的酱油哦,只是下午开始搭建自己毕业设计的框架,搭建的是spring+spring mvc+MyBatis的框架.今天遇到了一个问题,结果弄了我一天 ...

  10. oracle中分页的知识

    一:前言 自从出来实习后,基本上都没有按下心来总结下自己学的知识点,刚刚好现在快要国庆了,没有到深圳出差,在公司呆了三天,可以说是在公司打了三天的酱油啊,所以前两天都是在看些正则的文档,并且写了下总结 ...