[Algorithm] Calculate the square of 1 number of n numbers 1

Threads:

For the S = 111…11 (n digits 1, decimal system), S ^ 2 properties.
Input
– First line: number of tests k (to<=40).
– k next line, Each line contains the number n – number of digits 1 window. (1 <= n <= 1000000)
Output
– For each test burn results 1 current.
Example
Input:
2
1
2
Output:
1
121

Solution:
We see symmetrical KQ 123…n…321. VD n = 3, the 111 ^ 2 = 12321
But notice with n = 10, the symmetrical shape of this does not seem quite right.
CEO: with n = 13 ie we 1111111111111 ^ 2.

            1111111111111
           1111111111111
          1111111111111
         1111111111111
+       1111111111111
       1111111111111
      1111111111111
     1111111111111
    1111111111111
   1111111111111
  1111111111111
 1111111111111
1111111111111
=========================
1234567901234320987654321

We see the rules of symmetry is gone. And I had an intention that this. I just write as symmetrical go, then do the following:

I will perform in each number from right to left, and we have the following code:

#include <iostream>
#include <string>
using namespace std;
int main()
{
		int k, n;

		cin>>k;
		for (int i=0; i<k; i++)
		{
			cin>>n;
			string s = "";
			int temp = 0;	// temp la phan du khi chi so cho 10. VD 13 thi temp la 1.
			int j = 1, ji = 1;
			// bat dau viet tu phai sang trai
			while (j>0)
			{
				// cong tung cap nhu hinh ((j+temp)%10)
				//sau do chen vao giua
				s.insert(0, 1, (char)((j+temp)%10+'0')); 
				temp = (j+temp)/10;
				if (j==n) ji = -1;	// quay nguoc lai khi du n so
				j += ji;
			}
		cout<<s<<endl;
		}
	return 0;
}

After obtaining the code above, KQ is a number drawn following rules:
Sequence divided 3 header section is structured as follows :
+/ striving: 123456790123456790123456790… ie repetition of paragraphs 123456790 (A-1)/9 = N1 times.
+ the middle section: 123…x…32 with x = n – 9*n1.
+/ end: n1 repeat visits 098765432 and have some 1 at last. For simplicity let's just for the middle section is symmetrically shaped ie 12321 Then we inserted into the front end of 1 at the end.

#include <iostream>
#include <string>
using namespace std;
int main()
{
	int k, n;
	
	cin>>k;
	for (int i=0; i<k; i++)
	{
		cin>>n;
		string s = "", s1 = "123456790", s2 = "098765432";
		int n1 = (n-1)/9, n2 = n - 9*n1;
		
		// doan giua
		for (int i=1; i<=n2; i++)
			s.insert(s.length(), 1, (char)(i + '0'));
			
		for (int i=n2-1; i>0; i--)
			s.insert(s.length(), 1, (char)(i + '0'));
		
		// neu n tu 10 tro len
		if (n>9)
		{
			// doan dau
			for (int i=0; i<n1; i++)
				s.insert(0, s1);
			// doan cuoi
			for (int i=0; i<n1; i++)
				s.insert(s.length()-1, s2);
		}
		cout<<s<<endl;
	}
	return 0;
}

KQ illustrate entering k = 13 and 5 paint (14, 37, 30, 113, 352) respectively as shown in Figure.