本文介绍了C中的倒数功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,如果我输入092,它不会返回290,而是返回29,这不是我想要的.有什么建议吗?

So if I input say 092, it does not return 290, instead returns 29 which is not what I want it to do. Any advice?

int reverseNumber(int number){
    int ans = 0;
    while(number > 0) {
        int rightDigit;
        rightDigit = number % 10;
        ans =ans *10 + rightDigit;
        number = number / 10;
    }
    return ans;
}

推荐答案

这是因为 029 29 十进制没有区别.为此,您需要将输入作为字符串,然后反转字符.之后,您可以将结果字符串转换为数字.

It is because 029 is no different from 29 decimal. For this to work you need to take the input as a string instead, and invert the characters. After that you can convert the resulting string to a number.

这篇关于C中的倒数功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 15:45
查看更多