列表

详情


NC237446. Seventeen

描述

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

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

输入描述

The input contains one integer n ().

输出描述

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

示例1

输入:

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





原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(g++ 7.5.0) 解法, 执行用时: 3ms, 内存消耗: 520K, 提交时间: 2022-09-24 09:31:53

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	int n;
	cin>>n;
	if(n<4) cout<<"-1";
	else if(n==4) cout<<"(2+4)*3-1";
	else if(n==5) cout<<"2*5+3+4*1";
	else{
		cout<<"5+2*6+(4-3-1)";
		for(int i=7;i<=n;i++)
			cout<<"*"<<i;
	}
	return 0;		
} 

C 解法, 执行用时: 2ms, 内存消耗: 380K, 提交时间: 2022-09-24 09:39:30

#include<stdio.h>
#include<string.h>
char s[51][100]={
    "0","-1","-1",
    "-1","(4+2)*3-1","3*5+1*(4-2)",
    "1-2+3+4+5+6","2+3+4+7+(6-5)*1","1+3+4+6+(5-2)*(8-7)"
};
int main()
{
    int n;
    scanf("%d",&n);
    while(n>8)
    {
        printf("%d+%d-%d-%d+",n,n-3,n-1,n-2);
        n-=4;
    }
    printf("%s",s[n]);
}

C++(clang++ 11.0.1) 解法, 执行用时: 3ms, 内存消耗: 548K, 提交时间: 2023-06-03 22:18:24

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

Python3 解法, 执行用时: 43ms, 内存消耗: 4564K, 提交时间: 2022-05-29 14:54:46

s ,n= ["","","","","(2+4)*3-1","(5*4)-3*(2-1)"], int(input())
for i in range(6,55):
    s.append(s[i-2]+"*({}-{})".format(i,i-1))
print("-1" if n <= 3 else s[n])

上一题