zoukankan      html  css  js  c++  java
  • 数据异或操作

    给你两个整数,n 和 start 。

    数组 nums 定义为:nums[i] = start + 2*i(下标从 0 开始)且 n == nums.length 。

    请返回 nums 中所有元素按位异或(XOR)后得到的结果。

    示例1:

    输入:n = 5, start = 0
    输出:8
    解释:数组 nums 为 [0, 2, 4, 6, 8],其中 (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8"^" 为按位异或 XOR 运算符。

    示例2:

    输入:n = 4, start = 3
    输出:8
    解释:数组 nums 为 [3, 5, 7, 9],其中 (3 ^ 5 ^ 7 ^ 9) = 8.

    参考代码:

    package com.gong;
    
    public class xorOperation {
        public static int xor (int n,int start){
            int[] nums=new int[n];
            int res=0;
            for (int i=0;i<nums.length;i++){
                nums[i]=start+2*i;
                res=res^nums[i];
            }
            //打印数组的值
            System.out.println("数组的值为");
            for (int j=0;j<nums.length;j++) {
                System.out.print(nums[j]+"  ");
            }
            return res;
        }
    
        public static void main(String[] args) {
            int n=7;
            int start=4;
            int result=0;
            result=xor(n,start);
            System.out.println();
            System.out.println("result的值为"+result);
        }
    }
  • 相关阅读:
    Spring学习笔记(8)——依赖注入
    JS中Ajax的实现部分
    Hibernate4之注解零配置
    SQL中的DDL、DML、DCL、TCL
    冷启动与热启动
    双击退出
    显示或者隐式
    Android笔记01--手机振动
    github与pycharm
    正则re模块--入门
  • 原文地址:https://www.cnblogs.com/braveym/p/14750103.html
Copyright © 2011-2022 走看看