codeforces 468 A. 24 Game (构造)

题目链接

题意:给出n,有1..n n个数,可以选择两个数进行加,减,乘,三种操作,操做完得到一个数放回。 n-1次操作后只剩下一个数。现在要求剩下的数为24.问方法。

思路:我们发现。。。两个数相减可以为1.。那么只要找到4个数的方案和5个数的方案就好了。。。

4个数:123*4

5个数:4*5+3+2-1

然而窝一开始以为必须前面减后面。。。

所以是按照4K,4K+1,4K+2,4K+3分的类。。。每4个数得到两个-1,再相乘。。。。麻烦了一点。。代码写了一半的时候意识到了按2K,2K+1分类就行。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年10月03日 星期一 00时50分18秒
 4File Name :code/cf/problem/468A.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <deque>
15#include <map>
16#include <string>
17#include <cmath>
18#include <cstdlib>
19#include <bitset>
20#define fst first
21#define sec second
22#define lson l,m,rt<<1
23#define rson m+1,r,rt<<1|1
24#define ms(a,x) memset(a,x,sizeof(a))
25typedef long long LL;
26#define pi pair < int ,int >
27#define MP make_pair
28
29using namespace std;
30const double eps = 1E-8;
31const int dx4[4]={1,0,0,-1};
32const int dy4[4]={0,-1,1,0};
33const int inf = 0x3f3f3f3f;
34int n;
35int main()
36{
37	#ifndef  ONLINE_JUDGE 
38    //freopen("code/in.txt","r",stdin);
39  #endif
40
41	cin>>n;
42	if (n<4)
43	{
44	    puts("NO");
45	    return 0;
46	}
47	puts("YES");
48	int m = n % 4;
49	if (m==0)
50	{
51	    puts("1 * 2 = 2");
52	    puts("3 * 4 = 12");
53	    puts("2 * 12 = 24");
54	}else if (m==1)
55	{
56	    puts("4 * 5 = 20");
57	    puts("3 + 2 = 5");
58	    puts("5 - 1 = 4");
59	    puts("20 + 4 = 24");
60	}else if (m==2)
61	{
62	    puts("1 + 2 = 3");
63	    puts("3 + 3 = 6");
64	    puts("6 - 5 = 1");
65	    puts("4 * 6 = 24");
66	    puts("1 * 24 = 24");
67	}else
68	{
69	    puts("7 - 4 = 3");
70	    puts("6 - 5 = 1");
71	    puts("3 + 1 = 4");
72	    puts("4 * 3 = 12");
73	    puts("12 * 2 = 24");
74	    puts("24 * 1 = 24");
75	}
76	int cnt = n/4-1;
77	int cur = m+4;
78	for ( int i = 1 ; i <= cnt ; i++)
79	{
80	    printf("%d - %d = %d\n",cur+1,cur+2,-1);
81	    cur+=2;
82	    printf("%d - %d = %d\n",cur+1,cur+2,-1);
83	    cur+=2;
84	    puts("-1 * -1 = 1");
85	}
86	for ( int i = 1 ; i <= cnt-1 ; i++)
87	    puts("1 * 1 = 1");
88	if (cnt>0)
89	puts("24 * 1 = 24");
90
91
92  #ifndef ONLINE_JUDGE  
93  fclose(stdin);
94  #endif
95    return 0;
96}