📜  检查十六进制数的可除性

📅  最后修改于: 2021-05-04 11:20:49             🧑  作者: Mango

给定一个由大十六进制数组成的字符串S ,任务是检查给定十进制数M的可除性。如果可分割,则打印“是”,否则打印“否”

例子:

方法:本文将使用一种方法来避免溢出。从背面迭代整个字符串。
如果子串S [0…i]的其余部分在与M相除时是已知的。我们将此余数称为R。当子字符串S [0…i + 1]被分割时,可用于获取余数。为此,首先将字符串S [0…i]左移1 。这相当于将字符串乘以16 。然后,将S [i + 1]添加到此,并使用M修改其mod。通过一点点模块化算术,它可以归结为

因此,继续上述步骤,直到字符串。如果最后的余数为0,则该字符串被整除,否则不能被整除。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
const string CHARS = "0123456789ABCDEF";
const int DIGITS = 16;
  
// Function that returns true
// if s is divisible by m
bool isDivisible(string s, int m)
{
    // Map to map characters to real values
    unordered_map mp;
  
    for (int i = 0; i < DIGITS; i++) {
        mp[CHARS[i]] = i;
    }
  
    // To store the remainder at any stage
    int r = 0;
  
    // Find the remainder
    for (int i = 0; i < s.size(); i++) {
        r = (r * 16 + mp[s[i]]) % m;
    }
  
    // Check the value of remainder
    if (!r)
        return true;
    return false;
}
  
// Driver code
int main()
{
    string s = "10";
    int m = 3;
  
    if (isDivisible(s, m))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
static char []CHARS = "0123456789ABCDEF".toCharArray();
static int DIGITS = 16;
  
// Function that returns true
// if s is divisible by m
static boolean isDivisible(String s, int m)
{
    // Map to map characters to real values
    Map mp = new HashMap<>();
  
    for (int i = 0; i < DIGITS; i++)
    {         
        mp. put(CHARS[i], i);
    }
  
    // To store the remainder at any stage
    int r = 0;
  
    // Find the remainder
    for (int i = 0; i < s.length(); i++) 
    {
        r = (r * 16 + mp.get(s.charAt(i))) % m;
    }
  
    // Check the value of remainder
    if (r == 0)
        return true;
    return false;
}
  
// Driver code
public static void main(String []args) 
{
    String s = "10";
    int m = 3;
  
    if (isDivisible(s, m))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
CHARS = "0123456789ABCDEF"; 
DIGITS = 16; 
  
# Function that returns true 
# if s is divisible by m 
def isDivisible(s, m) :
  
    # Map to map characters to real value
    mp = dict.fromkeys(CHARS, 0); 
  
    for i in range( DIGITS) :
        mp[CHARS[i]] = i; 
  
    # To store the remainder at any stage 
    r = 0; 
  
    # Find the remainder 
    for i in range(len(s)) :
        r = (r * 16 + mp[s[i]]) % m; 
  
    # Check the value of remainder 
    if (not r) :
        return True; 
          
    return False; 
  
# Driver code 
if __name__ == "__main__" : 
      
    s = "10"; 
    m = 3; 
  
    if (isDivisible(s, m)) :
        print("Yes"); 
    else :
        print("No"); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG
{
  
static char []CHARS = "0123456789ABCDEF".ToCharArray();
static int DIGITS = 16;
  
// Function that returns true
// if s is divisible by m
static bool isDivisible(String s, int m)
{
    // Map to map characters to real values
    Dictionary mp = new Dictionary();
  
    for (int i = 0; i < DIGITS; i++)
    {         
        if(mp.ContainsKey(CHARS[i]))
            mp[CHARS[i]] = i;
        else
            mp.Add(CHARS[i], i);
    }
  
    // To store the remainder at any stage
    int r = 0;
  
    // Find the remainder
    for (int i = 0; i < s.Length; i++) 
    {
        r = (r * 16 + mp[s[i]]) % m;
    }
  
    // Check the value of remainder
    if (r == 0)
        return true;
    return false;
}
  
// Driver code
public static void Main(String []args) 
{
    String s = "10";
    int m = 3;
  
    if (isDivisible(s, m))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
  
// This code is contributed by 29AjayKumar


输出:
No