含泪求告知哪里有问题

P1307 [NOIP2011 普及组] 数字反转

wangchuanqiushui @ 2020-11-27 20:33:57

include<iostream>

using namespace std; int main() { int x; cin >> x; if (x == 0)cout << "0" << endl; else if (x > 0) { if (x % 10 == 0)x /= 10; while (x != 0) { cout << x % 10; x /= 10; } } else { x = -x; cout << "-"; if (x % 10 == 0)x /= 10; while (x != 0) { cout << x % 10; x /= 10; } } return 0; }```c


by BlueSu @ 2020-11-27 20:36:25

@wangchuanqiushui 这个代码叫人怎么看???


by 旭日临窗 @ 2020-11-27 20:37:22

@BlueSu

编译器格式化代码


by BlueSu @ 2020-11-27 20:42:01

@wangchuanqiushui 这个代码没有处理末尾有很多0的情况

测试样例 #2:-592900000

需要加一个while循环过滤掉多余的0


by cxqghzj @ 2020-11-27 20:42:43

望丰展,使md


by BlueSu @ 2020-11-27 20:47:16

@wangchuanqiushui 学过自定义函数么

#include <iostream>
using namespace std;
void f ( int x ) {
    while ( x % 10 == 0 ) { //加一个过滤末尾连续0的部件
        x /= 10 ;
    }
    while (x != 0)
    {
        cout << x % 10;
        x /= 10;
    }
}
int main()
{
    int x;
    cin >> x;
    if (x == 0)
        cout << "0" << endl;
    else if (x > 0)
    {
        f (x) ;
    }
    else
    {
        x = -x;
        cout << "-";
        f (x) ;
    }
    return 0;
}

没学过的话,这里

#include <iostream>
using namespace std;
int main()
{
    int x;
    cin >> x;
    if (x == 0)
        cout << "0" << endl;
    else if (x > 0)
    {
        while (x % 10 == 0)
        {
            x /= 10;
        }
        while (x != 0)
        {
            cout << x % 10;
            x /= 10;
        }
    }
    else
    {
        x = -x;
        cout << "-";
        while (x % 10 == 0)
        {
            x /= 10;
        }
        while (x != 0)
        {
            cout << x % 10;
            x /= 10;
        }
    }
    return 0;
}

by cxqghzj @ 2020-11-27 20:55:53

@BlueSu 这跟自定义函数有什么关系,而且不需要这么复杂吧


by cxqghzj @ 2020-11-27 20:56:26

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    int s = 0,i,k;
    cin >> k;
    for (i = k;i != 0;){
        s = s * 10 + i % 10;
        i = i / 10;
    }
    cout << s << endl;

    return 0;
}

by Aw顿顿 @ 2020-11-27 21:11:33

std::string


by BlueSu @ 2020-11-28 07:44:07

@cxqghzj 我只是想在他原来的代码上改qwq 然后如果用函数的话可能主函数会简洁一点


|