51信用卡春招在线考试

  2018年5月7日19:00–21:00,参与了51信用卡春招在线考试,题型为20道单项选择题、10道不定项选择题、两道编程题、一道问答题。现将编程题记录如下:

字符串AB

题目描述
  有两个都是大写的字符串 A 和 B ,判断 A 中是否包含 B 中所有的字符。
  注意:字符串 B 里的字符在 A 中不需要连续或有序。

输入示例 1

ABCU51NB;NB51

输出示例 1

true

输入示例 2

ACGBEF;DB

输出示例 2

false

时间限制:C/C++语言 1 秒;其他语言 2 秒
空间限制:C/C++语言 32768 K;其他语言 65536 K

// 本题AC
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        boolean flag = true;
        int index = str.indexOf(";");
        StringBuffer A = new StringBuffer(str.substring(0, index));
        StringBuffer B = new StringBuffer(str.substring(index + 1, str.length()));
        if (A.length() < B.length()) {
            System.out.println(false);
            return;
        }

        for (int i = 0; i < B.length(); i++) {
            String substr = B.substring(i, i + 1);
            int indexOfA = A.indexOf(substr);
            if (indexOfA < 0 || indexOfA > A.length() - 1) {
                flag = false;
                break;
            } else {
                A.deleteCharAt(indexOfA);
            }
        }
        System.out.println(flag);
    }
}

红绿蓝

题目描述
  我们使用 R、G、B 分别表示红、绿、蓝三种颜色,给定一个包含这三个颜色的长度为 n 的数组,现在要求对数组进行分组,使相同颜色的相邻,并按照红、绿、蓝的顺序进行排序。
  注意:排序需要在原数组中进行。

输入示例 1

RGRB

输出示例 1

RRGB

时间限制:C/C++语言 1 秒;其他语言 2 秒
空间限制:C/C++语言 32768 K;其他语言 65536 K

扫描二维码关注公众号,回复: 1117118 查看本文章
// 本题AC
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        StringBuffer color = new StringBuffer(str);
        int loc = 0;

        for (int i = 0; i < color.length(); i++) {
            String substr = color.substring(i, i + 1);
            if (substr.equals("R")) {
                String tempR = color.substring(loc, loc + 1);
                color.replace(loc, loc + 1, "R");
                color.replace(i, i + 1, tempR);
                loc++;
            }
        }

        loc = color.length() - 1;
        for (int j = color.length() - 1; j >= 0; j--) {
            String substr = color.substring(j, j + 1);
            if (substr.equals("B")) {
                String tempB = color.substring(loc, loc + 1);
                color.replace(loc, loc + 1, "B");
                color.replace(j, j + 1, tempB);
                loc--;
            }
        }

        System.out.println(color);
    }
}

猜你喜欢

转载自blog.csdn.net/u012102104/article/details/80231249