CodeForces 691C Exponential notation 模拟

CodeForces 691C  Exponential notation 模拟

题意: 给定一个长度为N的数字,转化为 标准的科学计数法形式。N最大可达1e6。要考虑前置0 和 后置 0 的特殊情况。 当指数为0 的时候,不输出指数部分。
纯粹的模拟。分好情况就好了。我分了一下几种情况。输入为x。
  1. x=0;
  2. x∈(0,1),这种情况下指数部分为-1;
  3. x 为整数,这种情况主要是处理不存在小数点的情况,其他情况输入都肯定包含小数点的;
  4. x∈(1, 10),这种情况不用输出指数部分。
  5. x > 10。
Tips:while(1) 的使用是为了避免情况没有考虑完全出现的WA.如果测评结果出现了TLE,那就证明是自己情况没有考虑完全了。
测试输入数据:
.100
.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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值