每日一题-数字反转-模拟-字符串处理

题意:

给一个数字,输出回文之后的数字,并且要满足数字的一般规范(比如说开头不能是0)

输入格式
输入共 1 行,1 个整数 N。

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

数据范围
|N|10^9
输入样例:
123
输出样例:
321
输入样例:
-380
输出样例:
-83

思路:

首先我们把它按照字符串读进来,然后对第一位是不是负号进行一个特判。
之后把后面的数字反转一下,然后遍历第一个不为0的数字进行输出即可

代码如下:

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
const int N = 100010;
char str[N];

int main()
{
    cin >> str;
    int len = strlen(str);
    if(str[0] == '-')
    {
        cout << '-';
        reverse(str + 1, str + len);
        int start = 1;
        while(start < len   && str[start] == '0')
            start ++;
        for(int i = start; i < len; i ++)
            cout << str[i];
        cout << endl;
        return 0;
    }
    reverse(str, str + len);
    int start = 0;
    while(start < len   && str[start] == '0')
        start ++;
    for(int i = start; i < len; i ++)
        cout << str[i];
    cout << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值