题解:UVA12992 Huatuo's Medicine

· · 题解

题意

华佗去病人家看病,当他需要携带两种药物时,例如药物 AB,他将把 A 放在一个瓶子里,把 B 放在两个瓶子里。然后他会按 -B-A-B- 的顺序把瓶子锁起来。

要求出华佗带 n 种药最少需要多少瓶子。

分析

我们模拟一下,带 3 种药是这样的 -C-B-A-B-C- 需要 5 个瓶子,带 4 种药是这样的 -D-C-B-A-B-C-D- 需要 7 个瓶子。

不难看出,ans=(n-1)\times2+1=n\times2-1

有了等式,代码就简单了,上代码。

代码

#include<bits/stdc++.h>
using namespace std;
int T,n;
int main(){
    cin>>T;
    for(int i=1;i<=T;i++){
        cin>>n;
        cout<<"Case #"<<i<<":"<<' '<<n*2-1<<endl;
    }
}