Dado un número num en base N , comprueba si es par o impar.
Ejemplos:
Input: num = 10, N = 8 Output: Even Explanation: 108 = 810, which is even Input: num = 122, N = 5 Output: Odd Explanation: 1225 = 3710, which is odd
Acercarse:
- Convierta el número num de base N a base decimal .
- Comprueba si el número es par o impar. Esto se puede comprobar fácilmente dividiéndolo por 2 y comprobando su resto.
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ code to check if a Octal // number is Even or Odd #include <iostream> using namespace std; // To return value of a char. int val(char c) { if (c >= '0' && c <= '9') return (int)c - '0'; else return (int)c - 'A' + 10; } // Function to convert a // number from N base to decimal int toDeci(string str, int base) { int len = str.length(); // power of base int power = 1; int num = 0; int i; // Decimal equivalent is // str[len-1]*1 + str[len-1] * // base + str[len-1]*(base^2) + ... for (i = len - 1; i >= 0; i--) { // A digit in input number // must be less than // number's base if (val(str[i]) >= base) { cout << "Invalid Number"; return -1; } num += val(str[i])* power; power = power * base; } return num; } // Returns true if n is even, else odd bool isEven(string num, int N) { int deci = toDeci(num, N); return (deci % 2 == 0); } int main() { string num = "11A"; int N = 16; if (isEven(num, N)) { cout << "Even"; } else { cout << "Odd"; } } // This code is contributed by AnkitRai01
Java
// Java code to check if a Octal // number is Even or Odd import java.io.*; class Main { // To return value of a char. static int val(char c) { if (c >= '0' && c <= '9') return (int)c - '0'; else return (int)c - 'A' + 10; } // Function to convert a // number from N base to decimal static int toDeci(String str, int base) { int len = str.length(); // power of base int power = 1; int num = 0; int i; // Decimal equivalent is // str[len-1]*1 + str[len-1] * // base + str[len-1]*(base^2) + ... for (i = len - 1; i >= 0; i--) { // A digit in input number // must be less than // number's base if (val(str.charAt(i)) >= base) { System.out.println("Invalid Number"); return -1; } num += val(str.charAt(i)) * power; power = power * base; } return num; } // Returns true if n is even, else odd public static boolean isEven( String num, int N) { int deci = toDeci(num, N); return (deci % 2 == 0); } // Driver code public static void main(String[] args) { String num = "11A"; int N = 16; if (isEven(num, N)) { System.out.println("Even"); } else { System.out.println("Odd"); } } }
Python3
# Python3 code to check if a Octal # number is Even or Odd # To return value of a char. def val(c): if (ord(c) >= ord('0') and ord(c) <= ord('9')): return ord(c) - ord('0') else: return ord(c) - ord('A') + 10 # Function to convert a # number from N base to decimal def toDeci(str, base): Len = len(str) # power of base power = 1 num = 0 # Decimal equivaLent is # str[Len-1]*1 + str[Len-1] * # base + str[Len-1]*(base^2) + ... for i in range(Len-1, -1, -1): # A digit in input number # must be less than # number's base if (val(str[i]) >= base): print("Invalid Number") return -1 num += val(str[i])*power power = power * base return num # Returns true if n is even, else odd def isEven(num, N): deci = toDeci(num, N) return (deci % 2 == 0) # Driver code if __name__ == '__main__': num = "11A" N = 16 if (isEven(num, N)): print("Even") else: print("Odd") # This code is contributed by mohit kumar 29
C#
// C# code to check if a Octal // number is Even or Odd using System; class Gfg{ // To return value of a char. static int val(char c) { if (c >= '0' && c <= '9') return (int)c - '0'; else return (int)c - 'A' + 10; } // Function to convert a // number from N base to decimal static int toDeci(string str,int base_var) { int len = str.Length; // power of base int power = 1; int num = 0; int i; // Decimal equivalent is // str[len-1]*1 + str[len-1] * // base + str[len-1]*(base^2) + ... for (i = len - 1; i >= 0; i--) { // A digit in input number // must be less than // number's base if (val(str[i]) >= base_var) { Console.WriteLine("Invalid Number"); return -1; } num += val(str[i]) * power; power = power * base_var; } return num; } // Returns true if n is even, else odd public static bool isEven( string num, int N) { int deci = toDeci(num, N); return (deci % 2 == 0); } // Driver code public static void Main(string[] args) { string num = "11A"; int N = 16; if (isEven(num, N)) { Console.WriteLine("Even"); } else { Console.WriteLine("Odd"); } } } // This code is contributed by AnkitRai01
Javascript
<script> // JavaScript code to check if a Octal // number is Even or Odd // To return value of a char. function val(c) { if (c >= '0' && c <= '9') return c.charCodeAt() - '0'.charCodeAt(); else return c.charCodeAt() - 'A'.charCodeAt() + 10; } // Function to convert a // number from N base to decimal function toDeci(str, base) { let len = str.length; // power of base let power = 1; let num = 0; let i; // Decimal equivalent is // str[len-1]*1 + str[len-1] * // base + str[len-1]*(base^2) + ... for (i = len - 1; i >= 0; i--) { // A digit in input number // must be less than // number's base if (val(str[i]) >= base) { document.write("Invalid Number"); return -1; } num += val(str[i]) * power; power = power * base; } return num; } // Returns true if n is even, else odd function isEven(num, N) { let deci = toDeci(num, N); return (deci % 2 == 0); } // Driver Code let num = "11A"; let N = 16; if (isEven(num, N)) { document.write("Even"); } else { document.write("Odd"); } </script>
Producción:
Even
Complejidad de tiempo: O (len)
Espacio Auxiliar: O(1)