“山大地纬杯”第十二届山东省ICPC大学生程序设计竞赛(正式赛)A Seventeen

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

题目描述

Seventeen is a game where players try to construct an equation that evaluates to 17 using operators +-* and parentheses (,) and using each of the given numbers exactly once.

Construct an equation using integers from 111 to nnn exactly once with the value of 17.

输入描述:

The input contains one integer nnn (1≤n≤501 \le n \le 501≤n≤50).

输出描述:

If there is no solution, output −1.
Otherwise, output an expression consisting of +, -, *, (, and ). The length of the expression should not exceed 1000 characters.

示例1

输入

复制10

10

输出

复制1+2+3+4+5+6+7+8-9-10

1+2+3+4+5+6+7+8-9-10

说明

 

The following expression are considered right, too.

-10+1+2+3+4+5+6+7+8-9

((-10+1))+2+3+4+5+6+7+8-9

(-10+1)+2+3+4+5+6+7+8-9

题目意思是给出n,你要使用1到n之中的数字,并用+ , - , *,(,)符号讲他们连接起来,使他们最后的答案等于17.

这一题有一个思路是我的队友写出来的,他使用的是dfs穷举的思想,将n 大于6的情况穷举出答案等于17的式子。

还有一个是8以后的数字x都可以由x - 4得到,比如,4 是(1 + 4) * 3 + 2,8就可以是(1 + 4)*3 + 2 +5 - 6 - 7 + 8,最后还是17,得到转移方程:s[x] = s[x - 4] + (x - 3) - (x - 2) - (x - 1) + x;

代码:

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
#define ll long long
const int N = 1e3 + 10;

string s[N];
int main()
{
    ll n;
    cin >> n;
    s[4] = "(1+4)*3+2";
    s[5] = "1*2*5+3+4";
    s[6] = "1-2+3+4+5+6";
    s[7] = "-3+4+5+6+7-1*2";
    if (n <= 3)
    {
        cout << -1 << '\n';
    }
    else if (n == 4)
    {
        cout << s[4] << '\n';
    }
    else if (n == 5)
    {
        cout << s[5] << '\n';
    }
    else if (n == 6)
    {
        cout << s[6] << '\n';
    }
    else if (n == 7)
    {
        cout << s[7] << '\n';
    }
    else
    {
        ll m = 8;
        while (m <= n)
        {
            s[m] = s[m - 4];
            s[m] += '+';
            if(m - 3 <= 9)s[m] += m - 3 + '0';
            else
            {
                string p = "";
                ll xx = m - 3;
                while (xx)
                {
                    ll x = xx % 10;
                    p += x + '0';
                    xx /= 10;
                }
                reverse(p.begin(), p.end());
                s[m] += p;
            }
            s[m] += '-';
            if (m - 2 <= 9)s[m] += m - 2 + '0';
            else
            {
                string p = "";
                ll xx = m - 2;
                while (xx)
                {
                    ll x = xx % 10;
                    p += x + '0';
                    xx /= 10;
                }
                reverse(p.begin(), p.end());
                s[m] += p;
            }
            s[m] += '-';
            if (m - 1 <= 9)s[m] += m - 1 + '0';
            else
            {
                string p = "";
                ll xx = m - 1;
                while (xx)
                {
                    ll x = xx % 10;
                    p += x + '0';
                    xx /= 10;
                }
                reverse(p.begin(), p.end());
                s[m] += p;
            }
            s[m] += '+';
            if (m <= 9)s[m] += m + '0';
            else
            {
                string p = "";
                ll xx = m;
                while (xx)
                {
                    ll x = xx % 10;
                    p += x + '0';
                    xx /= 10;
                }
                reverse(p.begin(), p.end());
                s[m] += p;
            }
            m++;
        }
        cout << s[n] << '\n';
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值