0分,求大佬帮助

P1307 [NOIP2011 普及组] 数字反转

small_Dongpo @ 2023-02-08 13:20:27

我的代码:

#include <iostream>
#include <stack>
using namespace std;

stack<int> st;

int main()
{
    int n;
    bool f = 0;
    cin >> n;
    if (n == 0)
    {
        cout << 0;
        return 0;
    }
    if (n < 0) 
    {
        f = 1;
        n = -n;
    }
    while (n != 0)
    {
        st.push(n % 10);
        n /= 10;
    }
    while(!st.empty())
    {
        n *= 10;
        n += st.top();
        st.pop();
    }
    if (f == 1) cout << -n;
    else cout << n;
}

by iterator_traits @ 2023-02-08 13:32:00

@Howson_20120405 可能是因为在第2个while循环之前,没有把n清零


by 0x3F @ 2023-02-08 13:45:14

不需要那么长


by 0x3F @ 2023-02-08 13:45:33

@Howson_20120405

#include <bits/stdc++.h>
using namespace std;
int n, m;
int main() {
    cin >> n;
    while (n) {
        m = m * 10 + n % 10;
        n /= 10;
    }
    cout << m << endl;
    return 0;
}

by kyrie_lrving1992 @ 2023-02-08 14:35:31

@Howson_20120405

#include <bits/stdc++.h>
using namespace std;
int n = 0 , s = 0;
int main(){
    for(cin >> n ; n != 0 ; n /= 10){
        s = s * 10 + n % 10;
    }
    cout << s;
    return 0; 
}

by small_Dongpo @ 2023-02-09 13:35:08

谢谢


|