牛客--华为机试(5)

题目1

给出一个名字,该名字有26个字符串组成,定义这个字符串的“漂亮度”是其所有字母“漂亮度”的总和。
每个字母都有一个“漂亮度”,范围在1到26之间。没有任何两个字母拥有相同的“漂亮度”。字母忽略大小写。
给出多个名字,计算每个名字最大可能的“漂亮度”。
解题思路:统计每个字符出现的次数,让出现次数多的字符“漂亮度”大。

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

int main(){
    
    
    int n;
    while (cin >> n){
    
    
        vector<int> result(n,0);
        for (int i = 0; i < n; i++){
    
    
            string str;
            cin >> str;
            vector<int> count(26,0);
            int length = str.size();
            for (int j = 0; j < length; j++){
    
    
                if (str[j] >= 65 && str[j] <= 90)
                    count[str[j] - 65]++;
                else
                    count[str[j] - 97]++;
            }
            sort(count.begin(),count.end());
            int k = 25, sum = 0;
            while(count[k] != 0){
    
    
                sum = sum + count[k] * (k + 1);
                k--;
            }
            cout << sum << endl;
        }
    }
}
题目2

输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。

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

int main(){
    
    
    string str;
    while(getline(cin,str)){
    
    
        int length = str.size();
        int letterCount = 0, spaceCount = 0, digitCount = 0, otherCount = 0;
        for (int i = 0; i < length; i++){
    
    
            if ((str[i] >= 65 && str[i] <= 90) || (str[i] >= 97 && str[i] <= 122))
                letterCount++;
            else if (str[i] >= 48 && str[i] <= 57)
                digitCount++;
            else if (str[i] == ' ')
                spaceCount++;
            else
                otherCount++;
        }
        cout << letterCount << endl;
        cout << spaceCount << endl;
        cout << digitCount << endl;
        cout << otherCount << endl;
    }
}
题目3

假设一个球从任意高度自由落下,每次落地后反跳回原高度的一半; 再落下, 求它在第5次落地时,共经历多少米?第5次反弹多高?最后的误差判断是小数点6位。

#include<iostream>
using namespace std;

int main(){
    
    
    int h;
    while (cin >> h){
    
    
        double h1 = h;
        double sum = 2.875 * h1;
        double h5 = h1 / 32;
        cout << sum << endl;
        cout << h5 << endl;
    }
}
题目4

先输入key和要加密的字符串,返回加密后的字符串。
工作原理:首先,选择一个单词作为密匙,如TRAILBLAZERS。如果单词中包含有重复的字母,只保留第1个,其余几个丢弃。现在,修改过的那个单词属于字母表的下面,如下所示:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
T R A I L B Z E S C D F G H J K M N O P Q U V W X Y
上面其他用字母表中剩余的字母填充完整。在对信息进行加密时,信息中的每个字母被固定于顶上那行,并用下面那行的对应字母一一取代原文的字母(字母字符的大小写状态应该保留)。

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

int main(){
    
    
    string key, str;
    while (cin >> key >> str){
    
    
        //去除key中的重复字母
        int keyLength = key.size();
        for (int i = 0; i < keyLength; i++){
    
    
            while (key.find(key[i]) != key.rfind(key[i])){
    
    
                key.erase(key.rfind(key[i]), 1);
            }
            keyLength = key.size();
        }
        //获得修改后的字母表顺序
        string letter = "abcdefghijklmnopqrstuvwxyz";
        for (int i = 0; i < keyLength; i++)
            letter.erase(letter.find(key[i]),1);
        letter = key + letter;
        //输出加密字符串
        for (int i = 0; i < str.size(); i++){
    
    
            cout << letter[str[i]-97];
        }
        cout << endl;
    }
}
题目5

蛇形矩阵是由1开始的自然数依次排列成的一个矩阵上三角形。
样例输入
5
样例输出
1 3 6 10 15
2 5 9 14
4 8 13
7 12
11

#include<iostream>
using namespace std;
int main(){
    
    
    int n;
    while(cin >> n){
    
    
        int first = 1;
        for (int i = 0; i < n; i++){
    
    
            first += i;//每列的第一个数是上面的数加i
            cout << first << ' ';
            int temp = first;
            for (int j = 1; j < n-i; j++){
    
    
                temp = temp + i + j + 1;
                cout << temp << ' ';
            }
            cout << endl;
        }
    }
}
题目6

密码转换规则: abc–2, def–3, ghi–4, jkl–5, mno–6, pqrs–7, tuv–8 wxyz–9,把密码中出现的小写字母都变成对应的数字,数字和其他的符号都不做变换,密码中出现的大写字母则变成小写之后往后移一位,如:X,先变成小写,再往后移一位,不就是y了嘛,简单吧。记住,z往后移是a哦。

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

int main(){
    
    
    string str;
    while (getline(cin,str)){
    
    
        int length = str.size();
        string result;
        for (int i = 0; i < length; i++){
    
    
            if (str[i] >= 65 && str[i] <= 90){
    
    
                if (str[i] == 'Z')
                    result.push_back('a');
                else
                    result.push_back(str[i]+32+1);
            }
            else if (str[i] >= 97 && str[i] <= 99)
                result.push_back('2');
            else if (str[i] >= 100 && str[i] <= 102)
                result.push_back('3');
            else if (str[i] >= 103 && str[i] <= 105)
                result.push_back('4');
            else if (str[i] >= 106 && str[i] <= 108)
                result.push_back('5');
            else if (str[i] >= 109 && str[i] <= 111)
                result.push_back('6');
            else if (str[i] >= 112 && str[i] <= 115)
                result.push_back('7');
            else if (str[i] >= 116 && str[i] <= 118)
                result.push_back('8');
            else if (str[i] >= 119 && str[i] <= 122)
                result.push_back('9');
            else
                result.push_back(str[i]);
                
        }
        cout << result << endl;
    }
}
题目7

给定n个字符串,请对n个字符串按照字典序排列。

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

bool stringCompare(string str1, string str2){
    
    //判断st1是否大于str2
    int length1 = str1.size();
    int length2 = str2.size();
    int length = length1 < length2 ? length1 : length2;
    int i;
    for (i = 0; i < length; i++){
    
    
        if (str1[i] > str2[i])
            return true;
        if (str1[i] < str2[i])
            return false;
    }
    if (i == length && length == length1)//相同位置的字符相同,但str1较短
        return false;
    if (i == length && length == length2)//相同位置的字符相同,但str2较短
        return true;
}

int main(){
    
    
    int n;
    while (cin >> n){
    
    
        vector<string> strVec(n);
        for (int i = 0; i < n; i++)
            cin >> strVec[i];
        //冒泡法排序
        for (int i = 0; i < n-1; i++){
    
    
            for (int j = 0; j < n-i-1; j++){
    
    
                if (stringCompare(strVec[j], strVec[j+1])){
    
    
                    string temp = strVec[j+1];
                    strVec[j+1] = strVec[j];
                    strVec[j] = temp;
                }
            }
        }
        for (int i = 0; i < n; i++)
            cout << strVec[i] << endl;
    }
}

也可以直接使用algorithm头文件中的sort函数对vector容器中的字符串排序。

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

int main(){
    
    
    int n;
    while (cin >> n){
    
    
        vector<string> strVec(n);
        for (int i = 0; i < n; i++)
            cin >> strVec[i];
        sort(strVec.begin(),strVec.end());
        for (int i = 0; i < n; i++)
            cout << strVec[i] << endl;
    }
}
题目8

编写一个函数,计算字符串中含有的不同字符的个数。字符在ACSII码范围内(0~127),换行表示结束符,不算在字符里。不在范围内的不作统计。
方法一:先去除重复字符,再统计个数。

#include<iostream>
#include<string> 
using namespace std;
int main(){
    
    
    string str;
    while (getline(cin,str)){
    
    
        int length = str.size();
        for (int i = 0; i < length; i++){
    
    //字符串去重
            while (str.find(str[i]) != str.rfind(str[i]))
                str.erase(str.rfind(str[i]),1);
        }
        int count = 0;//统计数量
        int length2 = str.size();
        for (int i = 0; i < length2; i++){
    
    
            if (str[i] >= 0 && str[i] <= 127)
                count++;
        }
        cout << count << endl;
    }
}

方法二:使用一个数组记录每个字符出现的次数,统计出现次数不为0的字符的个数。

#include<iostream>
#include<string> 
using namespace std;
int main(){
    
    
    string str;
    while (getline(cin,str)){
    
    
        int length = str.size();
        int array[128] = {
    
    0};
        for (int i = 0; i < length; i++){
    
    
            if (str[i] >= 0 && str[i] <= 127)
                array[str[i]]++;
        }
        int count = 0;
        for (int i = 0; i <= 127; i++){
    
    
            if (array[i])
                count++;
        }
        cout << count << endl;
    }
}
题目9

输入一个int型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数。
解题思路:记录每个数字出现的次数,如果出现次数为0,则输出,否则,不输出。

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

int main(){
    
    
    int n;
    while (cin >> n){
    
    
        int count[10] = {
    
    0};
        while (n){
    
    //记录每个数字出现的次数
            if (count[n%10] == 0)
                cout << n%10;
            count[n%10]++;
            n = n / 10;
        }
        cout << endl;
    }
}
题目10

写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。(多组同时输入 ,最前面两位表示符号,如0xA)

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

int main(){
    
    
    string str;
    while (cin >> str){
    
    
        int length = str.size();
        int num = 0, a = 1;//a用来表示16的幂
        for (int i = length-1; i >= 2; i--){
    
    
            int d;
            if (str[i] >= 48 && str[i] <= 57)
                d = str[i] - 48;
            else
                d = str[i] - 55;
            int m = length - i - 1;
            num += d * a;
            a *= 16;
        }
        cout << num << endl;
    }
}
题目11

输入一个正整数,按照从小到大的顺序输出它的所有质因子(重复的也要列举)(如180的质因子为2 2 3 3 5 )。

#include<iostream>
using namespace std;

int main(){
    
    
    long n;
    while (cin >> n){
    
    
        while (n != 1){
    
    
            for (int i = 2; i <= n; i++){
    
    
                if (n % i == 0){
    
    
                    cout << i << ' ';
                    n = n / i;
                    break;//下次还是从2开始
                }
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42820853/article/details/106643524
今日推荐