Codeforces 691C. Exponential notation
题目链接:http://codeforces.com/problemset/problem/691/C
题意:
给你一个浮点数,让你把这个数转化为 aEb 的形式,含义为 a * 10b, 其中 a 只能为一个不小于 1.0 且不大于等于10.0的小数, b 为一个不为0 的整数.具体样例参考原题的输入输出.
思路:
直接模拟就好,感觉写的好复杂,分了许多情况,需要注意许多特殊情况,注意不输出小数点 (".")的情况还有多余的 “0” 的情况 .
代码:
#include <bits/stdc++.h> using namespace std; const int MAXN = ;
typedef long long LL; void Formatst(int &st, char str[]) { while(str[st] == '' ) st++; } //格式化前导 “0”
void Formated(int &ed, char str[]) { while(str[ed] == '' ) ed--; } // 格式化小数后面的 “0” int main() {
ios_base::sync_with_stdio(); cin.tie();
char str[MAXN + ] = {}; cin >> str;
int len = strlen(str);
int st = , ed = len - ;
int scale = -;
for(int i = st; i <= ed; i++) {
if(str[i] == '.') {
scale = i;
break;
}
}
if(scale == - || scale == len - || scale == ) { // 处理没有小数点或者 小数点在最后一位 或者小数点在第一位
if(scale == len - ) ed--;
if(scale == ) st++;
Formatst(st, str);
char zh = str[st];
if(zh == '\0' || zh == '.') {
cout << "" << endl;
return ;
}
int sc = st + ;
int EE = ed - st; // 结果为 10EE
Formated(ed, str);
if(scale == ) EE = -st;
cout << zh;
if(st != ed) {
cout << ".";
for(int i = sc; i <= ed; i++) cout << str[i];
}
if(EE != ) cout << "E" << EE;
cout << endl;
}
else {
Formatst(st, str);
Formated(ed, str);
if(str[st] == '.' && str[ed] == '.') { // 处理小数点两端都是 0 的情况
cout << "" << endl;
return ;
}
else if (str[st] == '.' && str[ed] != '.') { // 处理小数点前面全部都是 0, 后面存在数字的情况
int EE = ;
int i;
for(i = st + ; i <= ed; i++) {
EE--;
if(str[i] == '') continue;
else break;
}
char zh = str[i];//整数部分第一个数
if(i == ed) {
cout << zh << 'E' << EE << endl;
}
else {
cout << zh << '.';
for(int j = i + ; j <= ed; j++) cout << str[ed];
cout << 'E' << EE;
}
}
else if(str[st] != '.' && str[ed] == '.'){ // 处理小数点前面有数字, 后面都是 0 的情况
--ed;
if(ed == st) {
cout << str[st] << endl;
return ;
}
char zh = str[st];
int EE = ed - st;
while(str[ed] == '') ed--;
cout << zh;
for(int i = st + ; i <= ed; i++) cout << (i == st + ? ".":"")<< str[i];
cout << 'E' << EE << endl;
}
else { // 处理小数点前面和后面都有数字的情况
char zh = str[st];
int EE = scale - st - ;
cout << zh << '.';
for(int i = st + ; i <= ed; i++) if(str[i] != '.') cout << str[i];
if(EE != )cout << 'E' << EE;
cout << endl;
}
}
return ;
}
Codeforces 691C. Exponential notation的更多相关文章
- Codeforces 691C. Exponential notation 模拟题
C. Exponential notation time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...
- 【模拟】Codeforces 691C Exponential notation
题目链接: http://codeforces.com/problemset/problem/691/C 题目大意: 输入一个数,把它表示成a·10b形式(aEb).输出aEb,1<=a< ...
- CF-697B Barnicle与691C Exponential notation
无聊写两个题解吧,上午做比赛拉的,感触很多! B. Barnicle time limit per test 1 second memory limit per test 256 megabytes ...
- codeforces 691C C. Exponential notation(科学计数法)
题目链接: C. Exponential notation time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- Educational Codeforces Round 14 C. Exponential notation 数字转科学计数法
C. Exponential notation 题目连接: http://www.codeforces.com/contest/691/problem/C Description You are gi ...
- Exponential notation
Exponential notation You are given a positive decimal number x. Your task is to convert it to the &q ...
- Educational Codeforces Round 14
A - Fashion in Berland 水 // #pragma comment(linker, "/STACK:102c000000,102c000000") #inclu ...
- D3中动画(transition函数)的使用
关于transition的几个基本点: 1. transition()是针对与每个DOM element的,每个DOM element的transition并不会影响其他DOM element的tra ...
- jQuery静态方法isFunction,isArray,isWindow,isNumeric使用和源码分析
上一篇随笔中总结了js数据类型检测的几个方法和jQuery的工具方法type方法,本篇要分析几个方法都依赖type方法,所以不了解type方法的请先参看http://www.cnblogs.com/y ...
随机推荐
- 【bzoj2007】[Noi2010]海拔 最小割+对偶图+最短路
题目描述 YT市是一个规划良好的城市,城市被东西向和南北向的主干道划分为n×n个区域.简单起见,可以将YT市看作一个正方形,每一个区域也可看作一个正方形.从而,YT城市中包括(n+1)×(n+1)个交 ...
- text-overflow使用文字超多div的宽度或超过在table中<td>
关键字:text-overflow:ellipsis 语法:text-overflow:clip | ellipsis 取值 clip:默认值.不显示省略标记(...),而是简单的裁切. ellips ...
- 【算法】最小乘积生成树 & 最小乘积匹配 (HNOI2014画框)
今天考试的时候果然题目太难于是我就放弃了……转而学习了一下最小乘积生成树. 最小乘积生成树定义: (摘自网上一篇博文). 我们主要解决的问题就是当k = 2时,如何获得最小的权值乘积.我们注意到一张图 ...
- 【CF edu 30 A. Chores】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【NOIP模拟赛】 permutation 数学(打表)
biubiu~~~ 这道题卡读题卡得很死......首先他告诉我们读循环的时候要顺着圈读,然后又说这个圈在数列上要以最大数开始读,而且以这样的循环的首数排序,得到的序列与原序列一样那么他就是可行序列, ...
- D. Sorting the Coins
Recently, Dima met with Sasha in a philatelic store, and since then they are collecting coins togeth ...
- bzoj 4004 [JLOI2015]装备购买 拟阵+线性基
[JLOI2015]装备购买 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1820 Solved: 547[Submit][Status][Dis ...
- 通过AWS的DHCP自动获取的IP地址是否会发生改变?
针对您的问题,分析如下:1.在一个VPC内,通过AWS的DHCP自动获取的IP地址,在如何情况下会发生改变?例如我把vpc的内所有100个ec2实例全部关闭,再全部重新打开,是否会发生IP地址变化的情 ...
- HDU2571--命运---DP
http://acm.hdu.edu.cn/showproblem.php?pid=2571 #include "iostream" #include "cstdio&q ...
- 【SPOJ - QTREE2】树链剖分
http://acm.hust.edu.cn/vjudge/problem/19960 题意: 有一棵N个节点的树(1<=N<=10000),N-1条边,边的编号为1~N-1,每条边有一个 ...