C++洛谷题解(29)——P4414

题名:ABC

题目(英文):You will be given three integers A, B and C. The numbers will not be given in that exact order, but we do know that A is less than B and B less than C. In order to make for a more pleasant viewing, we want to rearrange them in the given order.

输入(英文):The first line contains three positive integers A, B and C, not necessarily in that order. All three numbers will be less than or equal to 100. The second line contains three uppercase letters 'A', 'B' and 'C' (with no spaces between them) representing the desired order.

输出(英文):Output the A, B and C in the desired order on a single line, separated by single spaces.


题目:三个整数分别为 A,B,C。这三个数字不会按照这样的顺序给你,但它们始终满足条件:A < B < C。为了看起来更加简洁明了,我们希望你可以按照给定的顺序重新排列它们。

输入:第一行包含三个正整数 A,B,C,不一定是按这个顺序。这三个数字都小于或等于 100。第二行包含三个大写字母 A、B 和 C(它们之间没有空格)表示所需的顺序。

输出:在一行中输出 A,B 和 C,用一个  (空格)隔开。

感谢 @smartzzh 提供的翻译

这是目前为止最奇葩的一道题,居然是歪果仁的题。而且这道题居然不是CCF_NOI,而是克罗地亚信息学比赛COCI比赛。所以我们这一期可以会会COCI的题了,还有点小激动。

言归正传。这道题其实全难在题目上了,小学六年级的一篇作文才要求500字,它光一个题目就800字。而读懂了这道题的题目后就轻松多了,瞬间从史诗难题变成了家常小菜。直接写思路吧:

  1. 先声明一个数组和一个变量,分别表示三个数和字符串A,B,C
  2. 直接用cin输入数组
  3. sort函数从小到大排序
  4. 直接cout输出

你没有看错,就是这么简单。但是真正难的地方是不起眼的cout输出部分,因为这里要用到ASCII值。连sort函数都天天用,难道还怕ASCII值吗?所以,直接上代码:

#include<bits/stdc++.h>//万能头
using namespace std;
int a[3];//声明数组
char A,B,C;//声明字符串
int main(){//程序开始
    cin>>a[0]>>a[1]>>a[2];//输入三个数
    cin>>A>>B>>C;//输入ABC的顺序
    sort(a,a+3);//从小到大排个序
    cout<<a[A-'A']<<" "<<a[B-'A']<<" "<<a[C-'A'];//cout输出
    //a[A-'A']用当前字符减去'A'就可以了
    return 0;//完美的结束
}

复制版(挑战全网最短代码):

#include<bits/stdc++.h>
using namespace std;
int a[3];
char A,B,C;
int main(){
    cin>>a[0]>>a[1]>>a[2];
    cin>>A>>B>>C;
    sort(a,a+3);
    cout<<a[A-'A']<<" "<<a[B-'A']<<" "<<a[C-'A'];
}

至此,洛谷第二篇题单我们也刷完了!

posted @ 2022-07-19 19:47  GitTJBKBeta  阅读(84)  评论(0编辑  收藏  举报