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

相关推荐

陆续:不可思议 竟然没那就话 那就我来吧 :你是我在牛客见到的最美的女孩
点赞 评论 收藏
分享
07-17 12:09
门头沟学院 Java
讲的口干舌燥,头都晕了怎么要讲这么长啊
码农索隆:没事,你口干舌燥,他不一定会看,
投递小鹏汽车等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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