AcWing第34场周赛题解

第34场周赛活动链接

problem.A AcWing4209 三元组

对每列数字求和即可

代码

#include <iostream>

using namespace std;

int main()
{
    int a=0,b=0,c=0;
    int t;
    cin>>t;
    while(t--)
    {
        int x,y,z;
        cin>>x>>y>>z;
        a+=x;
        b+=y;
        c+=z;
    }
    if(a==0&&b==0&&c==0)
        puts("YES");
    else
        puts("NO");
    
    return 0;
}

problem.B AcWing4210 数字

暴力计算后用欧几里得算法约分。

代码

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int base(int n, int b)
{
    int res = 0;
    while (n) res += n % b, n /= b;
    return res;
}

int gcd(int a, int b)  // 欧几里得算法
{
    return b ? gcd(b, a % b) : a;
}

int main()
{
    int n;
    cin >> n;

    int sum = 0;
    for (int i = 2; i < n; i ++ )
        sum += base(n, i);

    int a = sum, b = n - 2;
    int d = gcd(a, b);

    cout << a / d << '/' << b / d << endl;
    return 0;
}

problem.C AcWing4211 序列重排

法一:dfs找到所有序列,输出符合题目要求的序列(如下代码)
法二:数学构造(详见yxc讲解),很精妙。

代码

#include <iostream>

using namespace std;

typedef long long LL;

const int N = 110;

int n;
LL a[N];
LL path[N];
bool flag;

void dfs(int u,__int128_t st)
{
    if(flag) return;
    if(u==n)
    {
        for(int i=0;i<n;i++)
            cout<<path[i]<<' ';
        flag=true;
        return;
    }
    
    for(int i=0;i<n;i++)    
        if(!((st>>i)&1))
            if(u==0||path[u-1]==a[i]*3||path[u-1]*2==a[i])
            {
                path[u]=a[i];
                dfs(u+1,st+(1<<i));
            }
}

int main()
{
    cin>>n;
    for(int i=0;i<n;i++) cin>>a[i];
    
    dfs(0,0);
    
    return 0;
}
posted @ 2022-01-17 11:52  泝涉江潭  阅读(33)  评论(0)    收藏  举报