AC了两道题,但是排名靠后了好多,大家都说是手速场qwq,可惜我不会dfs,第三题就放了,不然估计能混到250左右?Maybe~
1.AcWing 4209. 三元组
原代码:
#include<bits/stdc++.h>
using namespace std;
int a[110],b[110],c[110];
int x, y, z;
int main()
{
int n;
cin >> n;
for (int i = 0;i < n;i ++)
{
cin >> a[i] >> b[i] >> c[i];
}
for (int i = 0;i < n;i ++)
{
x += a[i];
y += b[i];
z += c[i];
}
if (x == 0 && y == 0 & z == 0) cout << "YES";
else cout << "NO";
return 0;
}
写的略傻,不必开三个数组,完全可以这么写:PS:开始用大模板写题了算了,比赛再用吧,补题不用
#include <bits/stdc++.h>
using namespace std;
int x, y, z;
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n;
cin >> n;
while (n --)
{
int a,b,c;
cin >> a >> b >> c;
x += a;
y += b;
z += c;
}
if (!x && !y&& !z) cout << "YES";
else cout << "NO";
return 0;
}
2.AcWing 4210. 数字
原代码:
#include<bits/stdc++.h>
using namespace std;
int sum;
int gcd(int sum, int t)
{
if (t != 0) return gcd(t, sum % t);
return sum;
}
int main()
{
int n;
cin >> n;
for (int i = 2;i <= n-1;i ++)
{
int s = n;
while (s > 0)
{
int mod = s % i;
s /= i;
sum += mod;
}
}
int t = n-2;
int tem = gcd(sum, t);
sum /= tem;
t /= tem;
cout << sum << '/' << t;
return 0;
}
补题:
#include<bits/stdc++.h>
using namespace std;
int sum;
int gcd(int a, int b)
{
if (a % b == 0) return b;
return gcd(b, a % b);
}
int main()
{
int n;
cin >> n;
for (int i = 2;i < n;i ++)
{
int s = n;
while (s > 0)
{
sum += s % i;
s /= i;
}
}
int t = n - 2;
int tem = gcd(sum, t);
sum /= tem;
t /= tem;
cout << sum << '/' << t;
return 0;
}
或者直接调用库函数的最大公约数 __gcd():
#include<bits/stdc++.h>
using namespace std;
int sum;
int main()
{
int n;
cin >> n;
for (int i = 2;i < n;i ++)
{
int s = n;
while (s > 0)
{
sum += s % i;
s /= i;
}
}
int t = n - 2;
int tem = __gcd(sum, t);
sum /= tem;
t /= tem;
cout << sum << '/' << t;
return 0;
}
3.AcWing 4211. 序列重排
可证明合法序列中任一元素的2因子和3因子之和唯一,故可只双关键字排序即可。
补题代码:
#include<bits/stdc++.h>
using namespace std;
const long long N = 105;
pair<int, long long> f[N];
int main()
{
int n;
cin >> n;
for (int i = 0;i < n;i ++) cin >> f[i].second;
for (int i = 0;i < n;i ++)
{
long long x = f[i].second;
while (x % 2 == 0) {f[i].first ++; x /= 2;}
while (x % 3 == 0) {f[i].first --; x /= 3;}
}
sort(f, f+n);
for (int i = 0;i < n;i ++) cout << f[i].second << ' ';
return 0;
}
y总的三关键字vector排序:
#include<bits/stdc++.h>
using namespace std;
const int N = 105;
vector<long long> f[N];
int get(long long a, int b)
{
int sum = 0;
while (a % b == 0) {sum ++; a /= b;}
return sum;
}
int main()
{
int n;
cin >> n;
for (int i = 0;i < n;i ++)
{
long long x;
cin >> x;
f[i] = {get(x,2), ~get(x,3), x};
}
sort(f, f+n);
for (int i = 0;i < n;i ++) cout << f[i][2] << ' ';
return 0;
}
struct结构体写法,多关键字的结构体排序需要手写cmp:
#include<bits/stdc++.h>
using namespace std;
const int N = 105;
struct code
{
int x;
int y;
long long z;
}f[N];
int get(long long a, int b)
{
int sum = 0;
while (a % b == 0) {sum ++; a /= b;}
return sum;
}
bool cmp(code m, code n)
{
if (m.x == n.x) return m.y > n.y;
return m.x < n.x;
}
int main()
{
int n;
cin >> n;
for (int i = 0;i < n;i ++)
{
long long x;
cin >> x;
f[i].x = get(x,2);
f[i].y = get(x,3);
f[i].z = x;
// 或者 f[i] = {get(x,2), get(x,3), x};
}
sort(f, f+n, cmp);
for (int i = 0;i < n;i ++) cout << f[i].z << ' ';
return 0;
}
借鉴dalao的dfs做法
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 110;
int n;
LL a[N], b[N];
bool f[N];
void dfs(int u)
{
if (u == n + 1)
{
for (int i = 1;i <= n;i ++)
{
cout << b[i] << ' ';
}
return;
}
for (int i = 0;i < n;i ++)
{
if (u == 1)
{
b[u] = a[i];
f[i] = true;
dfs (u + 1);
f[i] = false; // 状态回溯
b[u] = 0;
}
else if ((a[i] == b[u - 1] * 2 ||a[i] * 3 == b[u - 1]) && !f[i])
{
b[u] = a[i];
f[i] = true;
dfs (u + 1);
f[i] = false;
b[u] = 0;
}
}
}
int main()
{
cin >> n;
for (int i = 0;i < n;i ++) cin >> a[i];
dfs(1);
return 0;
}