洛谷题ABC

P4414 [COCI2006-2007#2] 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.

大量if语句,暴力算法

#include<iostream>
using namespace std;
int main()
{
    string s;
    int a,b,c;
    cin>>a>>b>>c;
    cin>>s;
    int len=s.length();
        if(s[0]=='A'&&s[1]=='B'&&s[2]=='C')//小,中,大 
        {
            if(a>b&&b>c) cout<<c<<" "<<b<<" "<<a;
            if(a>c&&c>b) cout<<b<<" "<<c<<" "<<a;
            if(b>a&&a>c) cout<<c<<" "<<a<<" "<<b;
            if(b>c&&c>a) cout<<a<<" "<<c<<" "<<b;
            if(c>a&&a>b) cout<<b<<" "<<a<<" "<<c;
            if(c>b&&b>a) cout<<a<<" "<<b<<" "<<c;
        }
        else if(s[0]=='A'&&s[1]=='C'&&s[2]=='B')//小,大,中 
        {
            if(a>b&&b>c) cout<<c<<" "<<a<<" "<<b;
            if(a>c&&c>b) cout<<b<<" "<<a<<" "<<c;
            if(b>a&&a>c) cout<<c<<" "<<b<<" "<<a;
            if(b>c&&c>a) cout<<a<<" "<<b<<" "<<c;
            if(c>a&&a>b) cout<<b<<" "<<c<<" "<<a;
            if(c>b&&b>a) cout<<a<<" "<<c<<" "<<b;
        }
        else if(s[0]=='B'&&s[1]=='A'&&s[2]=='C')//中,小,大 
        {
            if(a>b&&b>c) cout<<b<<" "<<c<<" "<<a;
            if(a>c&&c>b) cout<<c<<" "<<b<<" "<<a;
            if(b>a&&a>c) cout<<a<<" "<<c<<" "<<b;
            if(b>c&&c>a) cout<<c<<" "<<a<<" "<<b;
            if(c>a&&a>b) cout<<a<<" "<<b<<" "<<c;
            if(c>b&&b>a) cout<<b<<" "<<a<<" "<<c;
        }
        else if(s[0]=='B'&&s[1]=='C'&&s[2]=='A') //中,大,小  
        {
            if(a>b&&b>c) cout<<b<<" "<<a<<" "<<c;
            if(a>c&&c>b) cout<<c<<" "<<a<<" "<<b;
            if(b>a&&a>c) cout<<a<<" "<<b<<" "<<c;
            if(b>c&&c>a) cout<<c<<" "<<b<<" "<<a;
            if(c>a&&a>b) cout<<a<<" "<<c<<" "<<b;
            if(c>b&&b>a) cout<<b<<" "<<c<<" "<<a;
        }
        else if(s[0]=='C'&&s[1]=='A'&&s[2]=='B') //大,小,中 
        {
            if(a>b&&b>c) cout<<a<<" "<<c<<" "<<b;
            if(a>c&&c>b) cout<<a<<" "<<b<<" "<<c;
            if(b>a&&a>c) cout<<b<<" "<<c<<" "<<a;
            if(b>c&&c>a) cout<<b<<" "<<a<<" "<<c;
            if(c>a&&a>b) cout<<c<<" "<<b<<" "<<a;
            if(c>b&&b>a) cout<<c<<" "<<a<<" "<<b;
        }
        else if(s[0]=='C'&&s[1]=='B'&&s[2]=='A') //大,中,小 
        {
            if(a>b&&b>c) cout<<a<<" "<<b<<" "<<c;
            if(a>c&&c>b) cout<<a<<" "<<c<<" "<<b;
            if(b>a&&a>c) cout<<b<<" "<<a<<" "<<c;
            if(b>c&&c>a) cout<<b<<" "<<c<<" "<<a;
            if(c>a&&a>b) cout<<c<<" "<<a<<" "<<b;
            if(c>b&&b>a) cout<<c<<" "<<b<<" "<<a;
        }
    return 0;
}

为避免使用大量if语句,可以min和max来进行匹配,赋值a、b、c;
#include<iostream>
using namespace std;
int a,b,c,i,x,y,z,sum;
char ch;
int main()
{
    cin>>x>>y>>z;
    sum=x+y+z;
    a=min(min(x,y),z);
    c=max(max(x,y),z);
    b=sum-a-c;
    for(i=1;i<=3;i++)
    {
        cin>>ch;
        if(ch=='A') cout<<a<<' ';
        if(ch=='B') cout<<b<<' ';
        if(ch=='C') cout<<c<<' ';
    }
    return 0;
}

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

int dat[3];
char ord[3];

int main()
{
    for (int i=0; i<3; i++)
        scanf("%d",&dat[i]);
    for (int i=0; i<3; i++)
        scanf("%c",&ord[i]);
    sort(dat,dat+3);
    for (int i=0; i<3; i++)
        printf("%d ",dat[ord[i]-'A']);
    return 0;
}

#include<iostream>
#include<algorithm>
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'];//字母是大写,减去‘A’后得到0(A),1(B),2(C)。
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a[3];
    for(int i=0;i<3;i++)
    {
        cin>>a[i];
    }
    sort(a,a+3);
    char b[3];
    for(int i=0;i<3;i++)
    {
        cin>>b[i];
        cout<<a[b[i]-'A']<<" ";
    }
    return 0;
}


其实由ASCLL码值知,'A'=65,可以把上式的改为a[b[i]-65]
#include <bits/stdc++.h>
using namespace std;
int p[3];
int main()
{
    int a,b,c;
    cin>>a>>b>>c;
    char i,j,k;
    cin>>i>>j>>k;
    p[0]=min(a,min(b,c));
    p[2]=max(a,max(b,c));
    int x=max(a,b),y=max(a,c),z=max(b,c);
    if(x==y) p[1]=z;
    if(x==z) p[1]=y;
    if(y==z) p[1]=x;
    cout<<p[i-65]<<" "<<p[j-65]<<" "<<p[k-65]<<endl;
    return 0;
}

  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Williamtym

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值