Question: Generate 10 numbers and move first number to the end 10 times

Question

Generate 10 numbers and move first number to the end 10 times

Answers 10
Added at 2016-12-28 06:12
Tags
Question

I have code that generates 1 to 10 numbers:

for (int i = 0; i <=10; i++)
{
     for (int j = 1; j <=10; j++)
     {
         int respones = i;
         int respones1 = j;

         if (respones1 > respones)
         {
              text.append(String.valueOf(respones1));
         }
     }
}

I get this result:

12345678910
2345678910
345678910
45678910
5678910
678910
78910
8910
910
10

But, I want this result:

12345678910
23456789101
34567891012
45678910123
56789101234
67891012345
78910123456
89101234567
91012345678
10123456789

How can I get my code so that it moves the first number to the end of the string?

Answers
nr: #1 dodano: 2016-12-28 06:12

This should do:

for (int i = 0; i < 10; i++){
    int res = i + 1;
    for (int j = 1; j <= 10; j++){
         text.append(String.valueOf(res));
         if (res == 10) res = 0;
         res++;
     }
}
nr: #2 dodano: 2016-12-28 06:12

Try This Code

StringBuilder text = new StringBuilder();
StringBuilder text1 = new StringBuilder();
for (int i = 0; i <= 10; i++) {
 for (int j = 1; j <= 10; j++) {
  int respones = i;
  int respones1 = j;
  if (respones1 > respones) {
   text.append(String.valueOf(respones1));
  }
 }
 text1.append(String.valueOf(i));
 if (i > 0) {
  text.append(text1);
 }
 text.append(" ");
}
System.out.println(text.toString());
nr: #3 dodano: 2016-12-28 06:12

Try this one:

for (int i = 0; i < 10; i++) {
    for (int j = i + 1; j <= i + 10; j++) {
        int respones = i;
        int respones1 = j;
        if (respones1 > respones) {
            text.append(String.valueOf(respones1 > 10 ? respones1 % 10 : respones1));
        }
    }
}
nr: #4 dodano: 2016-12-28 06:12

Try this one

int count = 1;
for (int i = 0; i < 10; i++) {
    for (int j = i + 1; j <= i + 10; j++) {
        text.append(String.valueOf(count));
        if (count == 10) {
            count = 1;
        }
        else {
            count++;
        }
    }
    count++;
}
nr: #5 dodano: 2016-12-28 06:12
final int count = 10;
for (int i = 1; i <= count; i++) {
    String str = "";

    // Loop through count + i
    for (int j = i; j <= i + count -1; j++) {

    // If j is greater than count then just minus it from count
    final int currentValue = j <= count ? j : j-count;
    str  = str + currentValue;
   }
  System.out.println(str);
}
nr: #6 dodano: 2016-12-28 06:12

try this

StringBuilder text=new StringBuilder();
int repeat=10;
for (int index=1;index<=repeat;index++){
   int count=0;
   int temp=index;
      while (count<repeat){
         if(temp<=repeat){
            text.append(temp);
         }else {
            temp=temp-repeat;
            text.append(temp);
         }
         count++;
         temp++;
      }
      text.append(" ");
}
nr: #7 dodano: 2016-12-28 07:12

It can also be done as below:

int no = 1;
    int n = 10;
    StringBuilder sb;
    for(int i=1;i<=n;i++){
        sb = new StringBuilder();
        int cnt = 1;
        no = i;
        while(cnt<=n){
            if(no<=n){
                sb.append(no).append(" ");
                no++;
                cnt++;
            }else{
                no=(no-n);
                sb.append(no).append(" ");
                no++;
                cnt++;
            }
        }
        System.out.println(sb.toString());
    }
nr: #8 dodano: 2016-12-28 11:12

Here is a dynamic solution that works for any range of numbers where min is less than max.

    int min = 1;
    int max = 10;
    int count = max - min;

    StringBuilder sb = new StringBuilder();

    for (int i = 0; i <= count; ++i) {
        for (int j = 0; j <= count; ++j) {
            sb.append(String.valueOf(((j + i) % (count + 1)) + min));
        }
        sb.append("\n");
    }

    System.out.println(sb.toString());
nr: #9 dodano: 2016-12-28 12:12

I believe the best way to approach this would be to use a deque/queue as it makes rotating the numbers in the sequence achieveable in just two operations.

ArrayDeque<int> deque = new ArrayDeque<int>(10); // Create a queue 
for(int i = 1; i <= 10; ++i) // Fill queue with [1..10]
{
    deque.addLast(i);
}

StringBuilder builder = new StringBuilder(); // A StringBuilder will be more efficient than string concatenation
for(int i = 1; i <= 10; ++i)
{
    for(int item : deque) // Append the queue contents to the string
    {
        builder.append(item);
    }
    builder.append("\n"); // New line

    int temp = deque.removeFirst(); // Take the first item from the deque
    deque.addLast(temp); // And append it to the end of the deque
}
nr: #10 dodano: 2016-12-28 12:12

This is c#

public static void OneToTen()
{
    StringBuilder sb = new StringBuilder();
    for (int i = 1; i <= 10; i++)
    {
        sb.Clear();
        for (int j = i; j <= 10; j++)
        {
            sb.Append(j);
        }
        for (int j = 1; j < i; j++)
        {
            sb.Append(j);
        }
        Debug.WriteLine(sb.ToString());
    }

    //second solution
    Debug.WriteLine("");
    for (int i = 0; i < 10; i++)
    {
        sb.Clear();
        for (int j = 0; j < 10; j++)
        {
            sb.Append(((i+j)%10) + 1);
        }
        Debug.WriteLine(sb.ToString());
    }
    Debug.WriteLine("");
}
Source Show
◀ Wstecz