题解:B4210 [常州市赛 2022] 最早对决
可以使用分类讨论的方法。
为了简化代码,不妨设小X编号 <= 小Y编号:
if(a>b){
t=a;
a=b;
b=t;}
| 所有情况如下:(括号内为例子与解释) | 小X编号 | 小Y编号 | 最早相遇轮次 |
|---|---|---|---|
| 1-128 | 33-128 | 1 (第一轮种子遇任意非种子) | |
| 1-16 | 17-32 | 3 (如1号位置遇8号位置) | |
| 17-32 | 17-32 | 4 (如8号位置遇9号位置) | |
| 1-8 | 9-16 | 4 (如1号位置遇16号位置) | |
| 9-16 | 9-16 | 5 (如16号位置遇17号位置) | |
| 1-4 | 5-8 | 5 (以此类推) | |
| 5-8 | 5-8 | 6 | |
| 1-2 | 3-4 | 6 | |
| 3-4 | 3-4 | 7 | |
| 1-2 | 1-2 | 7 |
代码如下:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,t;
cin>>a>>b;
if(a>b){
t=a;
a=b;
b=t;}// 设 a<=b
if(a>32||b>32) cout<<1<<endl;
else if(a<=16&&b>16) cout<<3<<endl;
else if(a>16&&b>16) cout<<4<<endl;
else if(a<=8&&b>8) cout<<4<<endl;
else if(a>8&&b>8) cout<<5<<endl;
else if(a<=4&&b>4) cout<<5<<endl;
else if(a>4&&b>4) cout<<6<<endl;
else if(a<=2&&b>2)cout<<6<<endl;
else if(a>2&&b>2) cout<<7<<endl;
else if(a<=2&&b<=2) cout<<7<<endl;// 进行分类讨论
return 0;
}