【QZMS】5 HuaWei 108 (1-8)

1.字符串最后一个单词的长度

在这里插入图片描述

#include<iostream>
#include<vector>
#include<string>

using namespace std;

int main() {
    
    string input;
    vector<string>arr;
    while(cin >> input) {
        arr.push_back(input);
    }
    cout << arr[arr.size() - 1].size() << endl;    
    
    return 0;
}

2.计算字符个数

在这里插入图片描述

#include <iostream>
#include <string>

using namespace std;

int main() {
    string input;
    char check;
    int res = 0;
    cin >> input;
    cin >> check;
    for(char x : input) {
        if(x == check) { //小写
            res ++;
        } 
        else if(x + 32 == check) { // 大写A 65 + 32 = a 97
            res ++;
        }
    }
    
    cout << res << endl;
    
    return 0;
}

3.明明的随机数

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <set>

using namespace std;

int main() {
    int n, x;    
    set<int>hash;
    while(cin >> n) {
        hash.clear(); //清楚上一组数据
        for(int i = 0; i < n; i++) {
            cin >> x;
            hash.insert(x);
        }
        for(int x : hash) {
            cout << x << endl;
        }
    }  
        
    return 0;
}

4.字符串分割

在这里插入图片描述

#include <iostream>
#include <string>

using namespace std;

int main() {
    string str;
    int n = 2;
    while(n --) {
        cin >> str;
        while(str.size() > 8) {
            cout << str.substr(0, 8) << endl;
            str = str.substr(8);
        }
        cout << str.append(8 - str.size(), '0') << endl;
    }    
    
    return 0;
}

5.进制转换

在这里插入图片描述

#include <iostream>
#include <string>

using namespace std;

int main() {
    int x;
    while(cin >> hex >> x) {
        cout << x << endl;
    }  
        
    return 0;
}
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    string s;
    while(cin >> s) {
        int bit = 0;
        int res = 0;
        for(int i = s.size() - 1; i > 1; i --) { //过滤前面的 0x
            if(s[i] >= '0' && s[i] <= '9') {
                res += (s[i] - '0') * pow(16, bit++);
            }
            else if(s[i] >= 'A' && s[i] <= 'F') {
                res += (s[i] - 'A' + 10) * pow(16, bit++);
            }
        }
        cout << res << endl;
    }    
    return 0;
}

6.质数因子

在这里插入图片描述

#include <iostream>

using namespace std;

int main() {
    long x;
    while(cin >> x) {
        while(x != 1) {
            for(int i = 2; i <= x; i ++) {
                if(x % i == 0) {
                    x /= i;
                    cout << i << ' ';
                    break;
                }
            }
        }
    }    
    return 0;
}

7.取近似值

在这里插入图片描述

#include <iostream>

using namespace std;

int main() {
    
    float x;
    cin >> x;
    cout << (int)(x + 0.5);    
    
    return 0;
}

8.合并表记录

在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <unordered_map>  //这是没有排序的
#include <map>

using namespace std;

int main() {
    
    int n;
    int k, v;
    map<int, int> hash;
    while(cin >> n) {
        for(int i = 0; i < n; i ++) {
            cin >> k >> v;
            if(hash.count(k)) {
                hash[k] += v;
            } else {
                hash[k] = v;
            }
        }

        for(auto x : hash) {
            cout << x.first << ' ' << x.second << endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值