Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何解码编码为1*100+的值;两个*10+;三个?_Java - Fatal编程技术网

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(看得太快了)。