Java 不使用数组反转 int 值
谁能向我解释如何在不使用数组或字符串的情况下反转整数。我从网上得到了这段代码,但不太明白为什么+输入%10并再次除以。
while (input != 0) {
reversedNum = reversedNum * 10 + input % 10;
input = input / 10;
}
以及如何使用此示例代码仅反转奇数。例子我得到这个输入12345,然后它将奇数反转输出531。
Can anyone explain to me how to reverse an integer without using array or String. I got this code from online, but not really understand why + input % 10 and divide again.
while (input != 0) {
reversedNum = reversedNum * 10 + input % 10;
input = input / 10;
}
And how to do use this sample code to reverse only odd number. Example I got this input 12345, then it will reverse the odd number to output 531.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
// 上面的代码也适用于负数
// The above code will work for negative numbers also
反转整数
Reversing integer
我是这个程序的新手,对我的错误感到抱歉。
这里我使用了String类型到int类型的转换
I'm new to this program, sorry for my mistakes.
Here I used a conversion from String type to int type
Java 反转 int 值 - 原理
将输入 int 乘以 10 (%) 将提取最右边的数字。示例: (1234 % 10) = 4
将整数乘以 10 会将其“向左推”,在该数字的右侧显示零,示例:(5 * 10) = 50
将整数除以 10 将删除最右边的数字。 (75 / 10) = 7
Java 反转 int 值 - 伪代码:
提取输入号码最右边的数字。 (1234 % 10) = 4
b.取出该数字 (4) 并将其添加到新的 returnedNum 中。
c.将 reveredNum 乘以 10 (4 * 10) = 40,这会在 (4) 的右侧显示一个零。
d.将输入除以 10(删除最右边的数字)。 (1234 / 10) = 123
e。使用 123 重复步骤 a
Java 反转 int 值 - 工作代码
在真实的工作世界中,您永远不会做这样的事情。然而,在没有帮助的情况下解决问题的过程是将能够解决问题的人和那些想要解决问题但不能解决问题的人区分开来的,除非他们是由博客上的好人填鸭式的。
Java reverse an int value - Principles
Modding (%) the input int by 10 will extract off the rightmost digit. example: (1234 % 10) = 4
Multiplying an integer by 10 will "push it left" exposing a zero to the right of that number, example: (5 * 10) = 50
Dividing an integer by 10 will remove the rightmost digit. (75 / 10) = 7
Java reverse an int value - Pseudocode:
a. Extract off the rightmost digit of your input number. (1234 % 10) = 4
b. Take that digit (4) and add it into a new reversedNum.
c. Multiply reversedNum by 10 (4 * 10) = 40, this exposes a zero to the right of your (4).
d. Divide the input by 10, (removing the rightmost digit). (1234 / 10) = 123
e. Repeat at step a with 123
Java reverse an int value - Working code
You will never do anything like this in the real work-world. However, the process by which you use to solve it without help is what separates people who can solve problems from the ones who want to, but can't unless they are spoon fed by nice people on the blogoblags.
我不清楚你的奇数。
这段代码的工作方式是(它不是 Java 特定的算法)
例如。
输入=2345
第一次进入 while 循环
转速=5 输入=234
第二次
转速=5*10+4=54 输入=23
第三次
转速=54*10+3 输入=2
第四次
rev=543*10+2 input=0
所以反转后的数是5432。
如果你只想要反转数中的奇数。
代码是:
I am not clear about your Odd number.
The way this code works is (it is not a Java specific algorithm)
Eg.
input =2345
first time in the while loop
rev=5 input=234
second time
rev=5*10+4=54 input=23
third time
rev=54*10+3 input=2
fourth time
rev=543*10+2 input=0
So the reversed number is 5432.
If you just want only the odd numbers in the reversed number then.
The code is:
只需使用此方法
即可将此方法与您想要反转的给定值一起使用。
Simply you can use this
You can use this method with the given value which you want revers.
这就是我用来解决这个问题的解决方案,并且效果很好。
更多详细信息:
此语句将为您提供原始号码的最后一位数字。
该语句将消除原始数字的最后一位数字,因此我们确信 while 循环将终止。
这里 rev*10 会将值左移,然后添加原始值的最后一位数字。
如果原始数字是 1258,并且在运行时间中间我们有 rev = 85,num = 12,所以:
数量%10 = 2
转*10 = 850
转速*10 + 数量%10 = 852
That is the solution I used for this problem, and it works fine.
More details:
This statement will get you the last digit from the original number.
This statement will eliminate the last digit from the original number, and hence we are sure that while loop will terminate.
Here rev*10 will shift the value by left and then add the last digit from the original.
If the original number was 1258, and in the middle of the run time we have rev = 85, num = 12 so:
num%10 = 2
rev*10 = 850
rev*10 + num%10 = 852
没有循环的Java解决方案。响应速度更快。
Java solution without the loop. Faster response.
为了得到任何数字的最后一位数字,我们将它除以 10,这样我们要么得到零,要么得到一个放在最后的数字,当我们连续这样做时,我们得到作为整数反转的整个数字。
See to get the last digit of any number we divide it by 10 so we either achieve zero or a digit which is placed on last and when we do this continuously we get the whole number as an integer reversed.
这将返回整数的反转
This will return reverse of integer
只是补充一下,希望使解决方案更加完整。
@sheki 的逻辑已经给出了在 Java 中反转整数的正确方法。如果您假设您使用的输入和获得的结果始终落在
[-2147483648, 2147483647]
范围内,那么您应该可以安全地使用 @sheki 的代码。否则,捕获异常将是一个很好的做法。Java 8 引入了方法 addExact、subtractExact,multiplyExact< /em> 和 toIntExact 。这些方法将在溢出时抛出
ArithmeticException
。因此,您可以使用下面的实现来实现一种干净且更安全的方法来反转整数。一般来说,我们可以使用上述方法进行数学计算并显式处理溢出问题,如果实际使用中存在溢出的可能性,则始终建议这样做。Just to add on, in the hope to make the solution more complete.
The logic by @sheki already gave the correct way of reversing an integer in Java. If you assume the input you use and the result you get always fall within the range
[-2147483648, 2147483647]
, you should be safe to use the codes by @sheki. Otherwise, it'll be a good practice to catch the exception.Java 8 introduced the methods addExact, subtractExact, multiplyExact and toIntExact. These methods will throw
ArithmeticException
upon overflow. Therefore, you can use the below implementation to implement a clean and a bit safer method to reverse an integer. Generally we can use the mentioned methods to do mathematical calculation and explicitly handle overflow issue, which is always recommended if there's a possibility of overflow in the actual usage.这是一个完整的解决方案(如果数字溢出则返回0):
Here is a complete solution(returns 0 if number is overflown):
很高兴您写出了原始代码。我有另一种方法来编码反转整数的概念。我最多只允许 10 位数字。但是,我将假设用户不会输入零。
It's good that you wrote out your original code. I have another way to code this concept of reversing an integer. I'm only going to allow up to 10 digits. However, I am going to make the assumption that the user will not enter a zero.
即使传递负整数,它也会给出负整数
试试这个...
Even if negative integer is passed then it will give the negative integer
Try This...
这是反转
整数
的最短代码This is the shortest code to reverse an
integer
123 映射到 321,可以计算为 3*(10^2)+2*(10^1)+1
使用两个函数来计算(10^N)。第一个函数计算 N 的值。第二个函数计算 10 的 N 次方的值。
123 maps to 321, which can be calculated as 3*(10^2)+2*(10^1)+1
Two functions are used to calculate (10^N). The first function calculates the value of N. The second function calculates the value for ten to power N.
如果想法不是使用数组或字符串,则必须通过一次从末尾读取数字来完成整数的反转。下面详细解释一下,以帮助新手。
伪代码:
有关步骤 4 的更多信息:如果每次向您提供一个数字,并要求您将其附加在数字末尾,您会怎么做 - 将原始数字向左移动一位以适应数字新数字。如果数字 23 必须变成 234,则将 23 乘以 10,然后加上 4。234
= 23x10 + 4;
代码:
If the idea is not to use arrays or string, reversing an integer has to be done by reading the digits of a number from the end one at a time. Below explanation is provided in detail to help the novice.
pseudocode :
More info on step 4: If you are provided with a digit at a time, and asked to append it at the end of a number, how would you do it - by moving the original number one place to the left so as to accommodate the new digit. If number 23 has to become 234, you multiply 23 with 10 and then add 4.
234 = 23x10 + 4;
Code:
这是一个过时的问题,但作为其他人的参考
首先reverseNum必须初始化为0;
input%10 用于获取输入中的最后一位数字
input/10 用于删除输入中的最后一位数字,您已将其添加到reverseNum
假设输入是 135
135 % 10 是 5
由于反转数被初始化为0
现在reverseNum将为5
然后我们通过将135除以10来去掉5
现在输入将只是13
您的代码循环执行这些步骤,直到所有数字都添加到反转的数字中,或者换句话说,直到输入变为0。
It is an outdated question, but as a reference for others
First of all reversedNum must be initialized to 0;
input%10 is used to get the last digit from input
input/10 is used to get rid of the last digit from input, which you have added to the reversedNum
Let's say input was 135
135 % 10 is 5
Since reversed number was initialized to 0
now reversedNum will be 5
Then we get rid of 5 by dividing 135 by 10
Now input will be just 13
Your code loops through these steps until all digits are added to the reversed number or in other words untill input becomes 0.
设一个数为 168,
+ 输入 % 10 返回最后一位数字作为提醒,即 8,但下次它应该返回 6,因此数字必须从 168 减少到 16,因为将 168 除以 10 会得到 16 而不是 16.8,因为变量输入应该是整数类型在上面的程序中。
let a number be 168,
+ input % 10 returns last digit as reminder i.e. 8 but next time it should return 6,hence number must be reduced to 16 from 168, as divide 168 by 10 that results to 16 instead of 16.8 as variable input is supposed to be integer type in the above program.
如果你想反转任何数字,比如 1234,并且你想反转这个数字,让它看起来像 4321。首先,初始化 3 个变量 int org ; int 反向 = 0;和int提醒;
然后把你的逻辑写成
If you wanna reverse any number like 1234 and you want to revers this number to let it looks like 4321. First of all, initialize 3 variables int org ; int reverse = 0; and int reminder ;
then put your logic like
求小于或等于整数的10的最大幂的方法:(递归)
反转实际整数的方法:(递归)
A method to get the greatest power of ten smaller or equal to an integer: (in recursion)
The method to reverse the actual integer:(in recursion)
您可以使用递归来解决这个问题。
首先使用以下递归函数获取整数的长度。
然后你可以简单地将数字的余数乘以 10^(整数的长度 - 1)。
整个源代码:
You can use recursion to solve this.
first get the length of an integer number by using following recursive function.
and then you can simply multiply remainder of a number by 10^(Length of integer - 1).
The whole Source Code :