首页 > 中兴笔试 进制转换问题
头像
剑斩Offer
编辑于 2020-08-24 12:16
+ 关注

中兴笔试 进制转换问题

import java.util.Scanner;

public class Test2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String[] str = new String[n];
        int[] l = new int[n];
        int[] r = new int[n];
        for(int i = 0;i < n;i++){
            str[i] = sc.next();
            l[i] = sc.nextInt();
            r[i] = sc.nextInt();
        }
        for(int i = 0; i < n;i++){
            int count = 0;
            for(int j = l[i];j <= r[i];j++){
                count += solution(str[i],j);
            }
            System.out.println(count%2);
        }

    }
    public static int solution(String str,int index){
        // 如果为偶进制,直接看末尾
        if(index % 2 == 0){
            char c = str.charAt(str.length()-1);
            if(c - '0' >= 0 && c -'0' <= 9){
                return (c -'0') % 2 == 0 ? 0 : 1;
            }
            if(c - 'A' >= 0 && c -'A' <= 25){
                return (c - 'A') % 2 == 0 ? 0 : 1;
            }
        }
        // 如果为奇进制,看字符串中奇数个数
        else{
            int count = 0;
            for(int i = 0 ; i < str.length(); i++){
                char c = str.charAt(i);
                if(c - '0' >= 0 && c -'0' <= 9){
                    count += (c -'0') % 2 == 0 ? 0 : 1;
                }
                else if(c - 'A' >= 0 && c -'A' <= 25){
                    count +=(c - 'A') % 2 == 0 ? 0 : 1;
                }
            }
            return count%2;
        }
        return -1;
    }
}
大佬们看下思路有什么问题,为什么一直0ac

全部评论

(7) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

热门推荐