📌  相关文章
📜  在一行中生成大小为 2 到全 0 的给定二进制数组

📅  最后修改于: 2022-05-13 01:57:49.594000             🧑  作者: Mango

在一行中生成大小为 2 到全 0 的给定二进制数组

给定一个二进制数组arr[N] ,(其中N = 2 )大小为 2,至少有一个元素为零。任务是编写一个单行函数来将数组的两个元素都设置为零。编写函数有一个限制。不能使用三元运算符和元素的直接赋值。

本文讨论了以下方法:

  1. 仅使用赋值运算符。
  2. 使用赋值运算符两次。
  3. 否定 (!)运算符(逻辑非)。

让我们开始详细讨论这些方法。

1.仅使用赋值运算符:

赋值运算符可用于将给定二进制数组的两个元素都设置为 0,但在这种方法中,不直接使用索引。

方法:

有三种方法可以实现这一点:

下面是实现该方法的 C++ 代码:

C++
// C++ program to set both elements
// to 0 in binary array[2].
#include 
using namespace std;
 
void MakeBothZeros(int arr[])
{
    arr[arr[1]] = arr[arr[0]];
 
    // Two other approaches to solve
    // the problem
    // arr[arr[1]] = 0;
    // arr[1 - arr[0]] = arr[1 - arr[1]];
}
 
// Driver code
int main()
{
    int First_Arr[] = {0, 1};
    MakeBothZeros(First_Arr);
    cout << First_Arr[0] << " " <<
            First_Arr[1] << endl;
 
    int Second_Arr[] = {1, 0};
    MakeBothZeros(Second_Arr);
    cout << Second_Arr[0] << " " <<
            Second_Arr[1] << endl;
 
    int Thrd_Arr[] = {0, 0};
    MakeBothZeros(Thrd_Arr);
    cout << Thrd_Arr[0] << " " <<
            Thrd_Arr[1] << endl;
 
    return 0;
}


Java
// Java program to set both elements
// to 0 in binary array[2]
import java.util.*;
class GFG{
 
static void MakeBothZeros(int arr[])
{
    arr[arr[1]] = arr[arr[0]];
 
    // Two other approaches to solve
    // the problem
    // arr[arr[1]] = 0;
    // arr[1 - arr[0]] = arr[1 - arr[1]];
}
 
// Driver code
public static void main(String[] args)
{
    int First_Arr[] = {0, 1};
    MakeBothZeros(First_Arr);
    System.out.print(First_Arr[0]+ " " + 
            First_Arr[1] +"\n");
 
    int Second_Arr[] = {1, 0};
    MakeBothZeros(Second_Arr);
    System.out.print(Second_Arr[0]+ " " + 
            Second_Arr[1] +"\n");
 
    int Thrd_Arr[] = {0, 0};
    MakeBothZeros(Thrd_Arr);
    System.out.print(Thrd_Arr[0]+ " " + 
            Thrd_Arr[1] +"\n");
 
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python code for the above approach
def MakeBothZeros(arr):
    arr[arr[1]] = arr[arr[0]]
 
    # Two other approaches to solve
    # the problem
    # arr[arr[1]] = 0;
    # arr[1 - arr[0]] = arr[1 - arr[1]];
 
# Driver code
First_Arr = [0, 1]
MakeBothZeros(First_Arr)
print(f"{First_Arr[0]} {First_Arr[1]}  ")
 
Second_Arr = [1, 0]
MakeBothZeros(Second_Arr)
print(f"{Second_Arr[0]} {Second_Arr[1]} ")
 
Thrd_Arr = [0, 0]
MakeBothZeros(Thrd_Arr)
print(f"{Thrd_Arr[0]} {Thrd_Arr[1]} ")
 
# This code is contributed by GFGKING


C#
// C# program to set both elements
// to 0 in binary array[2]
using System;
class GFG {
 
  static void MakeBothZeros(int[] arr)
  {
    arr[arr[1]] = arr[arr[0]];
 
    // Two other approaches to solve
    // the problem
    // arr[arr[1]] = 0;
    // arr[1 - arr[0]] = arr[1 - arr[1]];
  }
 
  // Driver code
  public static void Main()
  {
    int[] First_Arr = { 0, 1 };
    MakeBothZeros(First_Arr);
    Console.WriteLine(First_Arr[0] + " "
                      + First_Arr[1]);
 
    int[] Second_Arr = { 1, 0 };
    MakeBothZeros(Second_Arr);
    Console.WriteLine(Second_Arr[0] + " "
                      + Second_Arr[1]);
 
    int[] Thrd_Arr = { 0, 0 };
    MakeBothZeros(Thrd_Arr);
    Console.WriteLine(Thrd_Arr[0] + " " + Thrd_Arr[1]);
  }
}
 
// This code is contributed by ukasp.


Javascript


C++
// C++ program to set both elements
// to 0 in binary array[2].
#include 
using namespace std;
 
void MakeBothZeros(int arr[])
{
    arr[0] = arr[1] = arr[0] & arr[1];
 
    // Two other approaches to solve
    // the problem
    // arr[0] = arr[1] -= arr[1];
    // arr[1] = arr[0] -= arr[0];
}
 
// Driver code
int main()
{
    int First_Arr[] = {0, 1};
    MakeBothZeros(First_Arr);
    cout << First_Arr[0] << " " <<
            First_Arr[1] << endl;
 
    int Second_Arr[] = {1, 0};
    MakeBothZeros(Second_Arr);
    cout << Second_Arr[0] << " " <<
            Second_Arr[1] << endl;
 
    int Thrd_Arr[] = {0, 0};
    MakeBothZeros(Thrd_Arr);
    cout << Thrd_Arr[0] << " " <<
            Thrd_Arr[1] << endl;
 
    return 0;
}


Java
// Java program to set both elements
// to 0 in binary array[2].
 
import java.util.*;
 
class GFG{
 
static void MakeBothZeros(int arr[])
{
    arr[0] = arr[1] = arr[0] & arr[1];
 
    // Two other approaches to solve
    // the problem
    // arr[0] = arr[1] -= arr[1];
    // arr[1] = arr[0] -= arr[0];
}
 
// Driver code
public static void main(String[] args)
{
    int First_Arr[] = {0, 1};
    MakeBothZeros(First_Arr);
    System.out.print(First_Arr[0]+ " " + 
            First_Arr[1] +"\n");
 
    int Second_Arr[] = {1, 0};
    MakeBothZeros(Second_Arr);
    System.out.print(Second_Arr[0]+ " " + 
            Second_Arr[1] +"\n");
 
    int Thrd_Arr[] = {0, 0};
    MakeBothZeros(Thrd_Arr);
    System.out.print(Thrd_Arr[0]+ " " + 
            Thrd_Arr[1] +"\n");
 
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program to set both elements
# to 0 in binary array[2].
def MakeBothZeros(arr):
 
    arr[0] = arr[1] = arr[0] & arr[1]
 
    # Two other approaches to solve
    # the problem
    # arr[0] = arr[1] -= arr[1];
    # arr[1] = arr[0] -= arr[0];
 
# Driver code
First_Arr = [0, 1]
MakeBothZeros(First_Arr)
print(First_Arr[0], end=" ")
print(First_Arr[1])
 
Second_Arr = [0, 1]
MakeBothZeros(Second_Arr)
print(Second_Arr[0], end=" ")
print(Second_Arr[1])
 
 
Thrd_Arr = [0, 0]
MakeBothZeros(Thrd_Arr)
print(Thrd_Arr[0], end=" ")
print(Thrd_Arr[1])
 
# This code is contributed by Samim Hossain Mondal.


C#
// C# program to set both elements
// to 0 in binary array[2].
using System;
public class GFG
{
 
  static void MakeBothZeros(int []arr)
  {
    arr[0] = arr[1] = arr[0] & arr[1];
 
    // Two other approaches to solve
    // the problem
    // arr[0] = arr[1] -= arr[1];
    // arr[1] = arr[0] -= arr[0];
  }
 
  // Driver code
  public static void Main(String[] args) {
    int[] First_Arr = { 0, 1 };
    MakeBothZeros(First_Arr);
    Console.Write(First_Arr[0] + " " + First_Arr[1] + "\n");
 
    int []Second_Arr = { 1, 0 };
    MakeBothZeros(Second_Arr);
    Console.Write(Second_Arr[0] + " " + Second_Arr[1] + "\n");
 
    int []Thrd_Arr = { 0, 0 };
    MakeBothZeros(Thrd_Arr);
    Console.Write(Thrd_Arr[0] + " " + Thrd_Arr[1] + "\n");
 
  }
}
 
// This code is contributed by Rajput-Ji


Javascript


C++
// C++ program to set both elements
// to 0 in binary array[2].
#include 
using namespace std;
 
void MakeBothZeros(int arr[])
{
    arr[!arr[0]] = arr[arr[0]];
 
    // Two other approaches to solve
    // the problem
    // arr[arr[1]] = arr[!arr[1]]
    // arr[!arr[0]] = arr[!arr[1]]
}
 
// Driver code
int main()
{
    int First_Arr[] = {0, 1};
    MakeBothZeros(First_Arr);
    cout << First_Arr[0] << " " <<
            First_Arr[1] << endl;
 
    int Second_Arr[] = {1, 0};
    MakeBothZeros(Second_Arr);
    cout << Second_Arr[0] << " " <<
            Second_Arr[1] << endl;
 
    int Thrd_Arr[] = {0, 0};
    MakeBothZeros(Thrd_Arr);
    cout << Thrd_Arr[0] << " " <<
            Thrd_Arr[1] << endl;
 
    return 0;
}


Javascript



输出
0 0
0 0
0 0

2. 使用赋值运算符两次:

如约束中所列,不允许直接分配。因此 arr[0]=0 和 arr[1]=0 不是有效的语句。赋值运算符将被使用两次以将两个元素都设置为零。

方法:

有三种方法可以实现这一点:

下面是实现该方法的 C++ 程序:

C++

// C++ program to set both elements
// to 0 in binary array[2].
#include 
using namespace std;
 
void MakeBothZeros(int arr[])
{
    arr[0] = arr[1] = arr[0] & arr[1];
 
    // Two other approaches to solve
    // the problem
    // arr[0] = arr[1] -= arr[1];
    // arr[1] = arr[0] -= arr[0];
}
 
// Driver code
int main()
{
    int First_Arr[] = {0, 1};
    MakeBothZeros(First_Arr);
    cout << First_Arr[0] << " " <<
            First_Arr[1] << endl;
 
    int Second_Arr[] = {1, 0};
    MakeBothZeros(Second_Arr);
    cout << Second_Arr[0] << " " <<
            Second_Arr[1] << endl;
 
    int Thrd_Arr[] = {0, 0};
    MakeBothZeros(Thrd_Arr);
    cout << Thrd_Arr[0] << " " <<
            Thrd_Arr[1] << endl;
 
    return 0;
}

Java

// Java program to set both elements
// to 0 in binary array[2].
 
import java.util.*;
 
class GFG{
 
static void MakeBothZeros(int arr[])
{
    arr[0] = arr[1] = arr[0] & arr[1];
 
    // Two other approaches to solve
    // the problem
    // arr[0] = arr[1] -= arr[1];
    // arr[1] = arr[0] -= arr[0];
}
 
// Driver code
public static void main(String[] args)
{
    int First_Arr[] = {0, 1};
    MakeBothZeros(First_Arr);
    System.out.print(First_Arr[0]+ " " + 
            First_Arr[1] +"\n");
 
    int Second_Arr[] = {1, 0};
    MakeBothZeros(Second_Arr);
    System.out.print(Second_Arr[0]+ " " + 
            Second_Arr[1] +"\n");
 
    int Thrd_Arr[] = {0, 0};
    MakeBothZeros(Thrd_Arr);
    System.out.print(Thrd_Arr[0]+ " " + 
            Thrd_Arr[1] +"\n");
 
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python program to set both elements
# to 0 in binary array[2].
def MakeBothZeros(arr):
 
    arr[0] = arr[1] = arr[0] & arr[1]
 
    # Two other approaches to solve
    # the problem
    # arr[0] = arr[1] -= arr[1];
    # arr[1] = arr[0] -= arr[0];
 
# Driver code
First_Arr = [0, 1]
MakeBothZeros(First_Arr)
print(First_Arr[0], end=" ")
print(First_Arr[1])
 
Second_Arr = [0, 1]
MakeBothZeros(Second_Arr)
print(Second_Arr[0], end=" ")
print(Second_Arr[1])
 
 
Thrd_Arr = [0, 0]
MakeBothZeros(Thrd_Arr)
print(Thrd_Arr[0], end=" ")
print(Thrd_Arr[1])
 
# This code is contributed by Samim Hossain Mondal.

C#

// C# program to set both elements
// to 0 in binary array[2].
using System;
public class GFG
{
 
  static void MakeBothZeros(int []arr)
  {
    arr[0] = arr[1] = arr[0] & arr[1];
 
    // Two other approaches to solve
    // the problem
    // arr[0] = arr[1] -= arr[1];
    // arr[1] = arr[0] -= arr[0];
  }
 
  // Driver code
  public static void Main(String[] args) {
    int[] First_Arr = { 0, 1 };
    MakeBothZeros(First_Arr);
    Console.Write(First_Arr[0] + " " + First_Arr[1] + "\n");
 
    int []Second_Arr = { 1, 0 };
    MakeBothZeros(Second_Arr);
    Console.Write(Second_Arr[0] + " " + Second_Arr[1] + "\n");
 
    int []Thrd_Arr = { 0, 0 };
    MakeBothZeros(Thrd_Arr);
    Console.Write(Thrd_Arr[0] + " " + Thrd_Arr[1] + "\n");
 
  }
}
 
// This code is contributed by Rajput-Ji

Javascript



输出
0 0
0 0
0 0

笔记:
时间复杂度为 O(1),因为只使用了一条语句。

3. 通过使用否定 (!)运算符(逻辑非):

在这种方法中,赋值运算符与否定运算符一起使用,在一行代码中使给定数组的两个元素都为 0。

方法:

有三种方法可以做到这一点:

下面是实现该方法的 C++ 程序:

C++

// C++ program to set both elements
// to 0 in binary array[2].
#include 
using namespace std;
 
void MakeBothZeros(int arr[])
{
    arr[!arr[0]] = arr[arr[0]];
 
    // Two other approaches to solve
    // the problem
    // arr[arr[1]] = arr[!arr[1]]
    // arr[!arr[0]] = arr[!arr[1]]
}
 
// Driver code
int main()
{
    int First_Arr[] = {0, 1};
    MakeBothZeros(First_Arr);
    cout << First_Arr[0] << " " <<
            First_Arr[1] << endl;
 
    int Second_Arr[] = {1, 0};
    MakeBothZeros(Second_Arr);
    cout << Second_Arr[0] << " " <<
            Second_Arr[1] << endl;
 
    int Thrd_Arr[] = {0, 0};
    MakeBothZeros(Thrd_Arr);
    cout << Thrd_Arr[0] << " " <<
            Thrd_Arr[1] << endl;
 
    return 0;
}

Javascript



输出
0 0
0 0
0 0