📜  邪恶号

📅  最后修改于: 2021-05-04 19:42:58             🧑  作者: Mango

邪恶数是一个非负数,二进制扩展中的偶数为1。 (二进制扩展名-是二进制数字系统或基数2的数字系统中的数字表示,该数字使用两个不同的符号表示数字值:通常为0(零)和1(一))。

恶臭数字:不是邪恶的数字称为恶臭数字。给定一个数字,任务是检查它是邪恶数字还是恶臭数字。
例子 :

Input : 3
Output : Evil Number
Explanation: Binary expansion of 3 is 11,
the number of 1s in this is 2 i.e even. 

Input : 16
Output : Odious Number(not an evil number)
Explanation: Binary expansion of 16 = 10000, 
having number of 1s =1 i.e odd.

Input : 23
Output : Evil Number
Explanation: Binary expansion of 23 is 10111,
the number of 1s in this is 4 i.e even. 
C/C++
// C/C++ program to check if a number is
// Evil number or Odious Number
#include 
using namespace std;
#include 
  
// returns number of 1s from the binary number
int count_one(int n)
{
    int c_one = 0;
    while (n != 0) {
        int rem = n % 10;
  
        // counting 1s
        if (rem == 1)
            c_one = c_one + 1;
        n = n / 10;
    }
    return c_one;
}
  
// Check if number is evil or not
int checkEvil(int n)
{
    int i = 0, bin = 0, n_one = 0;
  
    // converting n to binary form
    while (n != 0) {
        // calculating remainder
        int r = n % 2;
  
        // storing the remainders in binary
        // form as a number
        bin = bin + r * (int)(pow(10, i));
        n = n / 2;
    }
  
    // Calling the count_one function to count
    // and return number of 1s in bin
    n_one = count_one(bin);
    if (n_one % 2 == 0)
        return 1;
    else
        return 0;
}
  
// Driver Code
int main(void)
{
    int i, check, num;
    num = 32;
    check = checkEvil(num);
    if (check == 1)
        cout << num << " is Evil Number\n";
    else
        cout << num << " is Odious Number\n";
    return 0;
}
  
// This code is contributed by Nikita Tiwari.


Java
// Java program to check if a number is
// Evil number or Odious Number
  
class GFG {
    // returns number of 1s from the binary number
    static int count_one(int n)
    {
        int c_one = 0;
        while (n != 0) {
            int rem = n % 10;
  
            // counting 1s
            if (rem == 1)
                c_one = c_one + 1;
            n = n / 10;
        }
        return c_one;
    }
  
    // Check if number is evil or not
    static int checkEvil(int n)
    {
        int i = 0, bin = 0, n_one = 0;
  
        // converting n to binary form
        while (n != 0) {
            // calculating remainder
            int r = n % 2;
  
            // storing the remainders in binary
            // form as a number
            bin = bin + r * (int)(Math.pow(10, i));
            n = n / 2;
        }
  
        // Calling the count_one function to count
        // and return number of 1s in bin
        n_one = count_one(bin);
        if (n_one % 2 == 0)
            return 1;
        else
            return 0;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int i, check, num;
        num = 32;
        check = checkEvil(num);
        if (check == 1)
            System.out.println(num + " is Evil Number");
        else
            System.out.println(num + " is Odious Number");
    }
}
  
/* This code is contributed by Mr. Somesh Awasthi */


Python
# Python program to check if a number is 
# Evil number or Odious number
  
# returns number of 1s from the binary number
def count_one(n):
    c_one = 0
    while n != 0:
        rem = n % 10
          
        # Counting 1s
        if rem == 1:
            c_one = c_one + 1
        n = n / 10
          
    return c_one
  
# Check if nnumber is evil or not
def checkEvil(n):
    i = 0
    binary = 0
      
    # Converting n to binary form
    while n != 0:
        r = n % 2
        # Calculating Remainder
        # Storing the remainders in binary
        # form as a number
        binary = binary + r*(int(10**i))
        n = n / 2
          
    # Calling the count_one function to count
    # and return number of 1s in bin
    n_one = count_one(binary)
    if n_one % 2 == 0:
        return True
    return False
      
# Driver Code
num = 32
check = checkEvil(num)
if check:
    print num, "is Evil Nummber"
else:
    print num, "is Odious Number"        
          
# Contributed by Harshit Agrawal


C#
// C# program to check if a number is
// Evil number or Odious Number
using System;
  
class GFG {
  
    // Returns number of 1s from
    // the binary number
    static int count_one(int n)
    {
        int c_one = 0;
        while (n != 0) {
            int rem = n % 10;
  
            // counting 1s
            if (rem == 1)
                c_one = c_one + 1;
            n = n / 10;
        }
        return c_one;
    }
  
    // Check if number is evil or not
    static int checkEvil(int n)
    {
        int i = 0, bin = 0, n_one = 0;
  
        // converting n to binary form
        while (n != 0) {
            // calculating remainder
            int r = n % 2;
  
            // storing the remainders in
            // binary form as a number
            bin = bin + r * (int)(Math.Pow(10, i));
            n = n / 2;
        }
  
        // Calling the count_one function to count
        // and return number of 1s in bin
        n_one = count_one(bin);
        if (n_one % 2 == 0)
            return 1;
        else
            return 0;
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
        int check, num;
        num = 32;
        check = checkEvil(num);
        if (check == 1)
            Console.WriteLine(num + " is Evil Number");
        else
            Console.WriteLine(num + " is Odious Number");
    }
}
  
// This code is contributed by vt_m.


PHP


输出 :

32 is Odius Number