头像

Struggle

$\color{#6495ED}{\Large Acwing}$




离线:7小时前


活动打卡代码 AcWing 507. 积木大赛

Struggle
17天前
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;

int n;
int h[N];

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++ ) scanf("%d", &h[i]);

    int res = 0;
    for (int i = n; i; i -- ) res += max(0, h[i] - h[i - 1]);

    cout << res << endl;

    return 0;
}



活动打卡代码 AcWing 148. 合并果子

Struggle
17天前
#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

int main()
{
    int n;
    scanf("%d", &n);

    priority_queue<int, vector<int>, greater<int>> heap;
    while (n -- )
    {
        int x;
        scanf("%d", &x);
        heap.push(x);
    }

    int res = 0;
    while (heap.size() > 1)
    {
        int a = heap.top(); heap.pop();
        int b = heap.top(); heap.pop();
        res += a + b;
        heap.push(a + b);
    }

    printf("%d\n", res);
    return 0;
}
。


活动打卡代码 AcWing 496. 机器翻译

Struggle
17天前
#include <iostream>
#include <queue>

using namespace std;

const int N = 1010;

int m, n;
bool st[N];

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

    queue<int> q;
    int res = 0;

    for (int i = 0; i < n; i ++ )
    {
        int x;
        cin >> x;
        if (!st[x])
        {
            if (q.size() == m)
            {
                int t = q.front();
                st[t] = false;
                q.pop();
            }

            q.push(x);
            st[x] = true;
            res ++ ;
        }
    }

    cout << res << endl;
    return 0;
}




活动打卡代码 AcWing 445. 数字反转

Struggle
17天前
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll a;
    cin>>a;
    if(a<0)
    {
        cout<<"-";
        a=(0-a);//判断正负,若为负数,则将其改为其绝对值,并先输出'-'。
    }
    ll s[100],xb=1;
    while(a%10==0)
    {
        a/=10;//删除前导零
    }
    while(a!=0)
    {
        s[xb]=(a%10);
        a/=10;
        xb++;//将数字反转,并存入数组(或者也可以直接输出,节省时间,在这里就分开写,助于理解)
    }
    for(ll i=1;i<=xb-1;i++)
    {
        cout<<s[i];
    }
    return 0;
}



活动打卡代码 AcWing 449. 质因数分解

Struggle
17天前
#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    for (int i = 2; ; i ++ )
        if (n % i == 0)
        {
            cout << n / i << endl;
            break;
        }

    return 0;
}


活动打卡代码 AcWing 441. 数字统计

Struggle
17天前
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 10010;

int main()
{
    int l, r;
    cin >> l >> r;

    int res = 0;
    for (int i = l; i <= r; i ++ )
        for (int j = i; j; j /= 10)
            if (j % 10 == 2)
                res ++ ;

    cout << res << endl;

    return 0;
}



活动打卡代码 AcWing 211. 计算系数

Struggle
20天前
#include <iostream>
#include <algorithm>

using namespace std;

const int mod = 10007;

int qmi(int a, int k)
{
    a %= mod;
    int res = 1;
    while (k)
    {
        if (k & 1) res = res * a % mod;
        a = a * a % mod;
        k >>= 1;
    }
    return res;
}

int main()
{
    int a, b, k, n, m;

    cin >> a >> b >> k >> n >> m;

    int res = qmi(a, n) * qmi(b, m) % mod;
    for (int i = 1, j = k; i <= n; i ++, j -- )
    {
        res = res * j % mod;
        res = res * qmi(i, mod - 2) % mod;
    }

    cout << res << endl;

    return 0;
}



活动打卡代码 AcWing 458. 比例简化

Struggle
20天前
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}

int main()
{
    int A, B, L;
    cin >> A >> B >> L;

    int a, b;
    double delta = 1e9;
    for (int i = 1; i <= L; i ++ )
        for (int j = 1; j <= L; j ++ )
            if (gcd(i, j) == 1)
            {
                double x = i * 1.0 / j;
                double X = A * 1.0 / B;

                if (x >= X && x - X < delta)
                {
                    delta = x - X;
                    a = i, b = j;
                }
            }

    cout << a << ' ' << b << endl;

    return 0;
}




活动打卡代码 AcWing 312. 乌龟棋

Struggle
22天前
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 360, M = 41;

int n, m;
int score[N];
int f[M][M][M][M];

int main()
{
    int b[5] = {0};
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i ++ ) scanf("%d", &score[i]);

    for (int i = 0; i < m; i ++ )
    {
        int t;
        scanf("%d", &t);
        b[t] ++ ;
    }

    for (int A = 0; A <= b[1]; A ++ )
        for (int B = 0; B <= b[2]; B ++ )
            for (int C = 0; C <= b[3]; C ++ )
                for (int D = 0; D <= b[4]; D ++ )
                {
                    int t = score[A + B * 2 + C * 3 + D * 4];
                    int &v = f[A][B][C][D];
                    v = t;
                    if (A) v = max(v, f[A - 1][B][C][D] + t);
                    if (B) v = max(v, f[A][B - 1][C][D] + t);
                    if (C) v = max(v, f[A][B][C - 1][D] + t);
                    if (D) v = max(v, f[A][B][C][D - 1] + t);
                }

    printf("%d\n", f[b[1]][b[2]][b[3]][b[4]]);
    return 0;
}





Struggle
22天前

STL版

写法一:利用STL的set容器自动去重并排序的特征

#include<iostream>
#include<set>
using namespace std;
int main()
{
    int m;
    set<int> res;
    cin>>m;
    while(m--)
    {
        int x;
        cin>>x;
        res.insert(x);
    }
    cout<<res.size()<<endl;
    for(int x:res) cout<<x<<' ';
    return 0;
}

写法二:利用STL的方法

#include<iostream>
#include<algorithm>
using namespace std;
const int N=110;
int a[N];
int main()
{
    int m;
    cin>>m;
    for(int i=0;i<m;i++) cin>>a[i];
    sort(a,a+m);
    int ed=unique(a,a+m)-a;
    cout<<ed<<endl;
    for(int i=0;i<ed;i++) cout<<a[i]<<' ';
    return 0;
}

手动实现版

#include<iostream>
using namespace std;
const int N=110;
int a[N];
void quick_sort(int * a,int l,int r)//快速排序
{
    if(l>=r) return;
    int x=a[l+r>>1],l1=l-1,r1=r+1;
    while(l1<r1)
    {
        do l1++;while(a[l1]<x);
        do r1--;while(a[r1]>x);
        if(l1<r1) swap(a[l1],a[r1]);
    }
    quick_sort(a,l,r1),quick_sort(a,r1+1,r);
}
int unique(int * a,int n)//去重的思想是,先排序后利用双指针算法
{
    quick_sort(a,0,n-1);
    int l=0;
    for(int r=0;r<n;r++)
        if(r==0 || a[r]!=a[r-1]) a[l++]=a[r];
    return l;
}
int main()
{
    int m;
    cin>>m;
    for(int i=0;i<m;i++) cin>>a[i];
    m=unique(a,m);
    cout<<m<<endl;
    for(int i=0;i<m;i++) cout<<a[i]<<' ';
    return 0;
}

$\color{blue}{如果有什么问题,可以在评论下方指出,欢迎点赞关注}$