C/C++教程

code

本文主要是介绍code,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

A

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

void solve() {
  int n;
  cin >> n;
  vector<int> g[n];
  vector<int> dep(n), cnt(n);
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  dep[0] = 1;
  function<void(int, int)> dfs = [&](int cur, int pre) {
    cnt[cur]++;
    for (auto x : g[cur]) {
      if (x != pre) {
        dep[x] = dep[cur] + 1;
        dfs(x, cur);
        cnt[cur] += cnt[x];
      }
    }
  };
  dfs(0, -1);
  for (int i = 0; i < n; i++) {
    cout << dep[i] << ' ' << n + 1 - cnt[i] << '\n';
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int tt = 1;
  cin >> tt;
  for (int _ = 1; _ <= tt; _++) {
    solve();
  }
  return 0;
}

E

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

void solve() {
  int n, m;
  cin >> n >> m;
  auto get = [&](string s) {
    int num = 0;
    s.pop_back();
    if (s == "A") {
      num = 14;
    } else if (s == "K") {
      num = 13;
    } else if (s == "Q") {
      num = 12;
    } else if (s == "J") {
      num = 11;
    } else if (s == "T") {
      num = 10;
    } else {
      num = stoi(s);
    }
    return num;
  };
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    a[i] = get(s);
  }
  vector<int> b(m);
  for (int i = 0; i < m; i++) {
    string s;
    cin >> s;
    b[i] = get(s);
  }
  string s;
  cin >> s;
  int pro = get(s);
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  // 必败
  if (n == 1 || b[0] >= pro || a[0] >= pro) {
    cout << "Shou" << '\n';
    return;    
  }
  int big = *prev(lower_bound(a.begin(), a.end(), pro));
  if (m == 1 && b[0] > big) {
    cout << "Shou" << '\n';
    return;
  }
  // 必胜
  // Bob手里只有一张牌
  if (m == 1 && b[0] < pro) {
    cout << "Pang" << '\n';
    return;
  }
  // Bob手里有两张小牌
  if (m >= 2 && b[0] < pro && b[1] < pro) {
    cout << "Pang" << '\n';
    return;
  }
  //特殊情况
  if (n > 3 && b[0] <= big && b.back() < a.back() && a[0] < pro && a[1] < pro) {
    cout << "Pang" << '\n';
    return;
  }
  cout << "Shou" << '\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int tt = 1;
  cin >> tt;
  for (int _ = 1; _ <= tt; _++) {
    solve();
  }
  return 0;
}

I

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  } 
  i64 cnt0 = 0, cnt1 = 0, cnt2 = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > 0) {
      cnt1++;
    } 
    if (a[i] < 0) {
      cnt2++;
    }
  }
  ans += cnt1 * cnt2;
  for (int i = 0; i < n; i++) {
    if (a[i] == 0) {
      cnt0++;
      ans += n - cnt0;
    }
  }
  cout << ans << '\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int tt = 1;
  cin >> tt;
  for (int _ = 1; _ <= tt; _++) {
    solve();
  }
  return 0;
}

L

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

void solve() {
  int n;
  string s;
  cin >> n >> s;
  vector<int> cnt(n + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i - 1] == '1') {
      if (i + (i & -i) <= n) {
        cnt[i + (i & -i)]++;
      }
    }
  }
  i64 ans = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i - 1] == '0' && cnt[i] == 1) {
      ans++;
    } else if (s[i - 1] == '1' && cnt[i] == 0) {
      ans++;
    }
  }
  cout << ans << '\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int tt = 1;
  cin >> tt;
  for (int _ = 1; _ <= tt; _++) {
    solve();
  }
  return 0;
}
这篇关于code的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!