Dados dos números hexadecimales N y K , la tarea es encontrar N módulo K.
Ejemplos:
Entrada: N = 3E8, K = 13
Salida: C
Explicación:
La representación decimal de N( = 3E8) es 1000 La
representación decimal de K( = 13) es 19
La representación decimal de (N % K) = 1000 % 19 = 12 ( = C).
Por lo tanto, la salida requerida es C.Entrada: N = 2A3, K = 1A
Salida: 19
Enfoque: siga los pasos a continuación para resolver el problema:
- Convierta los números hexadecimales, N y K en sus números decimales equivalentes, por ejemplo, X e Y respectivamente.
- Convierta el número decimal, (X % Y) en sus números hexadecimales equivalentes, digamos, res
- Finalmente, imprima el valor de res .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to calculate modulus of // two Hexadecimal numbers void hexaModK(string s, string k) { // Store all possible // hexadecimal digits map<char, int> mp; // Iterate over the range ['0', '9'] for(char i = 1; i <= 9; i++) { mp[i + '0'] = i; } mp['A'] = 10; mp['B'] = 11; mp['C'] = 12; mp['D'] = 13; mp['E'] = 14; mp['F'] = 15; // Convert given string to long long m = stoi(k, 0, 16); // Base to get 16 power long base = 1; // Store N % K long ans = 0; // Iterate over the digits of N for(int i = s.length() - 1; i >= 0; i--) { // Stores i-th digit of N long n = mp[s[i]] % m; // Update ans ans = (ans + (base % m * n % m) % m) % m; // Update base base = (base % m * 16 % m) % m; } // Print the answer converting // into hexadecimal stringstream ss; ss << hex << ans; string su = ss.str(); transform(su.begin(), su.end(), su.begin(), ::toupper); cout << (su); } // Driver Code int main() { // Given string N and K string n = "3E8"; string k = "13"; // Function Call hexaModK(n, k); return 0; } // This code is contributed by sallagondaavinashreddy7
Java
// Java program to implement // the above approach import java.util.*; public class Main { // Function to calculate modulus of // two Hexadecimal numbers static void hexaModK(String N, String k) { // Store all possible // hexadecimal digits HashMap<Character, Integer> map = new HashMap<>(); // Iterate over the range ['0', '9'] for (char i = '0'; i <= '9'; i++) { map.put(i, i - '0'); } map.put('A', 10); map.put('B', 11); map.put('C', 12); map.put('D', 13); map.put('E', 14); map.put('F', 15); // Convert given string to long long m = Long.parseLong(k, 16); // Base to get 16 power long base = 1; // Store N % K long ans = 0; // Iterate over the digits of N for (int i = N.length() - 1; i >= 0; i--) { // Stores i-th digit of N long n = map.get(N.charAt(i)) % m; // Update ans ans = (ans + (base % m * n % m) % m) % m; // Update base base = (base % m * 16 % m) % m; } // Print the answer converting // into hexadecimal System.out.println( Long.toHexString(ans).toUpperCase()); } // Driver Code public static void main(String args[]) { // Given string N and K String n = "3E8"; String k = "13"; // Function Call hexaModK(n, k); } }
Python3
# Python3 program to implement # the above approach # Function to calculate modulus of # two Hexadecimal numbers def hexaModK(s, k) : # Store all possible # hexadecimal digits mp = {}; # Iterate over the range ['0', '9'] for i in range(1, 10) : mp[chr(i + ord('0'))] = i; mp['A'] = 10; mp['B'] = 11; mp['C'] = 12; mp['D'] = 13; mp['E'] = 14; mp['F'] = 15; # Convert given string to long m = int(k); # Base to get 16 power base = 1; # Store N % K ans = 0; # Iterate over the digits of N for i in range(len(s) - 1, -1, -1) : # Stores i-th digit of N n = mp[s[i]] % m; # Update ans ans = (ans + (base % m * n % m) % m) % m; # Update base base = (base % m * 16 % m) % m; # Print the answer converting # into hexadecimal ans = hex(int(ans))[-1].upper() print(ans) # Driver Code if __name__ == "__main__" : # Given string N and K n = "3E8"; k = "13"; # Function Call hexaModK(n, k); # This code is contributed by AnkThon
C#
// C# program to implement // the above approach using System; using System.Collections.Generic; class GFG{ // Function to calculate modulus of // two Hexadecimal numbers static void hexaModK(String N, String k) { // Store all possible // hexadecimal digits Dictionary<char, int> map = new Dictionary<char, int>(); // Iterate over the range ['0', '9'] for(char i = '0'; i <= '9'; i++) { map.Add(i, i - '0'); } map.Add('A', 10); map.Add('B', 11); map.Add('C', 12); map.Add('D', 13); map.Add('E', 14); map.Add('F', 15); // Convert given string to long long m = long.Parse(k); // Base to get 16 power long Base = 1; // Store N % K long ans = 0; // Iterate over the digits of N for(int i = N.Length - 1; i >= 0; i--) { // Stores i-th digit of N long n = map[N[i]] % m; // Update ans ans = (ans + (Base % m * n % m) % m) % m; // Update base Base = (Base % m * 16 % m) % m; } // Print the answer converting // into hexadecimal Console.WriteLine(ans.ToString("X")); } // Driver Code public static void Main(String []args) { // Given string N and K String n = "3E8"; String k = "13"; // Function Call hexaModK(n, k); } } // This code is contributed by Princi Singh
Javascript
<script> // Javascript program to implement // the above approach // Function to calculate modulus of // two Hexadecimal numbers function hexaModK(s, k) { // Store all possible // hexadecimal digits var mp = new Map(); // Iterate over the range ['0', '9'] for(var i = 1; i <= 9; i++) { mp.set(String.fromCharCode( i + '0'.charCodeAt(0)), i); } mp.set('A', 10); mp.set('B', 11); mp.set('C', 12); mp.set('D', 13); mp.set('E', 14); mp.set('F', 15); // Convert given string to long var m = parseInt(k, 16); // Base to get 16 power var base = 1; // Store N % K var ans = 0; // Iterate over the digits of N for(var i = s.length - 1; i >= 0; i--) { // Stores i-th digit of N var n = mp.get(s[i]) % m; // Update ans ans = (ans + (base % m * n % m) % m) % m; // Update base base = (base % m * 16 % m) % m; } document.write(ans.toString(16).toUpperCase()); } // Driver Code // Given string N and K var n = "3E8"; var k = "13"; // Function Call hexaModK(n, k); // This code is contributed by famously </script>
Producción:
C
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por hemanthswarna1506 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA