• luogu P1553 数字反转(升级版)


    P1553 数字反转(升级版)

    直通

    思路:

      首先使用char数组进行读入,然后直接按照题目要求进行反转即可,

      但要注意的是对零的处理:(有点类似于高精去除前导零)

        ①去除只是整数、百分数的时候,反转后的前导零

        ②去除反转后'.','/'前半部分的前导零

        ③去除反转后'.'后半部分的后缀零

        ④去除反转后'/'后半部分的前导零

          注:我的代码中因为'/'后半部分是用逆序输出的,所以删除的是后缀零

    上代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    int s,z,flag=1;
    char a[30],tmp[30],tmp2[30]; 
    
    int main() {
        cin>>a;
        int len=strlen(a);
        if(a[len-1]=='%') flag=4,len--;
        else {
            for(int i=0; i<len; i++) {
                if(a[i]=='.' || a[i]=='/') {
                    for(int j=0; j<i; j++) tmp[j]=a[i-j-1];
                    for(int j=0; j<i; j++) a[j]=tmp[j];
                    while(a[s]=='0' && s+1<i) s++; //前导零 
                    if(a[i]=='.') flag=2;
                    if(a[i]=='/') flag=3;
                    z=i+1;
                    break;
                }
            }
        }
        if(flag==1 || flag==4) {
            for(int i=0; i<len; i++) tmp[i]=a[len-i-1];
            for(int i=0; i<len; i++) a[i]=tmp[i];
            while(a[s]=='0' && s+1<len) s++; //前导零 
            if(flag==4) len++;
        } else if(flag==2) {
            for(int i=z; i<len; i++) tmp2[i]=a[len-i-1+z];
            for(int i=z; i<len; i++) a[i]=tmp2[i];
            while(a[len-1]=='0' && len-2>=z) len--; //后半部分的后缀零 
        } else if(flag==3) {
            int top=0;
            for(int i=s; i<z; i++) printf("%c",a[i]);
            for(int i=z; i<len; i++) tmp2[top++]=a[i];
            while(tmp2[top-1]=='0' && top-2>=0) top--; //后半部分的前导零 
            for(int i=top-1; i>=0; i--) printf("%c",tmp2[i]);
            return 0;
        }
        for(int i=s; i<len; i++) printf("%c",a[i]);
        return 0;
    }
    View Code
  • 相关阅读:
    [LeetCode] Max Area of Island
    [TCP/IP] TCP数据报
    [LeetCode] Number of Islands
    [LeetCode] Binary Number with Alternating Bits
    [TCP/IP] Internet协议
    [LeetCode] Isomorphic Strings
    [LeetCode] Path Sum
    [LeetCode] Count and Say
    [学习OpenCV攻略][001][Ubuntu安装及配置]
    [国嵌攻略][038][时钟初始化]
  • 原文地址:https://www.cnblogs.com/zxqxwnngztxx/p/7787701.html
Copyright © 2020-2023  润新知