数字反转

数字反转

https://ac.nowcoder.com/acm/contest/67624/P

给定一个整数,请将该数各个位上数字反转得到一个新数。新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零(参见样例2)。 示例1 输入 复制 123 输出 复制 321 示例2 输入 复制 -380 输出 复制 -83 先见示例1,我们需要反转一个数,我们只需将这个数的每一位都提取出来,然后反向输出即可,如果是负数只需先变成整数然后重复上述操作即可。详见代码:#include #include #include #define endl '\n' #define buff ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr); using namespace std; int main() { ios::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);

int N, b, sum, n;
cin >> N;
n = N;
if (n < 0)
	n = -n;
if (n % 10 == 0)
	n /= 10;
sum = 0;
while (n >= 1)
{
	b = n % 10;
	sum = sum * 10 + b;
	n /= 10;
}
if (N < 0)
{
	sum = -sum;
}
cout << sum;

return 0; }

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务