CodeForces 691C Exponential notation 模拟
题意: 给定一个长度为N的数字,转化为 标准的科学计数法形式。N最大可达1e6。要考虑前置0 和 后置 0 的特殊情况。 当指数为0 的时候,不输出指数部分。
纯粹的模拟。分好情况就好了。我分了一下几种情况。输入为x。
- x=0;
- x∈(0,1),这种情况下指数部分为-1;
- x 为整数,这种情况主要是处理不存在小数点的情况,其他情况输入都肯定包含小数点的;
- x∈(1, 10),这种情况不用输出指数部分。
- x > 10。
测试输入数据:
.100
.001
1
0
.
0.
120.
200
.00123100132132
100123100.0132
.001
1
0
.
0.
120.
200
.00123100132132
100123100.0132
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
#define fst first
#define snd second
typedef __int64 LL;
//typedef long long LL;
typedef unsigned int uint;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int MAXN = 1e6 + 5;
const int MAXM = 600 + 5;
int N;
char s[MAXN];
int cnt;
int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif // ONLINE_JUDGE
while (~scanf("%s", s)) {
int len = strlen(s);
int dot = len, L = -1, R = -1;
for (int i = 0; i < len; i ++) {
if (s[i] == '.') dot = i;
}
for (int i = 0; i < dot; i ++) {
if (s[i] != '0') {
L = i;
break;
}
}
for (int i = len - 1; i > dot; i --) {
if (s[i] != '0') {
R = i;
break;
}
}
// 0.000
if (L == -1 && R == -1) {
puts("0");
continue;
}
// (0, 1) 0.011111
if (L == -1) {
cnt = 1;
int tmp = 0;
bool flag = false;
for (int i = dot + 1; i <= R; i ++) {
if (!flag && s[i] == '0') {
cnt ++;
} else {
flag = true;
if (tmp == 1) putchar('.');
putchar(s[i]);
tmp ++;
}
}
printf("E-%d\n", cnt);
continue;
}
// 为整数 16 16. 16.0000 0016
if (dot == -1 || R == -1) {
R = ~dot ? dot - 1 : len - 1;
cnt = R - L;
putchar(s[L]);
// 100.0
while (s[R] == '0') {
R --;
}
for (int i = L + 1; i <= R; i ++) {
if (i == L + 1) putchar('.');
putchar(s[i]);
}
if (cnt >= 1) {
printf("E%d", cnt);
}
puts("");
continue;
}
// (1, 10) L != -1, R != -1, dot != -1 01.23400
if (L + 1 == dot) {
for (int i = L; i <= R; i ++) {
putchar(s[i]);
}
puts("");
continue;
}
// >10 含小数 10.55
if (dot - L >= 2) {
cnt = dot - L - 1;
putchar(s[L]);
putchar('.');
for (int i = L + 1; i <= R; i ++) {
if (s[i] != '.') putchar(s[i]);
}
printf("E%d\n", cnt);
continue;
}
while(1);
}
return 0;
}