京东第一题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

相关推荐

昨天 18:19
已编辑
长江大学 Java
点赞 评论 收藏
分享
04-30 21:35
已编辑
长安大学 C++
晓沐咕咕咕:评论区没被女朋友好好对待过的计小将可真多。觉得可惜可以理解,毕竟一线大厂sp。但是骂楼主糊涂的大可不必,说什么会被社会毒打更是丢人。女朋友体制内生活有保障,读研女朋友还供着,都准备订婚了人家两情相悦,二线本地以后两口子日子美滋滋,哪轮到你一个一线城市房子都买不起的996清高计小将在这说人家傻😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务