题意:给你一个数字n,1,2,3,4......,n-1,n在一个集合里,每次拿出两个数进行加或者减或者乘三种操作,每次操作结果再放入集合中,问能不能使集合最后剩下的那个数是24!不能输出“NO”,能输出“YES”,并且输出步骤!
分析:题目做着做着越觉得神奇了!!看到这题一头雾水(我还是太年轻了),看了大神的提点,才恍然大悟!!感叹现在人的脑洞真的好大啊!!关键在于凑1!!凑1!!【具体看代码!】
Examples
input
1
output
NO
input
8
output
YES 8 * 7 = 56 6 * 5 = 30 3 - 4 = -1 1 - 2 = -1 30 - -1 = 31 56 - 31 = 25 25 + -1 = 24
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
int n;
while(~scanf("%d",&n))
{
int i;
if(n<4) {
printf("NO\n");continue;
}
printf("YES\n");
if(n%2)
{
printf("5 * 3 = 15\n");
printf("2 * 4 = 8\n");
printf("15 + 8 = 23\n");
printf("23 + 1 = 24\n");
int cnt=0;
for(i=n;i>=7;i-=2)
{
printf("%d - %d = 1\n",i,i-1);cnt++;
}
while(cnt--)
{
printf("24 * 1 = 24\n");
}
}
else
{
printf("2 * 3 = 6\n");
printf("6 * 4 = 24\n");
printf("24 * 1 = 24\n");
int cnt=0;
for(i=n;i>=6;i-=2)
{
printf("%d - %d = 1\n",i,i-1);cnt++;
}
while(cnt--)
{
printf("24 * 1 = 24\n");
}
}
}
return 0;
}