字符串秒解

P1307 [NOIP2011 普及组] 数字反转

HFColin @ 2017-03-01 14:12:26

#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    char k[11];
    bool b=true;
    scanf("%s",k);
    if (k[0]=='-')
    {
        printf("-");
        for (int i=strlen(k)-1;i>0;i--)
        {
            if (b&&k[i]=='0') continue;
            printf("%c",k[i]);
            b=false;
        }
    }
    else
    {
        for (int i=strlen(k)-1;i>=0;i--)
        {
            if (b&&k[i]=='0') continue;
            printf("%c",k[i]);
            b=false;
        }
    }
    return 0;
}

by Mojiu @ 2017-03-08 13:53:25

if (b&&k[i]=='0') continue;

        printf("%c",k[i]);

        b=false;

这段是什么意思?


by 野生小卒 @ 2017-03-12 19:18:38

翻版(C代码)


#include<stdio.h>
#include<string.h>
int main()
{
    char k[11];
    int b=1;
    scanf("%s",k);
    if (k[0]=='-')
        printf("-");
    for (int i=strlen(k)-1;i>=0;i--)
        {
            if(i==0&&k[0]=='-')
                break;
            if(b==1&&k[i]=='0')
                continue;
            printf("%c",k[i]);
            b=0;
        }
    return 0;
}

|