比赛链接
“山大地纬杯”第十二届山东省ICPC大学生程序设计竞赛(正式赛)
用1~n所有数字用+,-,*,(,),组成计算式使得其得数为17
解题思路
思维
由于任意连续的 \(4\) 个数都可以消为 \(0\),即 \(n+n-3-(n-1)-(n-2)=0\) ,故可以预处理出来满足条件的前 \(4\) 个数,后面每 \(4\) 个数消为 \(0\) 即可
- 时间复杂度:\(O(n)\)
代码
// Problem: Seventeen
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/43685/A
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
// %%%Skyqwq
#include <bits/stdc++.h>
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
template <typename T> void inline read(T &x) {
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}
int n;
string s[]={"","-1","-1","-1","(1+4)*3+2","1*2*5+3+4","1*2*5+4-3+6","2*5+3+4+7-6-1"};
int main()
{
cin>>n;
if(n<4)cout<<s[n];
else if(n<=7)cout<<s[n];
else
{
while(n>7)
{
cout<<n<<'+'<<n-3<<'-'<<n-1<<'-'<<n-2<<'+';
n-=4;
}
cout<<s[n];
}
return 0;
}
标签:typedef,第十二届,int,纬杯,long,ICPC,template,define
From: https://www.cnblogs.com/zyyun/p/16815946.html