5

“将最后一位移到第一位”的意思是: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 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);
}
4

4 回答 4

8

我使用了这个功能:

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亿的真实价值。我有一种强烈的感觉,这实际上是不可能的。我的猜测是,没有任何数字可以解决这个问题,但我要让它继续运行。如果我找到一个,我会编辑这个。

编辑:似乎有这样的数字。往上看。

于 2012-12-31T20:17:43.183 回答
3

http://answers.yahoo.com/question/index?qid=20080207083949AAoBkqv

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
于 2012-12-31T21:00:11.753 回答
3

比我聪明得多的人想出了这个问题的数学方程式:

寄生数方程

...这为您提供了一种编写非暴力方法的好方法。

(其中 n = 位数,a = 偏移量)

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

我决定向你提出这个问题的人是虐待狂。:)

于 2012-12-31T22:13:34.463 回答
0

你实际上是用一支铅笔和一张纸来做这件事的。你知道 18 位数字以 2 结尾。所以两倍大的数字必须以 4 结尾。因此,2 之后的数字是 4。下一个数字是 8。你需要知道如何乘以和保留几十个(8X2 = 16, 6X2+1= 13, 3X2+1=7),然后继续直到你得到 2 而上面没有任何 1 标记。这就是我同时找到 210526315789473684 和 105263157894736842 的方式

于 2013-08-13T08:34:25.257 回答