今日头条笔试

/*
 * 给定x , k(0 < x , k < 2000000000)
 * 求是的x+y = x|y的第k大数
 * 要使得x+y=x|y
 * x的二进制数其中为1的位置y一定为0,为0的位置可以选择为0还是为1
 * 对于第k个数只需要将k的二进制数从小到大放大x不为1的位置
 */

import java.util.Scanner;

public class Temp {
	public static final int maxn = 100 ;
	private static int[] a = new int[maxn];
	private static boolean[] vis = new boolean[maxn];
	private static int[] b = new int[maxn]; 
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in) ; 
		long n , m ; 
		while(in.hasNextLong()){
			n = in.nextLong() ; 
			m = in.nextLong() ; 
			long temp = n ; 
			int pos = 0 ; 
			while(temp != 0){
				a [pos++] = (int) (temp%2) ; 
				temp/=2 ; 
			}
			for(int i = 0;i<pos;i++){
				b [i] = 0 ; 
				vis[i] = false ; 
			}
			for(int i = 0;i < pos;i++){
				if(a[i] == 1){
					vis[i] = true ; 
				}
			}
			int len = pos ; 
			temp = m ; 
			pos = 0 ;
			while(temp != 0){
				int res = (int) (temp%2) ; 
				for(int i = 0;;i++){
					if(vis[i])continue ;
					vis[i] = true ; 
					b[i] = res ;
					if(i >= len)len++; 
					break ; 
				}
				temp /= 2 ; 
			}
			long ans = 0 ; 
			for(int i = len-1;i >= 0;i--){
//				1System.out.println(ans);
				ans*=2 ; 
				ans += b[i] ; 
			}
			System.out.println(ans);
		}
		in.close();
	}
}

你可能感兴趣的:(今日头条笔试)