3650. 转机折扣

一、题目

Problem #3650 - ECNU Online Judge

3650. 转机折扣_第1张图片3650. 转机折扣_第2张图片

二、思路

①先找出较小的字符串

②对该字符串最末尾字符加1

③从右往左对该字符串进行遍历,与'Z‘比较,如果大于’Z‘,前一个字符加1,否则,该字符赋值为'A’

三、代码

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String s1 = sc.nextLine();
            String s2 = sc.nextLine();
            if (s1.compareTo(s2) > 0) {
                print(s2);
            } else {
                print(s1);
            }
        }
    }

    public static void print(String s) {
        char res[] = s.toCharArray();
        res[s.length() - 1] += 1;
        for (int i = s.length() - 1; i >= 0; i--) {
            int temp = 0;
            if (res[i] > 'Z') temp = res[i] - 'Z';
            if (temp > 0) {
                res[i] = 'A' ;
                res[i - 1] += 1;
            }
        }
        System.out.println(new String(res));
    }
}

你可能感兴趣的:(题解,java,算法,题解)