📌  相关文章
📜  查找由给定数字组成的两个数字,以使它们的差可被N整除

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

给定两个数字NM ,任务是查找由M组成的两个数字作为其所有数字,以使它们的差可被N整除。
例子:

方法:
在此问题中,我们必须找到仅包含一个唯一数字的数字。假设M等于2,那么我们必须从2、22、222、2222之类的数字中找到A和B……依此类推。 A和B之间的差异应被N整除。为了满足此条件,我们必须选择A和B,以使A和B的其余部分除以N相同。
对于长度为N + 1长度的数字(仅包含一个唯一的数字M) ,我们将有N + 1个数字。如果我们将这些N + 1个数字除以N,我们将有N + 1个余数,范围为[0,N] 。由于数字可以超出整数值的范围,因此我们将数字的剩余长度作为键值对存储在Map中。一旦剩余值与Map中已经配对的值一起出现,则当前长度和映射的长度就是所需数字的长度。

下面的代码是上述方法的实现:

C++
// C++ implementation
// of the above approach
  
#include 
using namespace std;
  
// Function to implement
// the above approach
void findNumbers(int N, int M)
{
    int m = M;
  
    // Hashmap to store
    // remainder-length of the
    // number as key-value pairs
    map remLen;
  
    int len, remainder;
    // Iterate till N + 1 length
    for (len = 1; len <= N + 1; ++len) {
        remainder = M % N;
        // Search remainder in the map
        if (remLen.find(remainder)
            == remLen.end())
            // If remainder is not
            // already present insert
            // the length for the
            // corresponding remainder
            remLen[remainder] = len;
        else
            break;
  
        // Keep increasing M
        M = M * 10 + m;
        // To keep M in range of integer
        M = M % N;
    }
    // Length of one number
    // is the current Length
    int LenA = len;
    // Length of the other number
    // is the length paired with
    // current remainder in map
    int LenB = remLen[remainder];
  
    for (int i = 0; i < LenB; ++i)
        cout << m;
    cout << " ";
    for (int i = 0; i < LenA; ++i)
        cout << m;
  
    return;
}
  
// Driver code
int main()
{
    int N = 8, M = 2;
  
    findNumbers(N, M);
  
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
  
class GFG{
  
// Function to implement
// the above approach
static void findNumbers(int N, int M)
{
    int m = M;
  
    // Hashmap to store
    // remainder-length of the
    // number as key-value pairs
    Map remLen = new HashMap<>();
  
    int len, remainder = 0;
      
    // Iterate till N + 1 length
    for(len = 1; len <= N + 1; ++len)
    {
       remainder = M % N;
         
       // Search remainder in the map
       if (!remLen.containsKey(remainder))
       {
             
           // If remainder is not
           // already present insert
           // the length for the
           // corresponding remainder
           remLen.put(remainder, len);
       }
       else
       {
           break;
       }
         
       // Keep increasing M
       M = M * 10 + m;
         
       // To keep M in range of integer
       M = M % N;
    }
      
    // Length of one number
    // is the current Length
    int LenA = len;
      
    // Length of the other number
    // is the length paired with
    // current remainder in map
    int LenB = remLen.getOrDefault(remainder, 0);
  
    for(int i = 0; i < LenB; ++i)
       System.out.print(m);
    System.out.print(" ");
      
    for(int i = 0; i < LenA; ++i)
       System.out.print(m);
}
  
// Driver code
public static void main(String[] args)
{
    int N = 8, M = 2;
  
    findNumbers(N, M);
}
}
  
// This code is contributed by offbeat


Python3
# Python3 implementation
# of the above approach
  
# Function to implement
# the above approach
def findNumbers(N, M):
  
    m = M
  
    # Hashmap to store
    # remainder-length of the
    # number as key-value pairs
    remLen = {}
  
    # Iterate till N + 1 length
    for len1 in range(1, N + 1, 1):
        remainder = M % N
          
        # Search remainder in the map
        if (remLen.get(remainder) == None):
              
            # If remainder is not
            # already present insert
            # the length for the
            # corresponding remainder
            remLen[remainder] = len1
        else:
            break
  
        # Keep increasing M
        M = M * 10 + m
          
        # To keep M in range of integer
        M = M % N
          
    # Length of one number
    # is the current Length
    LenA = len1
      
    # Length of the other number
    # is the length paired with
    # current remainder in map
    LenB = remLen[remainder]
  
    for i in range(LenB):
        print(m, end = "")
    print(" ", end = "")
      
    for i in range(LenA):
        print(m, end = "")
  
    return
  
# Driver code
if __name__ == '__main__':
      
    N = 8
    M = 2
  
    findNumbers(N, M)
  
# This code is contributed by Bhupendra_Singh


C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
  
class GFG{
  
// Function to implement
// the above approach
static void findNumbers(int N, int M)
{
    int m = M;
  
    // To store remainder-length of 
    // the number as key-value pairs
    Dictionary remLen = new Dictionary();
  
    int len, remainder = 0;
      
    // Iterate till N + 1 length
    for(len = 1; len <= N + 1; ++len)
    {
        remainder = M % N;
              
        // Search remainder in the map
        if (!remLen.ContainsKey(remainder))
        {
                  
            // If remainder is not
            // already present insert
            // the length for the
            // corresponding remainder
            remLen.Add(remainder, len);
        }
        else
        {
            break;
        }
              
        // Keep increasing M
        M = M * 10 + m;
              
        // To keep M in range of integer
        M = M % N;
    }
      
    // Length of one number
    // is the current Length
    int LenA = len;
      
    // Length of the other number
    // is the length paired with
    // current remainder in map
    int LenB = remLen[remainder];
  
    for(int i = 0; i < LenB; ++i)
        Console.Write(m);
          
    Console.Write(" ");
      
    for(int i = 0; i < LenA; ++i)
        Console.Write(m);
}
  
// Driver code
public static void Main(String[] args)
{
    int N = 8, M = 2;
  
    findNumbers(N, M);
}
}
  
// This code is contributed by Amit Katiyar


输出:
22 222

时间复杂度: O(N)
辅助空间: O(1)