Java 如何解码编码为1*100+的值;两个*10+;三个?
如何返回值Java 如何解码编码为1*100+的值;两个*10+;三个?,java,Java,如何返回值1、2和3? 以下方法正确吗?看起来没有 int EncodedValue = one*100 + two*10 + three; // the following decryption is part of another procedure int one = EncodedValue / 100; int two = EncodedValue / 10; int three = EncodedValue % 10; 假设two和two最初在0-9范围内(否则您会有歧义),您
1
、2
和3
?
以下方法正确吗?看起来没有
int EncodedValue = one*100 + two*10 + three;
// the following decryption is part of another procedure
int one = EncodedValue / 100;
int two = EncodedValue / 10;
int three = EncodedValue % 10;
假设
two
和two
最初在0-9范围内(否则您会有歧义),您希望:
int two = (encodedValue / 10) % 10;
。。。否则,它基本上会得到添加到它的
1
值的十倍。假设2
和3
最初在0-9范围内(否则你会有歧义),你想要:
int two = (encodedValue / 10) % 10;
。。。否则,它的附加值基本上是
one
的十倍。这不太管用。虽然我不能说以下各项的效率,但它确实有效:
int encoded = one*100+two*10+three
int one = encoded/100;
int two = (encoded-100*one)/10;
int three = encoded - 100*one - 10*two;
请注意,只有当所有这些变量都是一位数时,这才有效。
它也可以递归实现,允许任意长度的数字分解为数字,但简单地转换为字符串,然后再转换为字符数组可能不那么密集。这不太管用。虽然我不能说以下各项的效率,但它确实有效:
int encoded = one*100+two*10+three
int one = encoded/100;
int two = (encoded-100*one)/10;
int three = encoded - 100*one - 10*two;
请注意,只有当所有这些变量都是一位数时,这才有效。
它也可以递归实现,以允许将任意长度的数字分解为数字,但简单地转换为字符串然后再转换为字符数组可能不太密集。1、2和3始终在0..9您应该将注释编辑到问题中。1,2和3总是在0..9你应该在问题中编辑你的评论。你的假设确实在OP的评论中。@mellamokb:Oops-我看到有一条评论,我不知道它来自OP(看得太快)。你的假设确实在OP的评论中。@mellamokb:Oops-我看到有一条评论,我没有意识到这是从OP(看得太快了)。