牛客网——华为题库(91~100)


91.走方格的方案数

#include <bits/stdc++.h>

using namespace std;

int main(){
    
    
    int n = 0, m = 0;
    while(cin >> n >> m){
    
    
        vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
        for(int i = 0; i <= n; i++){
    
    
            dp[i][0] = 1;
        }
        for(int j = 0; j <= m; j++){
    
    
            dp[0][j] = 1;
        }
        
        for(int i = 1; i <= n; i++){
    
    
            for(int j = 0; j <= m; j++){
    
    
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; //
            }
        }
        
        cout << dp[n][m] << endl;
    }
    
    return 0;
}

92.在字符串中找出连续最长的数字串

#include <bits/stdc++.h>
using namespace std;

int main() {
    
    
    string s = "";
    while(cin >> s){
    
    
        vector<pair<string, int>> res;
        int right = 0;
        int maxLength = INT_MIN;
        for(int i = 0; i < s.size(); i++){
    
    
            if(isdigit(s[i])){
    
    
                right = i;
                while(right < s.size() && isdigit(s[right])){
    
    
                    right++;
                }
                
                if(right - i> maxLength){
    
    
                    string tmp = s.substr(i, right - i);
                    res.clear();
                    res.push_back(make_pair(tmp, tmp.size()));
                    maxLength = right - i;
                }
                else if(right - i == maxLength){
    
    
                    string tmp = s.substr(i, right - i);
                    string str = res[0].first + tmp;
                    res.clear();
                    res.push_back(make_pair(str, tmp.size()));
                }
                
                i = right;
            }
        }
        
        //输出
        for(auto iter = res.begin(); iter != res.end(); iter++){
    
    
            cout << iter->first << "," << iter->second << endl;
        }
       
    }
}

93.数组分组

#include <bits/stdc++.h>

using namespace std;

bool dp(vector<int> &nums, int t, int k){
    
    
    if(t == 0 && k == nums.size()) return true;
    if(k == nums.size()) return false;
    
    //如果只是3的倍数,不能加到该集合中。
    if(nums[k] % 3 == 0) return dp(nums, t, k + 1);
    if(nums[k] == t) return true;
    
    //对于一个数有两种选择,加或者不加到该集合中
    return dp(nums, t - nums[k], k + 1) || dp(nums, t, k + 1); //
}

int main(){
    
    
    int n = 0;
    while(cin >> n){
    
    
        /*//法一
        vector<int> nums;
        int sum = 0; //只要找到总和的一半即可,剩下的数字之和自然为总和的一半。
        int part = 0; //5的倍数的数字之和
        for(int i =0; i < n; i++){
            int num = 0;
            cin >> num;
            if(num % 5 == 0) part += num; //如果是5的倍数,不放入数组nums
            else nums.push_back(num);
            
            sum += num;
        }
        
        //如果所有数之和不是偶数,则肯定是false
        if (sum%2){
            cout<<"false"<<endl;
        }
        else{
            sum = sum / 2;
            if(dp(nums, sum - part, 0)){
                cout << "true" << endl;
            }
            else cout<<"false"<<endl;
        }
        
        nums.clear();*/
        
        //法二
        vector<int> arr;
        int sum3 = 0;
        int sum5 = 0;
        int rest = 0; //剩余的数的和
        for(int i = 0; i < n; i++){
    
    
            int x;
            cin >> x;
            if(x % 5 == 0) //先求一个组的和
                sum5 += x;
            else if(x % 3 == 0) //再求另一个组的和
                sum3 += x;
            else{
    
    
                arr.push_back(x); //剩余的加入数组并求和
                rest += x;
            }
        }
        
        //
        unordered_set<int> s1, s2;
        s1.insert(0); //枚举所有组合的不重复和,0代表空数组
        for(int i = 0; i < arr.size(); i++){
    
     //遍历剩余的每一个数字,枚举所有可能性
            s2 = s1; //
            for(auto iter : s2){
    
    
                s1.insert(iter + arr[i]); //s1中保存的是所有的剩余数的和的可能性
            }
        }
        
        bool res = false;
        for(auto iter : s1){
    
     //遍历枚举的集合
            if(iter + sum5 == sum3 + (rest - iter)){
    
     //分成两部分后相等
                res = true;
                break;
            }
        }
        
        cout << (res ? "true" : "false") << endl;
    }
    
    return 0;
}

94.记票统计

#include <bits/stdc++.h>

using namespace std;

int main(){
    
    
    int n = 0; //候选人的人数
    cin >> n;
    vector<string> name(n, ""); //候选人的名字
    for(int i = 0; i < n; i++){
    
          
        string tmp = "";
        cin >> tmp;
        name[i] = tmp;
    }
    int num = 0; //投票人的人数
    cin >> num;
    vector<string> touPiao(num, ""); //投票
    for(int i = 0; i < num; i++){
    
    
        string tmp = "";
        cin >> tmp;
        touPiao[i] = tmp;
    }
    
    unordered_map<string, int> m;
    for(int i = 0; i < n; i++){
    
          
        m[name[i]] = 0; //初始化
    }
    
    int invalid = 0;
    for(int i = 0; i < touPiao.size(); i++){
    
    
        bool isValid = false;
        for(int j = 0; j < name.size(); j++){
    
    
            if(touPiao[i] == name[j]){
    
    
                m[name[j]]++;
                isValid = true;
            }
        }
        
        //投票的名字不存在n个候选人的名字中
        if(!isValid){
    
    
            invalid++;
        }
    }
    
    //输出
    for(int i = 0; i < n; i++){
    
    
        cout << name[i] << " : " << m[name[i]] << endl;
    }
    cout << "Invalid" << " : " << invalid << endl;
    
    return 0;
}

95.人民币转换

#include <bits/stdc++.h>

using namespace std;

unordered_map<int, string> m ={
    
    
    {
    
    0, "零"},
    {
    
    1, "壹"},
    {
    
    2, "贰"},
    {
    
    3, "叁"},
    {
    
    4, "肆"},
    {
    
    5, "伍"},
    {
    
    6, "陆"},
    {
    
    7, "柒"},
    {
    
    8, "捌"},
    {
    
    9, "玖"},
    {
    
    10, "拾"},
};

// 输入:151121.15
// 输出:人民币拾伍万壹仟壹佰贰拾壹元壹角伍分
void sayPre(string pre, string& res){
    
    
    if (pre == "0") return ; // 判断小数点前面是不是空的
    
    for(int i = 0, j = pre.size() - 1; i < pre.size(); i++, j--){
    
    
        // i代表的是我们遍历的字符串, j是我们i后面有几个数
        if(pre[i] != '0' && !(pre[i] == '1' && j % 4 == 1)){
    
    
            res += m[pre[i] - '0'];
        }
        //转换中文  // if分别对应后面输出的亿万千百十
        if(j != 0 && j >= 8 && j % 8 == 0){
    
    
            res += "亿";
        }
        if (j != 0 and j % 4 == 0 and j % 8 != 0)
            pre[i + 1] == '0' ? res += "万零" : res += "万";
        if (j != 0 and j % 4 == 3 and pre[i] != '0')
            pre[i + 1] == '0' && pre[i + 2] != '0' ? res += "仟零" : res += "仟";
        if (j != 0 and j % 4 == 2 and pre[i] != '0')
            pre[i + 1] == '0' && pre[i + 2] != '0' ? res += "佰零" : res += "佰";
        if (j != 0 and j % 4 == 1 and pre[i] != '0')
            res += "拾";
    }
    
    res += "元"; // 最后我们输出元
}

void sayEnd(string end, string& res){
    
    
    if (end == "00")
        res += "整";
    else if (end[0] == '0')
        res += m[end[1] - '0'] + "分";
    else if (end[1] == '0')
        res += m[end[0] - '0'] + "角";
    else
        res += m[end[0] - '0'] + "角" + m[end[1] - '0'] + "分";
    // 分类讨论, 讨论我们小数点后两位的所有情况
}

int main(){
    
    
    string s = "";
    while(cin >> s){
    
    
        string res = "";
        res += "人民币";
        string pre = "", end = "";
        bool xiaoShuDian = false;
        for(char ch : s){
    
    
            if(ch == '.'){
    
    
                xiaoShuDian = true;
                continue;
            }
            // 这里我们以小数点为分隔, 把小数点前面的存储到了pre里面,
            // 小数点后面的存储到了end里面
            xiaoShuDian ? end += ch : pre += ch;
        }
        
        sayPre(pre, res);
        sayEnd(end, res);
        
        cout << res << endl;
    }
}

96.表示数字

#include <bits/stdc++.h>

using namespace std;

int main(){
    
    
    string s = "";
    while(cin >> s){
    
    
        string res = "";
        int i = 0; //
        while(i < s.size()){
    
              
            if(isdigit(s[i])){
    
    
                res += "*";
                int right = i;
                while(right < s.size() && isdigit(s[right])){
    
         
                    res += s[right];
                    right++;
                    i++;
                }
                res += "*";
            }
            
            else{
    
    
                res += s[i];
                i++;
            }        
        }
        
        cout << res << endl;
    }
    
    return 0;
}

97.记负均正

#include <bits/stdc++.h>

using namespace std;

int main(){
    
    
    int n = 0;
    while(cin >> n){
    
    
        int negNums = 0;
        double sum = 0, cnt = 0;
        vector<int> nums(n, 0);
        for(int i = 0; i < n; i++){
    
    
            int num = 0;
            cin >> num;
            nums[i] = num;
            if(num < 0){
    
    
                negNums++;
            }
            else if(num > 0){
    
    
                cnt++;
                sum += num;
            }
        }
        
        double aveNum = 0.0;
        if(cnt != 0)
            aveNum = (double)sum / cnt; //sum * 1.0 / cnt
        
        //结果保留一位小数
        printf("%d %.1f\n", negNums, aveNum); //
    }
  
    
    return 0;
}

98.自动售货系统

99.自守数

#include <bits/stdc++.h>
using namespace std;

bool isZiShouShu(int num){
    
    
    int tmp = num * num;
    string s1 = to_string(num);
    string s2 = to_string(tmp);
    reverse(s1.begin(), s1.end());
    reverse(s2.begin(), s2.end());
    //cout << s1 << "," << s2 << endl;
    int i = 0;
    while(i < s1.size()){
    
    
        if(s1[i] != s2[i]){
    
    
            return false;
        }
        i++;
    }
    
    return true;
}

int main(){
    
    
    int n = 0;
    while(cin >> n){
    
    
        int res = 0;
        for(int i = 0; i <= n; i++){
    
    
            if(isZiShouShu(i)) res++;
        }
        
        cout << res << endl;
    }
    
    return 0;
}

100.等差数列

#include <bits/stdc++.h>

using namespace std;

#define a1 2
#define d 3

int main(){
    
    
    int n = 0; 
    while(cin >> n){
    
    
        int res = 0;
        int an = a1 + (n - 1) * d;
        res = (a1 + an) * n / 2;
        
        cout << res << endl;
    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/XUfengge111/article/details/125294680