题目链接: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 == '