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;
}