给定一个数字,形式为11..1,使得其中的数字位数小于10,请找到该数字的平方。
例子:
Input : 111111
Output : 12345654321
Input : 1111
Output : 1234321
11…1(长度小于10)的平方称为Demlo数。 demlo数字是由1、2、3….n,n-1,n-2,…..1组成的数字。
Demlo编号为:
1 = 1
11 * 11 = 121
111 * 111 = 12321
1111 * 1111 = 1234321
为了找到定额数,首先我们找到增加的n个数字,然后再减少的n个数字。
C++
// CPP program to print DemloNumber
#include
using namespace std;
// To return demlo number. This function assumes
// that the length of str is smaller than 10.
string printDemlo(string str)
{
int len = str.length();
string res = "";
// Add numbers to res upto size
// of str and then add number
// reverse to it
for (int i = 1; i <= len; i++)
res += char(i + '0');
for (int i = len - 1; i >= 1; i--)
res += char(i + '0');
return res;
}
// Driver program to test printDemlo()
int main()
{
string str = "111111";
cout << printDemlo(str);
return 0;
}
Java
// Java program to print DemloNumber
public class Main {
// To return demlo number. This function assumes
// that the length of str is smaller than 10.
static String printDemlo(String str)
{
int len = str.length();
String res = "";
// Add numbers to res upto size
// of str and then add number
// reverse to it
for (int i = 1; i <= len; i++)
res += Integer.toString(i);
for (int i = len - 1; i >= 1; i--)
res += Integer.toString(i);
return res;
}
// Driver program to test printDemlo()
public static void main(String[] args)
{
String str = "111111";
System.out.println(printDemlo(str));
}
}
Python
# Python program to print Demlo Number
# To return demlo number
# Length of s is smaller than 10
def printDemlo(s):
l = len(s)
res = ""
# Add numbers to res upto size
# of s then add in reverse
for i in range(1,l+1):
res = res + str(i)
for i in range(l-1,0,-1):
res = res + str(i)
return res
# Driver Code
s = "111111"
print printDemlo(s)
# Contributed by Harshit Agrawal
C#
// C# program to print DemloNumber
using System;
public class GFG {
// To return demlo number. This function assumes
// that the length of str is smaller than 10.
static String printDemlo(String str)
{
int len = str.Length;
String res = "";
// Add numbers to res upto size
// of str and then add number
// reverse to it
for (int i = 1; i <= len; i++)
res += i.ToString();
for (int i = len - 1; i >= 1; i--)
res += i.ToString();
return res;
}
// Driver program to test printDemlo()
public static void Main()
{
String str = "111111";
Console.WriteLine(printDemlo(str));
}
}
// This code is contributed by mits
PHP
= 1; $i--)
$res .= chr($i + 48);
return $res;
}
// Driver Code
$str = "111111";
echo printDemlo($str);
// This code is contributed by mits
?>
输出:
12345654321