题解 | #字符串合并处理#

字符串合并处理

https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static int up(char c) {
        if (c <= 'z' && c >= 'a') {
            return c - 32;
        } else {
            return c;
        }
    }
    public static int er(int x) {
        int sum = 0;
        for (int i = 3; i >= 0; i--) {
            sum += Math.pow(2, i) * (x % 2);
            x /= 2;
        }
        // if (sum == 15) return 'F';
        // else if (sum == 14) return 'E';
        // else if (sum == 13) return 'D';
        // else if (sum == 12) return 'C';
        // else if (sum == 11) return 'B';
        // else  return 'A';
        return sum;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // String str1=in.next();
        // String str2=in.next();
        String str = in.next() + in.next();
        char[] chs = str.toCharArray();

        for (int i = 0; i < chs.length; i += 2) {
            for (int j = 0; j < chs.length-2 ; j+=2) {
                if (chs[j] > chs[j + 2]) {
                    char temp = chs[j];
                    chs[j] = chs[j + 2];
                    chs[j + 2] = temp;
                }
            }
        }
        for (int i = 1; i < chs.length; i += 2) {
            for (int j = 1; j < chs.length - 2; j+=2) {
                if (chs[j] > chs[j + 2]) {
                    char temp = chs[j];
                    chs[j] = chs[j + 2];
                    chs[j + 2] = temp;
                }
            }
        }
        for (int i = 0; i < chs.length; i++) {
            if (up(chs[i]) <= 'F' && up(chs[i]) >= 'A') {
                int x = up(chs[i]) - 'A' + 10;
                switch(er(x)){
                    case 15:System.out.print('F');break;
                    case 14:System.out.print('E');break;
                    case 13:System.out.print('D');break;
                    case 12:System.out.print('C');break;
                    case 11:System.out.print('B');break;
                    case 10:System.out.print('A');break;
                    default:System.out.print(er(x));break;
                }
                
            } else if(chs[i]>='0'&&chs[i]<='9'){
                int sum = 0;
                int x=chs[i]-'0';
                for (int j = 3; j >= 0; j--) {
                    sum += Math.pow(2, j) * (x % 2);
                    x /= 2;
                }
                switch(sum){
                    case 15:System.out.print('F');break;
                    case 14:System.out.print('E');break;
                    case 13:System.out.print('D');break;
                    case 12:System.out.print('C');break;
                    case 11:System.out.print('B');break;
                    case 10:System.out.print('A');break;
                    default:System.out.print(sum);break;
                }
            }else{
                System.out.print(chs[i]);
            }
        }
    }
}

全部评论

相关推荐

再懒也要睡懒觉:大学4年玩的挺爽的哈😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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