Java 如果我们把最后一个数字移到第一个,那么如何找到一个数字X将等于双X
“将最后一位移到第一位”表示:12345-->51234 有人告诉我一定有答案。我认为它应该很简单,但我就是找不到任何X即使编码。有人帮忙吗Java 如果我们把最后一个数字移到第一个,那么如何找到一个数字X将等于双X,java,numbers,Java,Numbers,“将最后一位移到第一位”表示:12345-->51234 有人告诉我一定有答案。我认为它应该很简单,但我就是找不到任何X即使编码。有人帮忙吗 public static void main(String[] args) { for(int i=10; s<99999; i++){ if(2*i==convertInt(i)){ System.out.println(i); } } } private static i
public static void main(String[] args) {
for(int i=10; s<99999; i++){
if(2*i==convertInt(i)){
System.out.println(i);
}
}
}
private static int convertInt(int i) {
String s = i+"";
int index = s.length()-1;
String newS = s.charAt(index) + s.substring(0, index);
return Integer.parseInt(newS);
}
publicstaticvoidmain(字符串[]args){
对于(int i=10;s我使用了此函数:
public static boolean isMultiple(int x) {
String g = String.valueOf(x);
g = g.substring(g.length()-1) + g.substring(0, g.length()-1);
int y = Integer.parseInt(g);
if(y == x * 2) return true;
return false;
}
在此循环中:
int x = 10;
while(!isMultiple(x)) {
System.out.println(x + ", " + isMultiple(x));
x++;
}
我还没有得到高达6.5亿的真实价值。我有一种强烈的感觉,这实际上是不可能的。我的猜测是,没有任何数字会适用于这一点,但我会让它继续运行。如果我找到一个,我会编辑它
编辑:似乎有这样的数字。见上文。
有一个比我聪明得多的人想出了这个问题的数学方程式:
…这为您提供了一种编写非暴力方法代码的好方法
(其中n=位数,a=偏移量)
我认为向你提出这个问题的人是个虐待狂。你可以用铅笔和一张纸来做这件事。
你知道18位数字以2结尾。所以两倍大的数字必须以4结尾。
因此,2后面的数字是4。下一个数字是8。您需要知道如何相乘并保留打数(8X2=16,6X2+1=13,3X2+1=7)然后你继续,直到你得到2,上面没有任何1分。这就是我如何同时找到210526315789473684和105263157894736842的原因也许没有一个x
小于99999的值,这并不意味着没有答案。什么是“s”在你的for循环中?是的,这可能是我找不到那个数字的唯一原因,我以前把I设为一个长的数字,性能变得很差,但仍然找不到任何东西。我希望有人能以优雅的方式解决这个问题。事实上,这个工作的数字相当大(一个是105263157894736842)。暴力手段不太可能奏效。谷歌搜索@A.R.S.给出的值会得到一些有趣的结果。也就是说@A.R.S.刚刚找到一个数字:105263157894736842。我建议你停止线性搜索。哦,哈。这是一个有趣的数字。很高兴知道,谢谢![编辑]在isMultiple
中,您可以返回Integer.parseInt(String.valueOf(x%10)+(x/10))==x*2
以避免子字符串调用。这可能会让您在循环中获得更高的值,但仍然无法接近实际值。
Let's start with the number without the two. Call it n.
If you multiply this by 10 and add 2 you would have your first number:
10n + 2
Doubling this you'd have:
20n + 4
This should be equivalent to taking n and putting a two in the front.
n + 2 * 10^d
Here d represents the number of digits.
20n + 4 = n + 2* 10^d
19n + 4 = 2 * 10^d
n = (2 * 10^d - 4) / 19
If you try values of d = 1, 2, 3, etc. you eventually get to n = 17 that gives you an integer result:
n = (2 * 10^17 - 4) / 19
n = (200000000000000000 - 4) / 19
n = 199999999999999996 / 19
n = 10526315789473684
Just put a 2 at the end and you have your answer:
105263157894736842
x 2
---------------------------------
210526315789473684
Edit: I just read Joseph's method and I like it better.
2 / 2 = 1 remainder 0 (carry down 1 to the next division)
1 / 2 = 0 remainder 1 (carry down 10 to the next division)
10 / 2 = 5 remainder 0 (carry down 5 to the next division)
5 / 2 = 2 remainder 1 (carry down 12 to the next division)
12 / 2 = 6 remainder 0 (carry down 6 to the next division)
6 / 2 = 3 remainder 0 (carry down 3 to the next division)
3 / 2 = 1 remainder 1 (carry down 11 to the next division)
11 / 2 = 5 remainder 1 (carry down 15 to the next division)
15 / 2 = 7 remainder 1 (carry down 17 to the next division)
17 / 2 = 8 remainder 1 (carry down 18 to the next division)
18 / 2 = 9 remainder 0 (carry down 9 to the next division)
9 / 2 = 4 remainder 1 (carry down 14 to the next division)
14 / 2 = 7 remainder 0 (carry down 7 to the next division)
7 / 2 = 3 remainder 1 (carry down 13 to the next division)
13 / 2 = 6 remainder 1 (carry down 16 to the next division)
16 / 2 = 8 remainder 0 (carry down 8 to the next division)
8 / 2 = 4 remainder 0 (carry down 4 to the next division)
4 / 2 = 2 remainder 0
You can stop here because you have gotten to a 2. Now read the digits going down:
105263157894736842
Note: technically you could repeat the process and get a larger number, but it would just be this sequence of digits repeated, e.g.:
105263157894736842 105263157894736842
One final note:
You could even do this the other direction and it might be easier:
The lowest digit is 2. If you double this, you get 4.
..42
Then double the 4:
..842
Then double the 8 (remember the carry):
..(1)6842
Then double the 6 and add the carry (13), that gets you another carry:
..(1)36842
Double the 3 and add the carry (7)
..736842
Double the 7 (remember the carry)
..(1)4736842
Double the 4 and add the carry (9)
..94736842
Double the 9 and remember the carry
..(1)894736842
Double the 8 and add the carry (17), remember the new carry:
..(1)7894736842
etc.
..(1)57894736842
..(1)157894736842
..3157894736842
..63157894736842
..(1)263157894736842
..5263157894736842
..(1)05263157894736842
Now when you double you get back to the digit 2 so you are done:
Answer:
105263157894736842
txtResult.Text = string.Empty;
for (int n = 2; n <= 100; n++)
{
if (((ulong)(Math.Pow(10, n)) - 2) % 19 == 0)
{
for (ulong a = 1; a <= 9; a++)
{
var omgwut = (ulong)((decimal)Math.Pow(10, n+1) / 19m) * a;
txtResult.Text = string.Format("{0}, {1}", omgwut.ToString(), txtResult.Text);
}
}
}
473684210526315789,
421052631578947368,
368421052631578947,
315789473684210526,
263157894736842105,
210526315789473684,
157894736842105263,
105263157894736842,
52631578947368421