题目链接: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. 容器(vector)、数组、new创建的动态数组,你到底用哪一个(执行效率分析)

    1.问题的提出 在没有了解vector之前,动态数组一般都是又new创建的.在了解vector后发现vector竟是那样方便好用,但方便的同时却是以牺牲执行效率为代价的.网上对vector和array ...

  2. URAL1696 Salary for Robots

    题目戳这里. 最长下降子序列单调队列求法. \(f_{i,j,k}\)表示考虑前\(i\)个数,\(g_1 = j,g_2 = k\)的方案数.转移: \[f_{i,j,k} = \sum_{p = ...

  3. 【ZJ选讲·BZOJ 5071】

    小A的数字 有一串数字A1 ,A2,--,An,每次可以进行如下骚操作: 选择一个数字i,将(Ai-1,Ai,Ai+1)变为(Ai-1+Ai,-Ai,Ai+1+Ai), (特别地,若i=N,则( ...

  4. poj3133 Manhattan Wiring

    Manhattan Wiring Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2016   Accepted: 1162 ...

  5. js实现页面触摸滑动

    先设置一个div  高度不能设置100% . window.addEventListener("load",function(){ var addEventListener = ' ...

  6. Oracle查询字段内容为非数字的记录

    今天在一张3W多记录的表里查非数字的异常数据~数据库太水,记录一发,因为2.5使用人员误输入为2..5.... select t.routecardlist_id,trim(translate(RTR ...

  7. Install the AWS Command Line Interface on Linux

    Install the AWS Command Line Interface on Linux You can install the AWS Command Line Interface and i ...

  8. dbcp基本配置和重连配置

    转载自:http://agapple.iteye.com/blog/772507 最近在看一些dbcp的相关内容,顺便做一下记录,免得自己给忘记了. 1. 引入dbcp (选择1.4) Java代码  ...

  9. babel-preset-es2015,babel-polyfill 与 babel-plugin-transform-runtime

    babel-preset-es2015 是一个babel的插件,用于将部分ES6 语法转换为ES5 语法.转换的语法包括: 箭头函数 var a1 = () => 1 编译为 var a1 = ...

  10. JS模块化工具requirejs教程02

    基本API require会定义三个变量:define,require,requirejs,其中require === requirejs,一般使用require更简短 define 从名字就可以看出 ...