首页 > 其他 > 详细

洛谷 P1553 数字反转(升级版)

时间:2019-01-20 00:51:09      阅读:308      评论:0      收藏:0      [点我收藏+]

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

忘记给整数加上前导零去除的代码了。

#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\n");
            }
            else{
                for(int j=i+1;j<=last;j++){
                    printf("%c",s[j]);
                }
                printf("\n");
            }
            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("\n");
            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("\%\n");
            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("\n");

}

 

洛谷 P1553 数字反转(升级版)

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!