牛客网——华为题库(1~10)

本文介绍了如何在C++中实现字符串操作,包括最后一个单词的长度计算、字符出现次数统计、随机数生成、字符串分隔、进制转换、质因数分解、取近似值、合并表记录、提取不重复整数及字符计数。涵盖了信息技术中的字符串处理、算法设计与数据结构应用。
摘要由CSDN通过智能技术生成


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

#include <bits/stdc++.h>

using namespace std;

int main(int argc, char* argv[]){
    char inPut[5000];
    cin.get(inPut, 5000);
    int strLen = 0;
    while(inPut[strLen] != '\0'){
        strLen++; //字符串长度 
    }
    
    int num = 0;int i = 0;
    while(i < strLen){
        if(inPut[i] != ' '){
            i++;
            num++;
        }
        
        if(inPut[i] == ' '){
            i++;
            num = 0;
        }
    }
    
    cout << num <<endl;
}

2.计算某字符出现次数

#include <bits/stdc++.h>

using namespace std;

int main(int argc, char* argv[]){
    /*char inPut[1000];
    cin.get(inPut, 1000);
    cin.ignore();
    char inStr;
    cin>>inStr;
    int strLen = 0;
    while(inPut[strLen] != '\0'){
        strLen++;
    }
    int num = 0;
    for(int j = 0; j < strLen; j++){
        if(inStr >= '0' && inStr <= '9'){
            if(inPut[j] == inStr) num++;
        }
        else{
            if(inPut[j] == inStr || (inPut[j]^32) == inStr) num++;
        }
    }*/
    string s;
    getline(cin,s);
    char inStr;
    cin>>inStr;
    int num = 0;
    for(int j = 0; j < s.size(); j++){
        if(inStr >= '0' && inStr <= '9'){
            if(s[j] == inStr) num++;
        }
        else{
            if(s[j] == inStr || (s[j]^32) == inStr) num++;
        }
    }
    
    cout << num << endl;
    
    return 0;
}

3.明明的随机数

#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

int main(){
    int N = 0;
    cin>>N;
    
    priority_queue<int> pq;
    
    for(int i = 0; i < N; i++){
        int num = 0;
        cin>>num;
        pq.push(num);
    }
    
    stack<int> st;
    st.push(pq.top());
    pq.pop();
    while(!pq.empty()){
        if(st.top() != pq.top()){
            st.push(pq.top());
        }
        pq.pop();
    }
    
    vector<int> res;
    while(!st.empty()){
        res.push_back(st.top());
        st.pop();
    }
    
    for(int i = 0; i < res.size(); i++){
        cout << res[i] << endl;
    }
    
    return 0;
}

4.字符串分隔

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

using namespace std;

void process(vector<string>& res, string& s){
    while(s.size() % 8 != 0){
        s += "0";
    }
    for(int i = 0; i < s.size() - 7; i += 8){
        string tmp = s.substr(i, 8);
        //cout<<tmp<<endl;
        res.push_back(tmp);
    }
}

int main(){
    string str = "";
    vector<string> res;
    
    while(cin>>str){
        process(res, str);
    }
    
    for(int i = 0; i < res.size(); i++){
        cout<<res[i]<<endl;
    }
    
    return 0;
}

5.进制转换

#include <unordered_map>
#include <algorithm>
#include <math.h>

using namespace std;

unordered_map<char, int> m = {
    {'0', 0},
    {'1', 1},
    {'2', 2},
    {'3', 3},
    {'4', 4},
    {'5', 5},
    {'6', 6},
    {'7', 7},
    {'8', 8},
    {'9', 9},
    {'A', 10},
    {'B', 11},
    {'C', 12},
    {'D', 13},
    {'E', 14},
    {'F', 15}
};

void process(string s, int& res){
    reverse(s.begin(), s.end());
    //cout<<s<<endl;
    for(int i = 0; i < s.size(); i++){
        res += m[s[i]] * pow(16, i);
        //cout<<res<<endl;
    }
}

int main(){
    string str = "";
    int res = 0;
    
    while(cin>>str){
        process(str, res);
    }
    
    cout<<to_string(res)<<endl;
    
    return 0;
}


6.质数因子

#include <iostream>
#include <vector>

using namespace std;

int main(){
    long num = 0; //
    cin >> num;
    vector<int> res;
    
    for(int i = 2; i * i <= num; ++i){ //2是最小的质因子 只需要验证一半便知2是不是质因子
        //cout<<num<<endl;
        while(num % i == 0){
            res.push_back(i);
            num /= i;         
        }        
    }
    //如果m=1,则while循环中刚好被质数分解完,如果大于1,说明没有被分解完,m就是那最后一个质数
    //同时,这句也可以应对输入为质数的特殊情况
    if(num >= 2) res.push_back(num); 
    
    for(int i = 0; i < res.size(); i++){
        cout << res[i] << " ";
    }
    cout << endl;
    
    return 0;
}

7.取近似值

#include <iostream>

using namespace std;

int main() {
    double num;
    while(cin >> num)
        {
        cout << static_cast<int>(num + 0.5) << endl; //
    }
}

8.合并表记录

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

using namespace std;

/*struct cmp{
    bool operator()(pair<int, int>& m, pair<int, int>& n) {
        return m.first < n.first;
    } 
};*/

int main(){
    int num = 0;
    cin >> num;
    
    map<int, int> m; //map内部本身就是按照key的大小顺序进行存储的
    //vector<pair<int, int>> vec;
    for(int i = 0; i < num; i++){
        int key = 0, value = 0;
        while(cin>>key>>value){
            if(!m[key]){
                m[key] = value;
            }
            else m[key] += value;//不存在时赋值,存在时累加
             
            //vec.push_back(make_pair(key, value));
        }
    }
    
    //sort(vec.begin(), vec.end(), cmp());
    
    //map内部本身就是按照key的大小顺序进行存储的
    for(map<int, int>::iterator/*auto*/ iter = m.begin(); iter != m.end(); iter++){
        cout<<iter->first<<" "<<iter->second<<endl;
    }
    
    return 0;
}

9.提取不重复的整数

#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_set>

using namespace std;

int main(){
    int num = 0;
    cin >> num;
    
    string str = to_string(num);
    reverse(str.begin(), str.end());
    unordered_set<char> tmp;
    int res = 0;
    
    for(int i = 0; i < str.size(); i++){
        if(tmp.count(str[i]) == 0){
            res = res * 10 + (str[i] - '0');
        }
        tmp.insert(str[i]);      
    }
    
    cout << res << endl;
    
    return 0;
}

10.字符个数统计

#include <iostream>
#include <string>
#include <unordered_set>

using namespace std;

int main(){
    string str;
    cin>>str;
    
    unordered_set<char> s;
    for(char c : str){
        s.insert(c);
    }
    
    cout << s.size() << endl;
    
    return 0;
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wrdoct

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值