检查给定的十进制数是否只有 0 和 1 位
给定一个整数n ,任务是检查n是否为二进制。如果n是二进制表示,则打印true ,否则打印false 。
例子:
Input: n = 1000111
Output: true
Input: n = 123
Output: false
方法#1:使用集合首先将n的所有数字添加到集合中,然后从集合中删除0和1 ,如果集合的大小变为0 ,则数字为二进制格式。
下面是上述方法的实现:
C++
// C++ program to check whether the given number
// is in binary format
#include
using namespace std;
// Function that returns true if given number
// is in binary format i.e. number contains
// only 0's and/or 1's
bool isBinary(int number)
{
set set;
// Put all the digits of the number in the set
while (number > 0) {
int digit = number % 10;
set.insert(digit);
number /= 10;
}
// Since a HashSet does not allow duplicates so only
// a single copy of '0' and '1' will be stored
set.erase(0);
set.erase(1);
// If the original number only contained 0's and 1's
// then size of the set must be 0
if (set.size() == 0) {
return true;
}
return false;
}
// Driver code
int main()
{
int n = 1000111;
if(isBinary(n)==1)
cout<<"true"<
Java
// Java program to check whether the given number
// is in binary format
import java.util.HashSet;
import java.util.Set;
class GFG {
// Function that returns true if given number
// is in binary format i.e. number contains
// only 0's and/or 1's
static boolean isBinary(int number)
{
Set set = new HashSet<>();
// Put all the digits of the number in the set
while (number > 0) {
int digit = number % 10;
set.add(digit);
number /= 10;
}
// Since a HashSet does not allow duplicates so only
// a single copy of '0' and '1' will be stored
set.remove(0);
set.remove(1);
// If the original number only contained 0's and 1's
// then size of the set must be 0
if (set.size() == 0) {
return true;
}
return false;
}
// Driver code
public static void main(String a[])
{
int n = 1000111;
System.out.println(isBinary(n));
}
}
Python3
# Python 3 program to check whether
# the given number is in binary format
# Function that returns true if given
# number is in binary format i.e. number
# contains only 0's and/or 1's
def isBinary(number):
set1 = set()
# Put all the digits of the
# number in the set
while(number > 0):
digit = number % 10
set1.add(digit)
number = int(number / 10)
# Since a HashSet does not allow
# duplicates so only a single copy
# of '0' and '1' will be stored
set1.discard(0)
set1.discard(1)
# If the original number only
# contained 0's and 1's then
# size of the set must be 0
if (len(set1) == 0):
return True
return False
# Driver code
if __name__ == '__main__':
n = 1000111
if(isBinary(n) == 1):
print("true")
else:
print("No")
# This code is contributed by
# Surendra_Gangwar
C#
// C# program to check whether the given number
// is in binary format
using System;
using System.Collections.Generic;
public class GFG {
// Function that returns true if given number
// is in binary format i.e. number contains
// only 0's and/or 1's
static bool isBinary(int number)
{
HashSet set = new HashSet();
// Put all the digits of the number in the set
while (number > 0) {
int digit = number % 10;
set.Add(digit);
number /= 10;
}
// Since a HashSet does not allow duplicates so only
// a single copy of '0' and '1' will be stored
set.Remove(0);
set.Remove(1);
// If the original number only contained 0's and 1's
// then size of the set must be 0
if (set.Count == 0) {
return true;
}
return false;
}
// Driver code
public static void Main()
{
int n = 1000111;
Console.WriteLine(isBinary(n));
}
}
//This code is contributed by Rajput-Ji
Javascript
C++
// C++ program to check whether the
// given number is in binary format
#include
using namespace std;
// Function that returns true if
// given number is in binary format
// i.e. number contains only 0's and/or 1's
int isBinary(int number)
{
while (number > 0)
{
int digit = number % 10;
// If digit is other than 0 and 1
if (digit > 1)
return false;
number /= 10;
}
return true;
}
// Driver code
int main()
{
int n = 1000111;
if (isBinary(n) == 1)
cout << "true";
else
cout << "false";
// This code is contributed
// by Shivi_Aggarwal
}
Java
// Java program to check whether the
// given number is in binary format
class GFG {
// Function that returns true if
// given number is in binary format
// i.e. number contains only 0's and/or 1's
static boolean isBinary(int number)
{
while (number > 0) {
int digit = number % 10;
// If digit is other than 0 and 1
if (digit > 1)
return false;
number /= 10;
}
return true;
}
// Driver code
public static void main(String a[])
{
int n = 1000111;
System.out.println(isBinary(n));
}
}
Python3
# Python3 program to check whether the
# given number is in binary format
# Function that returns true if
# given number is in binary format
# i.e. number contains only 0's and/or 1's
def isBinary(number):
while (number > 0):
digit = number % 10
# If digit is other than 0 and 1
if (digit > 1):
return False
number //= 10
return True
# Driver code
if __name__ == "__main__":
n = 1000111
if (isBinary(n) == 1):
print ("true")
else:
print ("false")
# This code is contributed by ita_c
C#
// C# program to check whether the
// given number is in binary format
using System;
class GFG {
// Function that returns true if
// given number is in binary format
// i.e. number contains only 0's and/or 1's
static bool isBinary(int number)
{
while (number > 0) {
int digit = number % 10;
// If digit is other than 0 and 1
if (digit > 1)
return false;
number /= 10;
}
return true;
}
// Driver code
static void Main()
{
int n = 1000111;
Console.WriteLine(isBinary(n));
}
// This code is contributed by Ryuga
}
PHP
0)
{
$digit = $number % 10;
// If digit is other than 0 and 1
if ($digit > 1)
return false;
$number /= 10;
}
return true;
}
// Driver code
$n = 1000111;
if (isBinary($n) == 1)
echo "true";
else
echo "false";
// This code is contributed
// by Mukul Singh
Javascript
输出:
true
方法#2:原生方式
C++
// C++ program to check whether the
// given number is in binary format
#include
using namespace std;
// Function that returns true if
// given number is in binary format
// i.e. number contains only 0's and/or 1's
int isBinary(int number)
{
while (number > 0)
{
int digit = number % 10;
// If digit is other than 0 and 1
if (digit > 1)
return false;
number /= 10;
}
return true;
}
// Driver code
int main()
{
int n = 1000111;
if (isBinary(n) == 1)
cout << "true";
else
cout << "false";
// This code is contributed
// by Shivi_Aggarwal
}
Java
// Java program to check whether the
// given number is in binary format
class GFG {
// Function that returns true if
// given number is in binary format
// i.e. number contains only 0's and/or 1's
static boolean isBinary(int number)
{
while (number > 0) {
int digit = number % 10;
// If digit is other than 0 and 1
if (digit > 1)
return false;
number /= 10;
}
return true;
}
// Driver code
public static void main(String a[])
{
int n = 1000111;
System.out.println(isBinary(n));
}
}
Python3
# Python3 program to check whether the
# given number is in binary format
# Function that returns true if
# given number is in binary format
# i.e. number contains only 0's and/or 1's
def isBinary(number):
while (number > 0):
digit = number % 10
# If digit is other than 0 and 1
if (digit > 1):
return False
number //= 10
return True
# Driver code
if __name__ == "__main__":
n = 1000111
if (isBinary(n) == 1):
print ("true")
else:
print ("false")
# This code is contributed by ita_c
C#
// C# program to check whether the
// given number is in binary format
using System;
class GFG {
// Function that returns true if
// given number is in binary format
// i.e. number contains only 0's and/or 1's
static bool isBinary(int number)
{
while (number > 0) {
int digit = number % 10;
// If digit is other than 0 and 1
if (digit > 1)
return false;
number /= 10;
}
return true;
}
// Driver code
static void Main()
{
int n = 1000111;
Console.WriteLine(isBinary(n));
}
// This code is contributed by Ryuga
}
PHP
0)
{
$digit = $number % 10;
// If digit is other than 0 and 1
if ($digit > 1)
return false;
$number /= 10;
}
return true;
}
// Driver code
$n = 1000111;
if (isBinary($n) == 1)
echo "true";
else
echo "false";
// This code is contributed
// by Mukul Singh
Javascript
输出:
true