京东第一题0%怎么回事,求解答

import java.util.*;

public class Main{
    public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
    	while(sc.hasNext()){
    		String str = sc.nextLine();
    		int n = Integer.valueOf(str);
    		String a1 = sc.nextLine();
    		String a2 = sc.nextLine();
    		String re = "";
    		for(int i=0; i<n; i++){
    			char c1 = a1.charAt(i);
    			char c2 = a2.charAt(i);
    			if(c1=='0'&& c2=='1'){
    				re = re+"1";
    			}else if(c1=='1'&& c2=='0'){
    				re = re+"1";
    			}else{
    				re = re+"0";
    			}
    		}
    		System.out.println(re);
    		while(re.charAt(0)=='0'){
    			re = re.substring(1);
    		}
    		int r=0;
    		int temp=1;
    		for(int i=re.length()-1; i>=0; i--){
    			if(re.charAt(i)=='1'){
    				r += temp;
    			}
    			temp = temp*2;
    		}
    		System.out.println(r);
    	}
    }
}

全部评论
要不要做得这么麻烦…
点赞 回复 分享
发布于 2017-04-07 23:53
int xorString( int n, string a, string b) {     int res = 0 ;     int idx;     for (idx= 0 ;idx<n;++idx){         res*= 2 ;         if (a[ idx ]!=b[ idx ])             res+= 1 ;     }     return res; }
点赞 回复 分享
发布于 2017-04-07 21:07

相关推荐

05-25 10:45
西华大学 Java
Frank_zhang:没实习一个项目肯定不够,可以再做一个轮子,技术栈再补一个mq,微服务,整体再换个简历模板,暑期尽量再找一个日常实习
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务