头像

欣囍




离线:12小时前


活动打卡代码 AcWing 2. 01背包问题

欣囍
7天前
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1010;
int n, m, v[N], w[N], s[N];
int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        cin >> v[i] >> w[i];

    for(int i = 1; i <= n; i++)
        for(int j = m; j >= v[i]; j--)//从大到小
            s[j] = max(s[j], s[j - v[i]] + w[i]);

    cout << s[m] << endl;
    return 0;
}


活动打卡代码 AcWing 2065. 整除序列

欣囍
8天前
#include<iostream>
using namespace std;
int main()
{
    long long n;
    cin >> n;
    while(n)
    {
        cout << n << " ";
        n /= 2;
    }
    return 0;
}



欣囍
8天前
#include<iostream>
using namespace std;
int lowbit(int n)
{
    return n & -n;
}
int main()
{
    int n, m;
    cin >> n;
    while(n--)
    {
        cin >> m;
        int cnt = 0;
        while(m)
        {
            m -= lowbit(m);
            cnt++;
        }
        cout << cnt << " ";
    }
    return 0;
}


活动打卡代码 AcWing 794. 高精度除法

欣囍
8天前
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    vector<int> s, res;
    string a;
    int m;
    cin >> a >> m;

    for(int i = a.size() - 1; i >= 0; i--)
        s.push_back(a[i] - '0');//字符数组一定记得减0

    int r = 0;

    for(int i = s.size() - 1; i >= 0; i--)
    {
        r = r * 10 + s[i];
        res.push_back(r / m);
        r %= m;
    }
    reverse(res.begin(), res.end());

    while(res.size() > 1 && res.back() == 0) res.pop_back();

    for(int i = res.size() - 1; i >= 0; i--)
        printf("%d", res[i]);

    printf("\n%d", r);
    return 0;

}


活动打卡代码 AcWing 793. 高精度乘法

欣囍
9天前
#include<iostream>
#include<vector>
using namespace std;
int main()
{
    string a;
    int b;
    vector<int> aa, s;
    cin >> a >> b;
    if(b == 0)
    {
        printf("0\n");
        return 0;
    }

    for(int i = a.size() - 1; i>= 0; i--)
        aa.push_back(a[i] - '0');

    int t = 0;
    for(int i = 0; i < aa.size(); i++)
    {
        t += aa[i] * b;
        s.push_back(t % 10);
        t /= 10;
    }
    if(t) s.push_back(t);

    for(int i = s.size() - 1; i >= 0; i--)
        printf("%d", s[i]);
    return 0;
}


活动打卡代码 AcWing 791. 高精度加法

欣囍
9天前
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
string a, b;
int main()
{
    int t = 0;
    vector<int> c;
    cin >> a >> b;
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());

    for(int i = 0; i < a.size() || i < b.size(); i++)
    {
        if(i < a.size()) t += a[i] - '0';
        if(i < b.size()) t += b[i] - '0';
        c.push_back(t % 10);
        t /= 10;
    }
    if(t) c.push_back(1);

    for(int i = c.size() - 1; i >= 0; i--)
        printf("%d", c[i]);

    return 0;
}


活动打卡代码 AcWing 842. 排列数字

欣囍
9天前
#include<iostream>
using namespace std;
const int N = 10;
int path[N];
bool st[N];
int n;
void dfs(int u)
{
    if(u == n)
    {
        for(int i = 0; i < n; i++)
            cout << path[i] << " ";
        cout << endl;
    }
    for(int i = 1; i <= n; i++)
        if(!st[i])
        {
            path[u] = i;
            st[i] = true;
            dfs(u + 1);
            st[i] = false;
        }
}
int main()
{
    cin >> n;
    dfs(0);
    return 0;
}



欣囍
9天前

AcWing《蓝桥杯C++ AB组辅导课》拼团优惠!https://www.acwing.com/activity/content/introduction/19/group_buy/10343/