codeforces a 24 game

http://codeforces.com/contest/468/problem/A

其实一直不明白为啥程序员非常喜欢考智力题,可能算法=智力+数学?不过Codeforces的确挺好  代码短,挺像面试题什么的......

题目: n-1次操作凑出24  数不可以重复使用,操作可以是a-b a+b a*b

这个因为是DIV1的题,后来才发现可做

感觉有两种思路吧,凑0或者凑1

然后打印一堆24+0=24或者24*1=24

我凑一了

//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std;

#define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdout)
const ll ll_INF = ((ull)(-1))>>1;
const double EPS = 1e-8;
const double pi = acos(-1.0);
const int INF = 100000000;

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        if(n<4){puts("NO");continue;}
        printf("YES\n");
        if(n&1)
        {
            printf("5 * 3 = 15\n");
            printf("4 * 2 = 8\n");
            printf("15 + 8 = 23\n");
            printf("23 + 1 = 24\n");
            for(int i=7;i<=n;i+=2)
            {
                printf("%d - %d = 1\n",i,i-1);
                printf("24 * 1 = 24\n");
            }
        }
        else
        {
            printf("4 * 3 = 12\n");
            printf("12 * 2 = 24\n");
            printf("24 * 1 = 24\n");
            for(int i=6;i<=n;i+=2)
            {
                printf("%d - %d = 1\n",i,i-1);
                printf("24 * 1 = 24\n");
            }
        }
    }
    return 0;
}

另一种凑0的思路 摘自http://blog.youkuaiyun.com/u011394362/article/details/39476329

#include <iostream>  
using namespace std;  
const int N = 1e3 + 5;  
  
int main()  
{  
    int n;  
    cin >> n;  
    if(n <= 3){  
        cout << "NO" << endl;  
    }  
    else if(n == 4){  
        cout << "YES" << endl;  
        cout << "2 * 3 = 6" << endl;  
        cout << "4 * 6 = 24" << endl;  
        cout << "1 * 24 = 24" << endl;  
    }  
    else if(n == 5){  
        cout << "YES" << endl;  
        cout << "5 - 2 = 3" << endl;  
        cout << "3 + 3 = 6" << endl;  
        cout << "4 * 6 = 24" << endl;  
        cout << "24 * 1 = 24" << endl;  
    }  
    else {  
        cout << "YES" << endl;  
        cout << "6 - 5 = 1" << endl;  
        cout << "1 - 1 = 0" << endl;  
        for(int i = 7; i <= n; i ++){  
            cout << i << " * 0 = 0" << endl;  
        }  
        cout << "2 * 3 = 6" << endl;  
        cout << "6 * 4 = 24" << endl;  
        cout << "24 + 0 = 24" << endl;  
    }  
    return 0;  
}  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值