<Sicily>数字反转

一、题目描述

给定一个整数,请将该数各个位上数字反转得到一个新数。新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零(参见样例2)。

二、输入

输入共 1 行,一个整数N。

-1,000,000,000 ≤ N≤ 1,000,000,000。

三、输出

输出共 1 行,一个整数,表示反转后的新数。

例如:
输入:123
输出:321
输入:-380
输出:-83

四、解题思路

1、每次对原数对10求模

2、原数对10求商(取整数部分)

3、利用求的模逆转过来

五、代码

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

using namespace std;

int main()
{

    int initDouble;
    cin >> initDouble;

    int temp = initDouble;
    int result = 0;

    while(temp != 0)
    {
        int num = temp % 10;
        temp = temp / 10;
        result *= 10;
        result += num;
    }

    cout << result << endl;

    return 0;
}
posted @ 2016-06-08 00:49  chenximcm  阅读(130)  评论(0编辑  收藏  举报