[Offer收割]编程练习赛70 (-2)

版权声明:欢迎指出文章不足之处~~~ https://blog.csdn.net/zhui_xiuge/article/details/81458190
  1. 数位翻转
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    //^ : 按位或(相同为0,相异为1),'|': 按位或(有一个1结果为1)
    int ans = n ^ (n - 1);

    int cnt_op = 0;
    while (ans) {
        if (ans & 1) cnt_op++;
        ans >>= 1;
    }
    cout << cnt_op << '\n';
    return 0;
}

2 . 最短公共子序列

#include <iostream>
#include <algorithm>
using namespace std;
const int MAX_LEN = 1e5 + 5;

int main() {
    char A[MAX_LEN];
    int zero_cnt = 0, one_cnt = 0;
    cin >> A;
    for (int i = 0; A[i]; i++) {
        if (A[i] == '0') zero_cnt++;
        else one_cnt++;
    }
    cout << min(zero_cnt, one_cnt) << '\n';
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhui_xiuge/article/details/81458190
今日推荐