Skip to content

算法基础课

Count # of 1 in binary

https://www.acwing.com/problem/content/803/

Code

#include <iostream>

using namespace std;
inline int lowbit(int x) {
    return x & -x;
}
int main() {
    int n;
    cin >> n;
    while (n --) {
        int x, res = 0;
        cin >> x;

        while (x) {
            x -= lowbit(x);
            res++;
        }
        cout << res << ' ';
    }
}

a - b

https://www.acwing.com/problem/content/794/

Code

#include <iostream>
#include <vector>

using namespace std;

bool cmp(vector<int> &A, vector<int> &B)
{
    if (A.size() != B.size()) return A.size() > B.size(); // 大小不一致,长的大

    for (int i = A.size() - 1; i >= 0; i -- )
        if (A[i] != B[i]) // 左数第一个不一样的大小决定数字大小
            return A[i] > B[i];

    return true;
}

vector<int> sub(vector<int> &A, vector<int> &B)
{
    vector<int> C;
    for (int i = 0, t = 0; i < A.size(); i ++ )
    {
        t = A[i] - t; // 当前数减去借位的数
        if (i < B.size()) t -= B[i]; // 如果B数组比A数组短,那么剩余值减去B当前位
        C.push_back((t + 10) % 10); // 如果t < 0,取t + 10的值(0-6 = -4),同时借位,如果t >= 0,取%10,(5-3=2=2+10%10=2)
        if (t < 0) t = 1; // 借位
        else t = 0; // 不必借位
    }

    while (C.size() > 1 && C.back() == 0) C.pop_back();
    return C;
}

int main()
{
    string a, b;
    vector<int> A, B;
    cin >> a >> b;
    for (int i = a.size() - 1; i >= 0; i -- ) A.push_back(a[i] - '0');
    for (int i = b.size() - 1; i >= 0; i -- ) B.push_back(b[i] - '0');

    vector<int> C;

    if (cmp(A, B)) C = sub(A, B);
    else C = sub(B, A), cout << '-';

    for (int i = C.size() - 1; i >= 0; i -- ) cout << C[i];
    cout << endl;

    return 0;
}

作者yxc
链接https://www.acwing.com/activity/content/code/content/39793/
来源AcWing
著作权归作者所有商业转载请联系作者获得授权非商业转载请注明出处

a * b

https://www.acwing.com/activity/content/code/content/39794/

Code

#include <iostream>
#include <vector>

using namespace std;


vector<int> mul(vector<int> &A, int b)
{
    vector<int> C;

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

    while (C.size() > 1 && C.back() == 0) C.pop_back(); // 去除leading zeros

    return C;
}


int main()
{
    string a;
    int b;

    cin >> a >> b;

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

    auto C = mul(A, b);

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

    return 0;
}

作者yxc
链接https://www.acwing.com/activity/content/code/content/39794/
来源AcWing
著作权归作者所有商业转载请联系作者获得授权非商业转载请注明出处

a / b

https://www.acwing.com/activity/content/code/content/39795/

Code

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> div(vector<int> &A, int b, int &r)
{
    vector<int> C;
    r = 0; // reminder
    for (int i = A.size() - 1; i >= 0; i -- )
    {
        r = r * 10 + A[i];
        C.push_back(r / b);
        r %= b;
    }
    reverse(C.begin(), C.end());
    while (C.size() > 1 && C.back() == 0) C.pop_back();
    return C;
}

int main()
{
    string a;
    vector<int> A;

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

    int r;
    auto C = div(A, B, r);

    for (int i = C.size() - 1; i >= 0; i -- ) cout << C[i];

    cout << endl << r << endl;

    return 0;
}

作者yxc
链接https://www.acwing.com/activity/content/code/content/39795/
来源AcWing
著作权归作者所有商业转载请联系作者获得授权非商业转载请注明出处

Last update: August 17, 2020