pta Zhejiang University version "C language programming (3rd edition)" problem set exercises 6-6 Use a function to output an integer reverse number (20 points)

Exercise 6-6 Use a function to output the reverse number of an integer (20 points)

This question requires the realization of a simple function to find the inverse number of an integer.

Function interface definition:

int reverse( int number );

The function reversemust return numberthe reverse number of the integer passed in by the user .

Sample referee test procedure:

#include <stdio.h>

int reverse( int number );

int main()
{
    int n;

    scanf("%d", &n);
    printf("%d\n", reverse(n));

    return 0;
}

/* 你的代码将被嵌在这里 */

Input sample:

-12340

Sample output:

-4321

//1. If you have any questions, please leave a message to point out, thank you

//2. If you have a better idea, please leave a message

//3. Finally here, you are welcome to read

int reverse( int number)
{int N,f=1;
    int sum=0;
    //1. Determine the number of digits
    //2, and output the sum from the ones digit
    if(number==0)//if If the number is a sequence of 0, then only one 0 is returned.
        return 0;
    if(number<0)//If the number is negative, then turn it into a positive number first and use an identifier
    {         number=-number;// It first becomes a positive number         f=-1;//identifier     }     while(number)//This determines the number of cycles, that is, how many digits number is     {N=number%10;//Start one digit from the ones digit Find out one digit         number/=10;         sum=N+sum*10;//reverse order becomes a number     }     // 3. Should the result return an integer or a negative number     if(f==1)         return sum;//return the result     return -sum;//return result }












Guess you like

Origin blog.csdn.net/L_Z_jay/article/details/106106717