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

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

思路:

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

代码:

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int MAXN = ;
  6. typedef long long LL;
  7.  
  8. void Formatst(int &st, char str[]) { while(str[st] == '' ) st++; } //格式化前导 “0”
  9. void Formated(int &ed, char str[]) { while(str[ed] == '' ) ed--; } // 格式化小数后面的 “0”
  10.  
  11. int main() {
  12. ios_base::sync_with_stdio(); cin.tie();
  13. char str[MAXN + ] = {}; cin >> str;
  14. int len = strlen(str);
  15. int st = , ed = len - ;
  16. int scale = -;
  17. for(int i = st; i <= ed; i++) {
  18. if(str[i] == '.') {
  19. scale = i;
  20. break;
  21. }
  22. }
  23. if(scale == - || scale == len - || scale == ) { // 处理没有小数点或者 小数点在最后一位 或者小数点在第一位
  24. if(scale == len - ) ed--;
  25. if(scale == ) st++;
  26. Formatst(st, str);
  27. char zh = str[st];
  28. if(zh == '\0' || zh == '.') {
  29. cout << "" << endl;
  30. return ;
  31. }
  32. int sc = st + ;
  33. int EE = ed - st; // 结果为 10EE  
  34. Formated(ed, str);
  35. if(scale == ) EE = -st;
  36. cout << zh;
  37. if(st != ed) {
  38. cout << ".";
  39. for(int i = sc; i <= ed; i++) cout << str[i];
  40. }
  41. if(EE != ) cout << "E" << EE;
  42. cout << endl;
  43. }
  44. else {
  45. Formatst(st, str);
  46. Formated(ed, str);
  47. if(str[st] == '.' && str[ed] == '.') { // 处理小数点两端都是 0 的情况
  48. cout << "" << endl;
  49. return ;
  50. }
  51. else if (str[st] == '.' && str[ed] != '.') { // 处理小数点前面全部都是 0, 后面存在数字的情况
  52. int EE = ;
  53. int i;
  54. for(i = st + ; i <= ed; i++) {
  55. EE--;
  56. if(str[i] == '') continue;
  57. else break;
  58. }
  59. char zh = str[i];//整数部分第一个数
  60. if(i == ed) {
  61. cout << zh << 'E' << EE << endl;
  62. }
  63. else {
  64. cout << zh << '.';
  65. for(int j = i + ; j <= ed; j++) cout << str[ed];
  66. cout << 'E' << EE;
  67. }
  68. }
  69. else if(str[st] != '.' && str[ed] == '.'){ // 处理小数点前面有数字, 后面都是 0 的情况
  70. --ed;
  71. if(ed == st) {
  72. cout << str[st] << endl;
  73. return ;
  74. }
  75. char zh = str[st];
  76. int EE = ed - st;
  77. while(str[ed] == '') ed--;
  78. cout << zh;
  79. for(int i = st + ; i <= ed; i++) cout << (i == st + ? ".":"")<< str[i];
  80. cout << 'E' << EE << endl;
  81. }
  82. else { // 处理小数点前面和后面都有数字的情况
  83. char zh = str[st];
  84. int EE = scale - st - ;
  85. cout << zh << '.';
  86. for(int i = st + ; i <= ed; i++) if(str[i] != '.') cout << str[i];
  87. if(EE != )cout << 'E' << EE;
  88. cout << endl;
  89. }
  90. }
  91. return ;
  92. }

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. BZOJ4567 SCOI2016背单词(trie+贪心)

    倒过来变成查询前缀.考虑怎么排序.第一条代价n*n就相当于inf,说明一个单词的所有前缀都要排在它前面.那么串的依赖关系就是trie的结构.二三条说明代价是Σidi-idfa,那么显然最后的编号应该是 ...

  2. hdu 3486 Interviewe (RMQ+二分)

    Interviewe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  3. 纯css实现 switch开关

    <!-- 直接看代码,利用了css3兄弟选择器 --><!-- html --> <button class="switch"> <inp ...

  4. Exponial~(欧拉函数)~(发呆题)

    Description Everybody loves big numbers (if you do not, you might want to stop reading at this point ...

  5. wget下载HTTPS链接

    wget -c -O master.zip --no-check-certificate https://github.com/mitsuhiko/flask/archive/master.zip # ...

  6. MUI -- plus初始化原理及plus is not defined,mui is not defined 错误汇总

    不要在没有plus和mui的环境下调用相关API 普通浏览器里没有plus环境,只有HBuilder真机运行和打包后才能运行plus api. 在普通浏览器里运行时plus api时控制台必然会输出p ...

  7. jsp和servlet之间传数据

    一.jsp与java文件传递数据可以使用Servlet类来传递,jsp将数据存入到request对象中,Servlet类获取这个request对象,并将数据取出. 示例代码如下: JSP代码: < ...

  8. 关于C++随机函数

    #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main ...

  9. 【CF24D】Broken Robot (DP+高斯消元)

    题目链接 题意:给定一个\(n\times m\)的矩阵,每次可以向→↓←移动一格,也可以原地不动,求从\((x,y)\)到最后一行的期望步数. 此题标签\(DP\) 看到上面这个肯定会想到 方法一: ...

  10. BZOJ1037 DP

    2013-11-15 21:51 原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1037 拿到这道题想到了DP,后来发现三维无法确定的表示状 ...