热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

与给定数组异或和为给定数k的数

与给定数组异或和为给定数 k 的数原文:https://www . geesforgeks . org/number-what-

与给定数组异或和为给定数 k 的数

原文:https://www . geesforgeks . org/number-what-xor-sum-given-array-given-number-k/

给定一个由 N 个数字和 k 个数字组成的数组。任务是在给定的数组中插入一个数字,使新数组中所有元素的按位异或等于给定的输入 k。
示例:

Input:
a = {1, 2, 3, 4, 5}, k = 10
Output: 11
Explanation: 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 11 = 10
Input: A[] = { 12, 23, 34, 56, 78 }, k = 6
Output: 73

方法:基本思想是使用简单的 XOR 属性,即如果 X ^ Y = Z,那么 X ^ Z = Y。让我们假设要插入数组中的数字是 x,使得(a[0]^ a[1]^…^ a[n–1])^ x = k。因此,要找到 x,我们可以使用关系(a[0]^ a[1]^…a[n–1])k = x。下面的
是上述方法的实现。

C++

// CPP Program to find the number
// whose XOR sum with given array is
// equal to a given number k
#include
using namespace std;
// This function returns the number to
// be inserted in the given array
int findEletobeInserted(int A[], int n, int k)
{
    // initialise the answer with k
    int ans = k;
    for (int i = 0; i         ans ^= A[i]; // XOR of all elements in the array
    return ans;
}
// Driver Code to test above function
int main()
{
    int A[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(A) / sizeof(A[0]);
    int k = 10;
    cout <         <<" has to be inserted"
         " in the given array to make xor sum of "
         <    return 0;
}

Java 语言(一种计算机语言,尤用于创建网站)

// Java Program to find the number
// whose XOR sum with given array is
// equal to a given number k
import java.io.*;
class GFG {
    // This function returns the number to
    // be inserted in the given array
    static int findEletobeInserted(int A[],
                              int n, int k)
    {
        // initialise the answer with k
        int ans = k;
        for (int i = 0; i             // XOR of all elements in
            // the array
            ans ^= A[i];
        return ans;
    }
    // Driver Code to test above function
    public static void main (String[] args)
    {
        int A[] = { 1, 2, 3, 4, 5 };
        int n =A.length;
        int k = 10;
        System.out.println(
             findEletobeInserted(A, n, k)
              + " has to be inserted in "
              + "the given array to make"
                   + " xor sum of " + k);
    }
}
// This code is contributed by anuj_67.

Python 3

# Python 3 Program to find the number
# whose XOR sum with given array is
# equal to a given number k
# This function returns the number to
# be inserted in the given array
def findEletobeInserted(A, n, k):
    # initialise the answer with k
    ans = k
    for i in range(n):
        ans ^= A[i] # XOR of all elements
                    # in the array
    return ans
# Driver Code
if __name__ == '__main__':
    A = [1, 2, 3, 4, 5]
    n = len(A)
    k = 10
    print(findEletobeInserted(A, n, k),
          "has to be inserted in the given",
          "array to make xor sum of", k)
# This code is contributed by
# Surendra_Gangwar

C

// C# Program to find the number
// whose XOR sum with given array is
// equal to a given number k
using System ;
class GFG {
    // This function returns the number to
    // be inserted in the given array
    static int findEletobeInserted(int []A,
                            int n, int k)
    {
        // initialise the answer with k
        int ans = k;
        for (int i = 0; i             // XOR of all elements in
            // the array
            ans ^= A[i];
        return ans;
    }
    // Driver Code to test above function
    public static void Main ()
    {
        int []A = { 1, 2, 3, 4, 5 };
        int n =A.Length;
        int k = 10;
        Console.WriteLine(
            findEletobeInserted(A, n, k)
            + " has to be inserted in "
            + "the given array to make"
                + " xor sum of " + k);
    }
}
// This code is contributed by anuj_67.

服务器端编程语言(Professional Hypertext Preprocessor 的缩写)

// PHP Program to find the number
// whose XOR sum with given array is
// equal to a given number k
// This function returns the number to
// be inserted in the given array
function findEletobeInserted($A, $n, $k)
{
    // initialise the answer with k
    $ans = $k;
    for ( $i = 0; $i <$n; $i++)
        // XOR of all elements
        // in the array
        $ans ^= $A[$i];
    return $ans;
}
    // Driver Code
    $A = array(1, 2, 3, 4, 5);
    $n = count($A);
    $k = 10;
    echo findEletobeInserted($A, $n, $k) ;
    echo " has to be inserted";
    echo " in the given array to make xor sum of ";
    echo $k , "\n";
// This code is contributed by anuj_67.
?>

java 描述语言


输出:

11 has to be inserted in the given array to make xor sum of 10

推荐阅读
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • Python如何调用类里面的方法
    本文介绍了在Python中调用同一个类中的方法需要加上self参数,并且规范写法要求每个函数的第一个参数都为self。同时还介绍了如何调用另一个类中的方法。详细内容请阅读剩余部分。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Python语法上的区别及注意事项
    本文介绍了Python2x和Python3x在语法上的区别,包括print语句的变化、除法运算结果的不同、raw_input函数的替代、class写法的变化等。同时还介绍了Python脚本的解释程序的指定方法,以及在不同版本的Python中如何执行脚本。对于想要学习Python的人来说,本文提供了一些注意事项和技巧。 ... [详细]
  • 安卓select模态框样式改变_微软Office风格的多端(Web、安卓、iOS)组件库——Fabric UI...
    介绍FabricUI是微软开源的一套Office风格的多端组件库,共有三套针对性的组件,分别适用于web、android以及iOS,Fab ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了RPC框架Thrift的安装环境变量配置与第一个实例,讲解了RPC的概念以及如何解决跨语言、c++客户端、web服务端、远程调用等需求。Thrift开发方便上手快,性能和稳定性也不错,适合初学者学习和使用。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
author-avatar
迷人的哈喽柯柯_458
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有