C. Exponential notation

这个题典型的模拟题,各种情况要考虑:

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]!='.'){    //除去首部的0
            pre++;
        }

        while (end >=0 && s[end] == '0' && s[end] != '.'){ //除去末尾的0
            end--;
        }

        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("0\n");
            }else{
                printf("%c\n",s[pre]);
            }
            continue;
        }
        if (pre > end){     //为0的一种情况 "0, 00"
            printf("0\n");
            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.00
            while (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;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值