这个题典型的模拟题,各种情况要考虑:
1.为0的情况
1)0
2)00
3)0.0
2.没有小数点的情况
1)16
2)100
3)001
4) 1
3.带小数点的情况
1)100.
2)100.00
3)01.00
4)1.010
5)12.1
6)00.0010
各种考虑,最后A掉,上代码吧
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>using namespace std;const int MAX = 1000010;char s[MAX];int main(){while (scanf("%s", s) != EOF){int len = strlen(s);int pre = 0, end = len-1;while (pre<len && s[pre]=='0' && s[pre]!='.'){ //除去首部的0pre++;}while (end >=0 && s[end] == '0' && s[end] != '.'){ //除去末尾的0end--;}int mark = 0;for (int i = 0; i<len; i++) if (s[i] == '.'){ //判断是否具有小数点 mark = 1;break;}if (mark == 0){ //如果不带小数点,末尾的0不能除去end = len-1;}if (pre == end){ //值为个位数if (s[pre] == '.'){printf("0n");}else{printf("%cn",s[pre]);}continue;}if (pre > end){ //为0的一种情况 "0, 00"printf("0n");continue;}int u = pre;int e = 0, sign; //确定指数if (s[pre] != '.'){ //若指数应该为正sign = 1;while (pre<=end && s[pre] != '.'){e++;pre++;}e--;}else{ //若指数应该为负sign = -1;pre++;e++;while (s[pre] == '0'){pre++;e++;}}if (s[end] == '.' || mark==0){ //当小数位为0或者不存在的时候,确定整数的最后一个非0数字,考虑到类似100 100. 100.00while (s[end] == '0' || s[end]=='.'){end--;}}//输出结果if (sign > 0){printf("%c", s[u]);if (u != end){printf(".");for (int i = u+1; i<=end; i++) if (s[i] != '.'){printf("%c",s[i]);}}if (e > 0){printf("E%d",e);}printf("n");}else{printf("%c", s[pre]);if (pre != end){printf(".");for (int i = pre+1; i<=end; i++) {printf("%c", s[i]);}}if (e > 0){printf("E%d", -1*e);}printf("n");}}return 0;
}
本文发布于:2024-02-01 16:34:27,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170677646437978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |