Codeforces Round #268 (Div. 2)
题意:初始你手中有n张牌,数字分别是1~n,然后你任选其中两张牌,用“+”,“-”,“*”,来组合出另一张牌(另一个数字),并把使用过的牌扔掉,问最后能否剩下的最后一张牌为24?如果不能,输出NO;否则,输出YES,并将如何得到24的方法打印出来。
构造,当n<4时,不可能存在,
n==4时存在1*2*3*4=24,
当n==5时,存在5-1=4,4-2=1,现在剩下2*3*4=24,
n为6时,1*2*3*4=24,6-5=1,1*24=24,n>5时n为偶数时,n-(n-1)=1,将大于4的全部转换成1,
n>5时n为奇数时,可以转化成5
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
if(n<4) cout<<"NO\n";
else {
cout<<"YES\n";
if(n%2==0){
cout<<"1 * 2 = 2\n";
cout<<"2 * 3 = 6\n";
cout<<"6 * 4 = 24\n";
for(int i=5;i<=n;i+=2){
cout<<i+1<<" - "<<i<<" = 1\n";
cout<<"1 * 24 = 24\n";
}
}
else{
cout<<"4 - 2 = 2\n";
cout<<"5 - 1 = 4\n";
cout<<"2 * 3 = 6\n";
cout<<"6 * 4 = 24\n";
for(int i=6;i<=n;i+=2){
cout<<i+1<<" - "<<i<<" = 1\n";
cout<<"1 * 24 = 24\n";
}
}
}
return 0;
}