题意:给出n,有1..n n个数,可以选择两个数进行加,减,乘,三种操作,操做完得到一个数放回。 n-1次操作后只剩下一个数。现在要求剩下的数为24.问方法。
思路:我们发现。。。两个数相减可以为1.。那么只要找到4个数的方案和5个数的方案就好了。。。
4个数:1*2*3*4
5个数:4*5+3+2-1
然而窝一开始以为必须前面减后面。。。
所以是按照4K,4K+1,4K+2,4K+3分的类。。。每4个数得到两个-1,再相乘。。。。麻烦了一点。。代码写了一半的时候意识到了按2K,2K+1分类就行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
/* *********************************************** Author :111qqz Created Time :2016年10月03日 星期一 00时50分18秒 File Name :code/cf/problem/468A.cpp ************************************************ */ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <deque> #include <map> #include <string> #include <cmath> #include <cstdlib> #include <bitset> #define fst first #define sec second #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define ms(a,x) memset(a,x,sizeof(a)) typedef long long LL; #define pi pair < int ,int > #define MP make_pair using namespace std; const double eps = 1E-8; const int dx4[4]={1,0,0,-1}; const int dy4[4]={0,-1,1,0}; const int inf = 0x3f3f3f3f; int n; int main() { #ifndef ONLINE_JUDGE //freopen("code/in.txt","r",stdin); #endif cin>>n; if (n<4) { puts("NO"); return 0; } puts("YES"); int m = n % 4; if (m==0) { puts("1 * 2 = 2"); puts("3 * 4 = 12"); puts("2 * 12 = 24"); }else if (m==1) { puts("4 * 5 = 20"); puts("3 + 2 = 5"); puts("5 - 1 = 4"); puts("20 + 4 = 24"); }else if (m==2) { puts("1 + 2 = 3"); puts("3 + 3 = 6"); puts("6 - 5 = 1"); puts("4 * 6 = 24"); puts("1 * 24 = 24"); }else { puts("7 - 4 = 3"); puts("6 - 5 = 1"); puts("3 + 1 = 4"); puts("4 * 3 = 12"); puts("12 * 2 = 24"); puts("24 * 1 = 24"); } int cnt = n/4-1; int cur = m+4; for ( int i = 1 ; i <= cnt ; i++) { printf("%d - %d = %d\n",cur+1,cur+2,-1); cur+=2; printf("%d - %d = %d\n",cur+1,cur+2,-1); cur+=2; puts("-1 * -1 = 1"); } for ( int i = 1 ; i <= cnt-1 ; i++) puts("1 * 1 = 1"); if (cnt>0) puts("24 * 1 = 24"); #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; } |
说点什么
您将是第一位评论人!