(水题)洛谷

https://www.luogu.org/problemnew/show/P1553

忘记给整数加上前导零去除的代码了。其实不去也可以,额外的进位用一个carry另外存起来就好。

#include<bits/stdc++.h>
using namespace std;
#define ll long long

char s[40];

int main(){
    scanf("%s",s);
    int n=strlen(s);
    for(int i=0;i<n;i++){
        if(s[i]=='.'){
            reverse(s,s+i);
            reverse(s+i+1,s+n);
            int haveout=0;
            for(int j=0;j<i;j++){
                if(s[j]=='0'&&haveout==0)
                    ;
                else{
                    printf("%c",s[j]);
                    haveout=1;
                }
            }
            if(haveout==0){
                printf("0");
            }

            printf(".");
            int allzero=1;
            int last=0;
            for(int j=i+1;j<n;j++){
                if(s[j]!='0'){
                    last=j;
                    allzero=0;
                }
            }

            if(allzero){
                printf("0
");
            }
            else{
                for(int j=i+1;j<=last;j++){
                    printf("%c",s[j]);
                }
                printf("
");
            }
            return 0;
        }
        else if(s[i]=='/'){
            reverse(s,s+i);
            reverse(s+i+1,s+n);
            int haveout=0;
            for(int j=0;j<i;j++){
                if(s[j]=='0'&&haveout==0)
                    ;
                else{
                    printf("%c",s[j]);
                    haveout=1;
                }
            }
            if(haveout==0){
                printf("0");
            }

            printf("/");
            haveout=0;
            for(int j=i+1;j<n;j++){
                if(s[j]=='0'&&haveout==0)
                    ;
                else{
                    printf("%c",s[j]);
                    haveout=1;
                }
            }
            if(haveout==0){
                printf("0");
            }
            printf("
");
            return 0;

        }
        else if(s[i]=='%'){
            reverse(s,s+i);
            int haveout=0;
            for(int j=0;j<i;j++){
                if(s[j]=='0'&&haveout==0)
                    ;
                else{
                    printf("%c",s[j]);
                    haveout=1;
                }
            }
            if(haveout==0){
                printf("0");
            }

            printf("\%
");
            return 0;
        }
    }

    reverse(s,s+n);
    int haveout=0;
    for(int j=0;j<n;j++){
        if(s[j]=='0'&&haveout==0)
            ;
        else{
            printf("%c",s[j]);
            haveout=1;
        }
    }
    if(haveout==0){
        printf("0");
    }
    printf("
");

}
原文地址:https://www.cnblogs.com/Yinku/p/10293811.html