<NOIP> 9 . P1307 数字反转

题解:这是洛谷的第9道题目,考察的还是循环的使用。数字反转,我们就用简单的取位,放到数组里,然后输出即可。这里有几个问题需要注意:

1 . 数字可能是负数(很简单,取个绝对值,反转过来之后加上‘-’即可)

源代码:

#include <iostream>
#include <stdlib.h>
#include <vector>
#include <math.h>

using namespace std;

int main()
{
    bool isnot = false;
    long long a, b, bArray[100], counter = 0,sum = 0,c;
    cin >> a;

    a > 0 ? isnot = true : isnot = false;
    c = a = abs(a);

    if (c != 0)
    {
        while (a != 0)
        {
            b = a % 10; a = a / 10;
            if (b == 0 && counter == 0)
                continue;
            bArray[counter] = b;
            counter++;
        }

        if (isnot)
        {
            for (size_t i = 0; i <counter; i++)
                sum += pow(10, counter - 1 - i) * bArray[i];
            cout << sum << endl;
        }
        else
        {
            for (size_t i = 0; i < counter; i++)
                sum += pow(10, counter - 1 - i) * bArray[i];
            cout << -sum << endl;
        }
    }
    else
    {
        if (a == 0)
            cout << a << endl;
    }

    system("pause");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值