Luogu brush questions C++ language | P1307 Number Reversal

Learn C++ from a baby! Record the questions in the process of Luogu C++ learning and test preparation, and record every moment.

Attached is a summary post: Luogu Brush Questions C++ Language | Summary


【Description】

Given an integer  N , please reverse the digits of the number to get a new number. The new number should also satisfy the common form of integers, that is, the highest digit of the new number obtained after inversion should not be zero unless the original number given is zero (see example 2).

【enter】

an  integer N.

【Output】

An integer representing the new number after inversion.

【Input sample】

123

【Example of output】

321

【Code Explanation】

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n, s=0, t;
    bool mark=false;  //如果为负数,mark为true
    cin >> n;
    if (n<0) {
        mark = true;
        n = -1 * n;
    }
    while (n!=0) {
        t = n % 10;
        n /= 10;
        s = s * 10 + t;
    }
    if (mark) {
        s = -1 * s;
    }
    cout << s;
    return 0;
}

【operation result】

-380
-83

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/132634833