题目描述
三个整数分别为 A,B,CA,B,C。这三个数字不会按照这样的顺序给你,但它们始终满足条件:A < B < CA<B<C。为了看起来更加简洁明了,我们希望你可以按照给定的顺序重新排列它们。
输入格式
第一行包含三个正整数 A,B,CA,B,C,不一定是按这个顺序。这三个数字都小于或等于 100100。第二行包含三个大写字母 AA、BB 和 CC(它们之间没有空格)表示所需的顺序。
输出格式
在一行中输出 AA,BB 和 CC,用一个 (空格)隔开。
输入输出样例
输入#1 输出#1
1 5 3 1 3 5
ABC
输入#2 输出#2
6 4 2 6 2 4
CAB
代码(Java)
import java.util.Scanner;
//https://www.luogu.com.cn/problem/P4414
public class P4414 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] num = new int[3];
char[] xu = new char[3];
String str;
for(int i=0;i<3;i++) {
num[i] = sc.nextInt();
}
str = sc.next();
for(int i=0;i<3;i++) { //循环将字符串转换成字符
//xu[i] = sc.next().charAt(0); //获取输入字符的方法
xu[i] = str.charAt(i);
}
for(int i=0;i<3;i++) {
for(int j=i;j<3;j++) {
if(num[i]>num[j]) {
int temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
int a=num[0],b=num[1],c=num[2];
for(int i=0;i<3;i++) {
if(xu[i] == 'A') {
num[i] = a;
}
if(xu[i] == 'B') {
num[i] = b;
}
if(xu[i] == 'C') {
num[i] = c;
}
}
for(int i=0;i<3;i++) {
if(i!=2) {
System.out.print(num[i]+" ");
}else {
System.out.print(num[i]);
}
}
sc.close();
}
}
其中需要注意的就是输入字符的方法和将字符串转换成字符的方法
输入字符的方法
char ch = sc.next().charAt(0);
将字符串转成字符的方法
String str = "hello";
char[] ch = new char[n];
for(int i=0;i<3;i++) { //循环将字符串转换成字符
ch[i] = str.charAt(i);
}
還可以直接使用str.toCharArray()方法,詳見:https://blog.csdn.net/weixin_46353366/article/details/123343560