8.25 快手

1 版本号

#include <iostream>
#include <vector>
using namespace std;

vector<int> getVec(string a) {
 vector<int> res;
 int ind = -1, len = a.length();
 while(ind < len) {
  int num = 0;
  while(++ind < len && a[ind] != '.') num = num*10 + a[ind] - '0';
  res.push_back(num);
 }
 return res;
}

bool getRes(string sta, string stb) {

 vector<int> arr = getVec(sta);
 vector<int> brr = getVec(stb);
 int len = max(arr.size(), brr.size());

 for(int i=0; i<len; i++) {
  int a = (i >= arr.size() ? 0 : arr[i]);
  int b = (i >= brr.size() ? 0 : brr[i]);
  if(a > b) return false;
  if(b > a) return true;
 }
 return false;
}

int main() {
 int m;
 cin >> m;
 for(int i=0; i<m; i++){
  string sta, stb;
  cin >> sta >> stb;
  bool flag = getRes(sta, stb);
  if(flag) cout << "true" << endl;
  else cout << "false" << endl;
 }
 return 0;
}

2 快乐数

#include <iostream>
using namespace std;

bool isHappy(int n) {
    if (n == 1)return true;
        int temp;
        // 当n小于7时,均不为开心数
        while (n >= 7) {
            temp = 0;
            while (n) {
                int cur = n%10;
                temp += cur * cur;
                n /= 10;
            }
            if (temp == 1)return true;
            n = temp;
        }
        return false;
}

int main() {
 int m, tem;
 cin >> m;
 for(int i=0; i<m; i++){
  cin >> tem;
  bool flag = isHappy(tem);
  if(flag) cout << "true" << endl;
  else cout << "false" << endl;
 }
 return 0;
}

3 合并流

#include <iostream>
#include <vector>
#include <sstream>
using namespace std;

vector<string> getVec(string a){
    vector<string> res;
    stringstream ss(a);
    string tem;
    while(ss >> tem)
     res.push_back(tem);
    return res;
}

int main() {
    string sta, stb;
    getline(cin, sta);
    getline(cin, stb);
    vector<string> arr = getVec(sta);
    vector<string> brr = getVec(stb);
    vector<string> res;
    int i=0, j=0, len1 = arr.size(), len2 = brr.size();
    while(i<len1) {
     int right = min(i+4, len1);
     while(i < right) res.push_back(arr[i++]);
     if(j < len2)res.push_back(brr[j++]);
    }
    while(j<len2)  res.push_back(brr[j++]);
    string str;
    for(auto &it:res)
     str += it + " ";
    str.pop_back();
    cout << str << endl;

    return 0;
}

4 红黑序列

#include <iostream>
#include <vector>
using namespace std;

vector<int> faMap;
vector<int> sizeMap;
int mode = 1e9 + 7;
int getFa(int a) {
    int fa = faMap[a];
    if(fa != a) fa = getFa(fa);
    // 把并查集变扁平
    faMap[a] = fa;
    return fa;
}

void unionMap(int a, int b) {
    int fa = getFa(a), fb = getFa(b);
    if(fa == fb) return ;
    if(sizeMap[fa] > sizeMap[fb]) 
        sizeMap[fa] += sizeMap[fb], faMap[fb] = fa;
    else sizeMap[fb] += sizeMap[fa], faMap[fa] = fb;
}

long long pow(int num, int n) {
    if(num == 1 || n == 0) return 1;
    long long res = 1;
    long long x = num;
    while(n > 0) {
        if((n & 1) == 1) res = res * x % mode;
        n >>= 1;
        x = x * x % mode;
    }
    return res;
}

int main() {
    int n, k, a, b, c;
    cin >> n >> k;
    // 初始化并查集
    faMap.resize(n), sizeMap.resize(n);
    for(int i=0; i<n; i++) faMap[i] = i, sizeMap[i] = 1;

    for(int i=0; i<n-1; i++){
        cin >> a >> b >> c;
        a--, b--;
        if(c != 1) unionMap(a, b);
    }

    long long res = pow(n, k);

    for(int i=0; i<n; i++) 
        if(faMap[i] == i ) 
            res -= pow(sizeMap[i], k);

    cout << res % mode << endl;

    return 0;
}

1 版本号

图片说明

图片说明
图片说明

2 快乐数

图片说明

图片说明

3 流合并

图片说明

4 最难

图片说明
图片说明
图片说明

图片说明

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务