📌  相关文章
📜  具有重复一位数字的数字平方|一组1(3、6和9)

📅  最后修改于: 2021-04-26 18:28:13             🧑  作者: Mango

给定一个由一位数字组成的数字,找到其平方。可以假设一个数字是3、6和9。数字可以很大,即可以超过long long int。

例子:

Input : 33 66 99
Output : 
Square of 33 is : 1089
Square of 66 is : 4356
Square of 99 is : 9801

Input : 333 666 999 
Output : 
Square of 333 is : 110889
Square of 666 is : 443556
Square of 999 is : 998001

方法1(写为1111…1的倍数)
一个有趣的事实是,每个这样的数字都可以表示为1111…1的倍数。例如,33333 = 3 *11111。11、111、1111、11111…的平方分别是121,12321,1234321,123454321…。因此,一个简单的解决方案是找到111…11的平方,然后将结果与3或6或9相乘(我们可以使用大数相乘)。

方法2(使用数字模式)
对于333….333算不出数字并按以下方式打印:
假设没有数的位数是n,然后将n-1乘以1,然后将1写入0,然后再将n-1乘以8,最后写入9。
例子 :
{3333} = 11108889

对于666….666算不出数字并按以下方式打印:
假设没有数字是n,则将n-1乘以4,然后再写入1乘以3,然后再将n-1乘以5,最后写入6。
例子 :
{6666} = 44435556

对于999….999,请计算编号。数字并按以下方式打印:
假设没有数字是n,则将n-1乘以9,然后写一次8,然后再将n-1乘以0,最后写入1。
例子 :
{9999} = 99980001
下面是上述方法的实现:

C++
// C++ program to find square of 
// these large numbers
#include 
using namespace std;
  
// Function to find the square of
// 333...333, 666...666 and 999...999
string find_Square_369(string num)
{
    char a, b, c, d;
  
    // if the number is 333...333
    if (num[0] == '3')
        a = '1', b = '0', c = '8', d = '9';
  
    // if the number is 666...666
    else if (num[0] == '6')
        a = '4', b = '3', c = '5', d = '6';
  
    // if the number is 999...999
    else
        a = '9', b = '8', c = '0', d = '1';
  
    // variable for hold result
    string result = "";
  
    // find the no of digit
    int size = num.size();
  
    // add size-1 time a in result
    for (int i = 1; i < num.size(); i++)
        result += a;
  
    // add one time b in result
    result += b;
  
    // add size-1 time c in result
    for (int i = 1; i < num.size(); i++)
        result += c;
  
    // add one time d in result
    result += d;
  
    // return result
    return result;
}
  
// Drivers code
int main()
{
     
    string num_3, num_6, num_9;
    num_3 = "3333";
    num_6 = "6666";
    num_9 = "9999";
  
    string result = "";
  
    // find square of 33..33
    result = find_Square_369(num_3);
    cout << "Square of " << num_3 << " is : " << result << endl;
  
    // find square of 66..66
    result = find_Square_369(num_6);
    cout << "Square of " << num_6 << " is : " << result << endl;
  
    // find square of 66..66
    result = find_Square_369(num_9);
    cout << "Square of " << num_9 << " is : " << result << endl;
  
    return 0;
}


Java
// Java program to find square of 
// these large numbers
class GFG {
      
    // Function to find the square of
    // 333...333, 666...666 and 999...999
    static String find_Square_369(String num)
    {
        char a, b, c, d;
      
        // if the number is 333...333
        if (num.charAt(0) == '3')
            {a = '1'; b = '0'; c = '8'; d = '9';}
      
        // if the number is 666...666
        else if (num.charAt(0) == '6')
            {a = '4'; b = '3'; c = '5'; d = '6';}
      
        // if the number is 999...999
        else
            {a = '9'; b = '8'; c = '0'; d = '1';}
      
        // variable for hold result
        String result = "";
      
        // find the no of digit
        int size = num.length();
      
        // add size-1 time a in result
        for (int i = 1; i < size; i++)
            result += a;
      
        // add one time b in result
        result += b;
      
        // add size-1 time c in result
        for (int i = 1; i < size; i++)
            result += c;
      
        // add one time d in result
        result += d;
      
        // return result
        return result;
    }
      
    // Drivers code
    public static void main(String[] args)
    {
  
        String num_3, num_6, num_9;
        num_3 = "3333";
        num_6 = "6666";
        num_9 = "9999";
      
        String result = "";
      
        // find square of 33..33
        result = find_Square_369(num_3);
        System.out.println("Square of " + num_3 
                            + " is : " + result);
      
        // find square of 66..66
        result = find_Square_369(num_6);
        System.out.println("Square of " + num_9
                            + " is : " + result);
      
        // find square of 66..66
        result = find_Square_369(num_9);
        System.out.println("Square of " + num_9
                            + " is : " + result);
      
    }
}
  
// This code is contributed by Smitha.


Python 3
# Pyhton 3 program to find square of 
# these large numbers
  
# Function to find the square of
# 333...333, 666...666 and 999...999
def find_Square_369(num):
  
    # if the number is 333...333
    if (num[0] == '3'):
        a = '1'
        b = '0'
        c = '8'
        d = '9'
  
    # if the number is 666...666
    elif (num[0] == '6'):
        a = '4'
        b = '3'
        c = '5'
        d = '6'
  
    # if the number is 999...999
    else:
        a = '9'
        b = '8'
        c = '0'
        d = '1'
  
    # variable for hold result
    result = ""
  
    # find the no of digit
    size = len(num)
  
    # add size-1 time a in result
    for i in range(1, size):
        result += a
  
    # add one time b in result
    result += b
  
    # add size-1 time c in result
    for i in range(1, size):
        result += c
  
    # add one time d in result
    result += d
  
    # return result
    return result
  
  
# Drivers code
# Your Python 3 Code
  
num_3 = "3333"
num_6 = "6666"
num_9 = "9999"
  
result = ""
  
# find square of 33..33
result = find_Square_369(num_3)
print("Square of " + num_3 + " is : "
                            + result);
  
# find square of 66..66
result = find_Square_369(num_6)
print("Square of " + num_6 + " is : "
                            + result);
  
# find square of 66..66
result = find_Square_369(num_9)
print("Square of " + num_9 + " is : "
                           + result);
  
# This code is contributed by Smitha


C#
// C# program to find square of 
// these large numbers
using System;
  
class GFG {
      
    // Function to find the square of
    // 333...333, 666...666 and 999...999
    static string find_Square_369(string num)
    {
        char a, b, c, d;
      
        // if the number is 333...333
        if (num[0] == '3')
            {a = '1'; b = '0'; c = '8'; d = '9';}
      
        // if the number is 666...666
        else if (num[0] == '6')
            {a = '4'; b = '3'; c = '5'; d = '6';}
      
        // if the number is 999...999
        else
            {a = '9'; b = '8'; c = '0'; d = '1';}
      
        // variable for hold result
        string result = "";
      
        // find the no of digit
        int size = num.Length;
      
        // add size-1 time a in result
        for (int i = 1; i < size; i++)
            result += a;
      
        // add one time b in result
        result += b;
      
        // add size-1 time c in result
        for (int i = 1; i < size; i++)
            result += c;
      
        // add one time d in result
        result += d;
      
        // return result
        return result;
    }
      
    // Drivers code
    public static void Main()
    {
        string num_3, num_6, num_9;
        num_3 = "3333";
        num_6 = "6666";
        num_9 = "9999";
      
        string result = "";
      
        // find square of 33..33
        result = find_Square_369(num_3);
        Console.Write("Square of " + num_3 
                + " is : " + result + "\n");
      
        // find square of 66..66
        result = find_Square_369(num_6);
        Console.Write("Square of " + num_9 
                + " is : " + result + "\n");
      
        // find square of 66..66
        result = find_Square_369(num_9);
        Console.Write("Square of " + num_9
                + " is : " + result + "\n");
    }
}
  
// This code is contributed by Smitha


PHP


输出 :

Square of 3333 is : 11108889
Square of 6666 is : 44435556
Square of 9999 is : 99980001