zoukankan      html  css  js  c++  java
  • Codeforces 691C. Exponential notation

    题目链接: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 = 1000000;
     6 typedef long long LL;
     7 
     8 void Formatst(int &st, char str[]) { while(str[st] == '0' ) st++; } //格式化前导 “0”
     9 void Formated(int &ed, char str[]) { while(str[ed] == '0' ) ed--; } // 格式化小数后面的 “0”
    10 
    11 int main() {
    12     ios_base::sync_with_stdio(0); cin.tie(0);
    13     char str[MAXN + 3] = {0}; cin >> str;
    14     int len = strlen(str);
    15     int st = 0, ed = len - 1;
    16     int scale = -1;
    17     for(int i = st; i <= ed; i++) {
    18         if(str[i] == '.') {
    19             scale = i;
    20             break;
    21         }
    22     }
    23     if(scale == -1 || scale == len - 1 || scale == 0) { // 处理没有小数点或者 小数点在最后一位 或者小数点在第一位
    24         if(scale == len - 1) ed--;
    25         if(scale == 0) st++;
    26         Formatst(st, str);
    27         char zh = str[st];
    28         if(zh == '' || zh == '.') {
    29             cout << "0" << endl;
    30             return 0;
    31         }
    32         int sc = st + 1;
    33         int EE = ed - st; // 结果为 10EE  
    34         Formated(ed, str);
    35         if(scale == 0) 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 != 0) 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 << "0" << endl;
    49             return 0;
    50         }
    51         else if (str[st] == '.' && str[ed] != '.') { // 处理小数点前面全部都是 0, 后面存在数字的情况
    52             int EE = 0;
    53             int i;
    54             for(i = st + 1; i <= ed; i++) {
    55                 EE--;
    56                 if(str[i] == '0') 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 + 1; 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 0;
    74             }
    75             char zh = str[st];
    76             int EE = ed - st;
    77             while(str[ed] == '0') ed--;
    78             cout << zh;
    79             for(int i = st + 1; i <= ed; i++) cout << (i == st + 1 ? ".":"")<< str[i];
    80             cout << 'E' << EE << endl;
    81         }
    82         else { // 处理小数点前面和后面都有数字的情况
    83             char zh = str[st];
    84             int EE = scale - st - 1;
    85             cout << zh << '.';
    86             for(int i = st + 1; i <= ed; i++) if(str[i] != '.') cout << str[i];
    87             if(EE != 0)cout << 'E' << EE;
    88             cout << endl;
    89         }
    90     }
    91     return 0;
    92 }
  • 相关阅读:
    如何使用vs2008单元测试416a3648
    限制TextBox的长度
    DataGrid双击事件
    结构与类
    DataGrid删除对话框 & 限制编辑模试TextBox长度
    安逸...
    新来的,请多关照~!
    asp.net中使用JMail发邮件
    silverlight3datagrid中的数据导出到excel
    sl3中的DataGrid中的数据导出到Excel中 使用csv格式 解决中文是乱码的问题
  • 原文地址:https://www.cnblogs.com/Ash-ly/p/5837458.html
Copyright © 2011-2022 走看看