开发者

Simple Number generator logic

开发者 https://www.devze.com 2023-02-28 03:25 出处:网络
I have a number 0127 i am trying logic to generate number following sequence in JAVA 0 1 2 7 01 12 2开发者_如何学Python7

I have a number 0127

i am trying logic to generate number following sequence in JAVA

0
1
2
7
01
12
2开发者_如何学Python7
012
127
0127
1270
2701
7012
01270
12701
27012
.
.

I am breaking my head :-)


I would convert the number to a String object, then to an array of chars.

From there, you can iterate over the array with the following logic:

        int MAX_LENGTH = 10;
        char[] array = "0127".ToCharArray();
        for (int i = 0; i < MAX_LENGTH; i++)
        {
            for (int offset = 0; offset < array.Length; offset++)
            {
                String disp = "";
                for (int j = 0; j <= i; j++)
                {
                    int index = j + offset;
                    while (index >= array.Length)
                        index -= array.Length;
                    disp += array[index];
                }
                Console.WriteLine(disp);
            }
        }

Change MAX_LENGTH to be what ever the maximum length of the output string should be.

Here is the output that this code produces:

Simple Number generator logic


Divide it up into two steps: the substrings and superstrings.

Sometimes the best way to solve a big problem is to divide the work up into smaller problems that are easier to solve.

For the substrings, use nested for loops.

  1. Loop substring length from 1 to string.length() - 1
  2. Loop starting character from 0 to string.length() - 1 - substringLength

In the inner loop, generate the substring of the given length from the given starting character. This will generate all the substrings.

For the superstrings, you only need one loop to pick the starting character. For each item in the loop, start at that character and build your string until the given length, wrapping from the last character to the first.


The first few lines starting from 0 to 0127 (inclusively) are all subsequences of {0,1,2,7} (empty set is missing).

For the rest - it's like a ring, you pick a starting number and "go" n steps in one direction:

               0
              / \
             7   1
              \ /
               2

this would produce:

n=1: 0,    1,    2,    7
n=2: 01,   12,   27,   70
n=3: 012,  127,  270,  701
n=4: 0127, 1270, 2701, 7012

But I can't see a link between the two parts - are you sure, the sequence in your question is complete, no numbers missing? especially 70, 270 and 701?


int MAX_LENGTH = 5;
        String[] numStr = {"0","1","2","7"};
        for (int i = 0; i < MAX_LENGTH; i++)
        {
            for (int offset = 0; offset < numStr.length; offset++)
            {
                if(i>0 && offset+1 == numStr.length) continue;
                String disp = "";
                for (int j = 0; j <= i; j++)
                {
                    int index = j + offset;
                    if (index >= numStr.length)
                        index -= numStr.length;
                    disp += numStr[index];
                }
                System.out.println(disp);

            }
        }


This is my solution. It was executed with the tested number and it generates the same sequence:

public static void printSequence(String number) {
    final char[] charNumber = number.toCharArray();
    final int sizeNumber = number.length();

    int MAX_ITER = 5;
    for (int i = 0; i < MAX_ITER; i++) {
        for (int offset = 0; offset < sizeNumber; offset++) {
            String subSequence = "";
            for (int j = 0; j <= i; j++) {
                int index = (j + offset) % sizeNumber;
                subSequence += charNumber[index];
            }
            System.out.println(subSequence);
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消