hdoj 1266 Reverse Number

题目链接

把数字当做字符串来处理,设立一前一后两个游标,前游标标记是否是负数,后游标标记连续的0个数。

0MS 228K

#include<cstdio>
#include<cstring>
using namespace std;

int main()
{
    int T, pbegin, pend;
    char ch[11];
    bool isPos;
    scanf("%d", &T);
    getchar();
    while(T--)
    {
        gets(ch);
        if(ch[0]=='-')
        {
            isPos=false;
            printf("-");
            pbegin = 1;
        }
        else
        {
            isPos = true;
            pbegin = 0;
        }

        pend = strlen(ch)-1;
        for(int j=strlen(ch)-1; j>=0; --j)
        {
            if(ch[j]=='0')
                --pend;
            else
                break;
        }
        for(int j = pend; j>=pbegin; --j)
        {
            printf("%c", ch[j]);
        }
        for(int i=0; i<strlen(ch)-1-pend; ++i)
            printf("0");
        printf("\n");
    }
    return 0;
}


下面用数字来标记的就LTE了,1000ms,差距还是比较大的

#include<cstdio>
#include<queue>
using namespace std;

int main()
{
    int N, num, cnt;
    bool isPos = true;
    queue<int> inSt;
    scanf("%d", &N);
    while(N--)
    {
        cnt = 0;
        scanf("%d", &num);
        isPos = num >= 0 ? true : false;
        if(!isPos)
            num = -num;
        while(!(num%10))
        {
            ++cnt;
            num /= 10;
        }

        while(num)
        {
            inSt.push(num%10);
            num /= 10;
        }

        isPos ? 0 : printf("-", num);
        while(!inSt.empty())
        {
            printf("%d", inSt.front());
            inSt.pop();
        }

        while(cnt--)
            printf("0");
        printf("\n");
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值