华为机试部分刷题记录

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

#include <iostream>
#include <string>

using namespace std;

int main(){
    string str;
    getline(cin,str);
    int count = 0;
    int len = str.length();
    for(int i = len-1; i >=0 ; --i){
        if(str[i] != ' '){
            count++;
        }else{
            break;
        }
    }
    cout<< count <<endl;
    return 0;
}

2.计算某字母出现次数_

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main(){
    string str;
    getline(cin,str);
    char a,b;
    cin >> a;
    if(islower(a)){
        b = a-32;
    }else{
        b = a+32;
    }
    
    map<char,int> mp;
    for(auto s : str){
        ++mp[s];
    }
    cout << mp[a] + mp[b];
    return 0;
}

3.明明的随机数_

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

using namespace std;

int main(){
    int N;
    while(cin >> N){
        int round[1000] = { 0 };
        while(N--){
            int n;
            cin >> n;
            round[n] = 1;
        }
        for(int i = 0; i < 1000; ++i){
            if(round[i]){
                cout << i <<endl;
            }
        }
    }
    return 0;
}

4.字符串分隔_

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

using namespace std;

int main() {
    string s;
    getline(cin, s);
    while (true) {
        int len = s.size();
        if (len <= 8) {
            s.insert(s.end(), 8-len, '0');
            cout << s << endl;
            if (!getline(cin, s)) break;
        }
        else {
            cout << s.substr(0, 8) << endl;
            s = s.substr(8, len-8);
        }
    }
    
    return 0;
}

5.进制转换_

#include<iostream>
#include<string>
#include<cmath>

using namespace std;
 int main()
 {
     string s;
     while(cin>>s)
     {
         int bit=0;
         int ans =0;
         for(int i=s.length()-1;i>1;i--)
         {
             if(s[i]>='0'&&s[i]<='9')
                 ans+=(s[i]-'0')*pow(16,bit++);
             else if(s[i]>='A'&&s[i]<='F')
                 ans+=(s[i]-'A'+10)*pow(16,bit++);
         }
         cout<<ans<<endl;
     }
     return 0;
 }

6.质数因子_

#include<iostream>
#include<string>
#include<cmath>

using namespace std;
 int main()
 {
     long input;
     cin >> input;
     for(int i = 2; i <= sqrt(input); ++i){
         if(input % i == 0){
             cout << i << ' ';
             input /= i;
             i = 1;
         }
     }
     if(input == 1){
         ;
     }else{
         cout << input << ' ';
     }
 }

7.取近似值_

#include<iostream>
#include<string>
#include<cmath>

using namespace std;
 int main()
 {
     float input;
     cin >> input;
     int n = (int)(input*10);
     if(n%10 >= 5){
         cout << n/10 + 1;
     }else{
         cout << n/10;
     }
 }

8.合并表记录_)

#include<iostream>
#include<map>
#include<cmath>
using namespace std;
 int main()
 {
     map<int,int> mp;
     int n;
     cin >> n;
     while(n--){
         int index,value;
         cin >> index >> value;
         mp[index] += value;
     }
     for(auto a : mp){
         cout << a.first << ' ' << a.second <<endl;
     }
 }

9.提取不重复的整数_

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

using namespace std;

int main()
{
    int input;
    cin >> input;
    vector<int> vec;
    map<int,int> mp;
    for(int i = 0; i < 32; ++i){
        int n = input % 10;
        input /= 10;
        vec.push_back(n);
        if(input == 0 ) break;
    }
    for(int v : vec){
        mp[v] = 1;
    }
    for(int i = 0; i < vec.size(); ++i){
        if(mp[vec[i]] == 1){
            cout<< vec[i];
            mp[vec[i]] = 0;
        }
    }
}

10.字符个数统计_

#include<iostream>
#include<map>
#include<string>

using namespace std;

 int main()
 {
     string str;
     getline(cin, str);
     map<char,int> mp;
     for(char s : str){
         if(s > 0 && s < 127){
             ++mp[s];
         }
     }
     cout << mp.size()<<endl;
     return 0;
 }

11.数字颠倒_

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

using namespace std;

int main()
{
    int input;
    cin >> input;
    vector<int> vec;
    for(int i = 0; i < 32; ++i){
        int n = input % 10;
        input /= 10;
        vec.push_back(n);
        if(input == 0 ) break;
    }
    for(auto v : vec){
        cout << v;
    }
}

12.字符串反转_

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

using namespace std;

int main()
{
    string str;
    getline(cin,str);
    reverse(str.begin(), str.end());
    cout << str;
}

13.句子逆序_

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

using namespace std;

int main()
{
    string str;
    getline(cin,str);
    reverse(str.begin(), str.end());
    int len = str.length();
    int count = 0;
    for(int i = 0; i < len; ++i){
        if(str[i] == ' '){
            reverse(str.begin()+i-count, str.begin()+i);
            count = 0;
        }else{
            count++;
        }
    }
    reverse(str.end()-count, str.end());
    cout << str;
}

14.字符串排序_

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

using namespace std;

static bool cmp(string a,string b){
    return a < b;
}

int main()
{
    int n;
    cin >> n;
    vector<string> vec;
    while(n--){
        string str;
        cin>>str;
        vec.push_back(str);
    }
    sort(vec.begin(),vec.end(),cmp);
    for(auto v : vec){
        cout << v <<endl;
    }
}

15.求int型正整数在内存中存储时1的个数_

#include<iostream>


using namespace std;

int main()
{
    int n;cin>>n;
    int count = 0;
    while(n){
        n = n & (n-1);
        count++;
    }
    cout<< count;
}

16.购物单_

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

int main()
{
    int N, m, i, j; //m为商品总数 N为总钱数
    cin>>N>>m;
    int val[60][3] = { 0 }; //商品价格*重要程度
    int price[60][3] = { 0 }; //商品单价
    //int q[60][3] = {0}; //表示主件还是附件 q=0 表示主件 q=m 表示为主见m得附件
    int sum[3200] = { 0 };
    int v, p, q;//v表示物品的价格;p表示物品的重要程度;
    //q表示是主件还是附件,q=0 表示主件,q=j 表示为主件j的附件
    for (i = 1; i <= m; i++)
    {
        cin>>v>>p>>q;//输入每一行的数据
        if (q != 0)//表示该行的物品为附件
        {
            if (val[q][1] == 0)//表示该物品为物品q的附件一
            {
                val[q][1] = v * p;
                price[q][1] = v;
            }
            else //表示该物品为物品q的附件二
            {
                val[q][2] = v * p;
                price[q][2] = v;
            }
        }
        else//表示该行的物品为主件
        {
            val[i][0] = v * p;
            price[i][0] = v;
        }
    }
    
    
    for (i = 1; i <= m; i++)
    {
        for (j = N / 10; j >= price[i][0] / 10; j--)
        {
            //无附件
            sum[j] = max(sum[j - price[i][0] / 10] + val[i][0], sum[j]);
            //主件+附件1
            int total = price[i][0] / 10 + price[i][1] / 10;
            if (val[i][1] != 0)
            {
                if (j >= total)
                    sum[j] = max(sum[j - total] + val[i][0] + val[i][1], sum[j]);
            }
            //主件+附件1+附件2
            total = price[i][0] / 10 + price[i][1] / 10 + price[i][2] / 10;
            if (val[i][2] != 0)
            {
                if (j >= total)
                    sum[j] = max(sum[j - total] + val[i][0] + val[i][1] + val[i][2], sum[j]);
            }
        }
    }
    cout<<sum[N/10]<<endl;
    return 0;
}

17.坐标移动_

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

int main()
{
    string str;
    
    while(cin >> str)
    {
        //初始化坐标
        int x = 0, y = 0;
        //存储单步操作
        vector<string> steps;
        
        //把字符串拆分
        int wordlen = 0;
        for(int i = 0; i < str.size(); ++i)
        {
            while(str[i] != ';')
                wordlen ++, i ++;
            steps.push_back(str.substr(i - wordlen, wordlen));
            wordlen = 0;
        }
        //分解成功
        //for(auto x : steps) cout << x << endl;
        
        //对单个steps执行坐标变换
        for(int i = 0; i < steps.size(); ++i)
        {
            int num = 0;
            //长度3  A10
            if(steps[i].length() == 3 && steps[i][1] <= '9' && steps[i][1] >= '0' &&  steps[i][2] <= '9' && steps[i][2] >= '0')
                num = (steps[i][1] - '0') * 10 + steps[i][2] - '0';
            //长度2  A5
            if(steps[i].length() == 2 && steps[i][1] <= '9' && steps[i][1] >= '0')
                num = steps[i][1] - '0';
            
            switch(steps[i][0])//ASDW
            {
                case 'A': x -= num; break;
                case 'D': x += num; break;
                case 'W': y += num; break;
                case 'S': y -= num; break;
                default: break;
            }
        }
        cout << x << ',' << y << endl;
    }
    
    return 0;
}

19.简单错误记录_

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

using namespace std;

int main(){
    string str1,str2;
    vector<string> vec;
    map<string,int> mp;
    while(cin >> str1 >> str2){
        int len = str1.length();
        string file;
        int count = 16;
        int i = len - 1;
        for(; i >= 0; --i){
            if(count-- == 0 || str1[i] == '\\') break;
        }
        file = str1.substr(i+1,len);
        string result = file + ' ' + str2;
        if(mp.find(result) == mp.end()){
            ++mp[result];
            vec.push_back(result);
        }else{
            ++mp[result];
        }
    }
    int size = vec.size();
    if(size >= 8){
        for(int i = vec.size()-8; i < vec.size(); ++i){
            cout << vec[i] << ' ' << mp[vec[i]] <<endl;
        }
    }else{
        for(auto v : vec){
            cout << v << ' ' << mp[v] <<endl;
        }
    }
    return 0;
}

20.密码验证合格程序_

#include <iostream>
#include <string>

using namespace std;

int main(){
    string str;
    while(cin >>str){
        bool flag = true;
        int len = str.length();
        if(len <=8){ cout << "NG"<<endl; flag = false;}
        int number = 0,lower = 0,upper = 0,symbol = 0;
        for(int i = 0; i < len; ++i){
            for(int j = i + 2; j < len; ++j){
                if(str[i] == str[j]){
                    if(str[i+1] == str[j+1] && str[i+2] == str[j+2] && flag){
                        cout << "NG"<<endl;
                        flag = false;
                    }
                }
            }
            if(isdigit(str[i])){ number = 1; }
            else if(islower(str[i])){ lower = 1; }
            else if(isupper(str[i])){ upper = 1; }
            else{ symbol = 1;}
        }
        if(number + lower + upper + symbol < 3 && flag){ cout << "NG"<<endl;flag = false;}
        if(flag){ cout << "OK"<<endl;}
    }
    
    return 0;
}

21.简单密码_

#include <iostream>
#include <string>

using namespace std;

int main(){
    string str;
    while(cin >> str){
        for(int i = 0; i < str.length(); ++i){
            if(islower(str[i])){
                int a = str[i];
                if(a >= 97 && a <=99){str[i] = '2';}
                else if(a >= 100 && a <=102){str[i] = '3';}
                else if(a >= 103 && a <=105){str[i] = '4';}
                else if(a >= 106 && a <=108){str[i] = '5';}
                else if(a >= 109 && a <=111){str[i] = '6';}
                else if(a >= 112 && a <=115){str[i] = '7';}
                else if(a >= 116 && a <=118){str[i] = '8';}
                else if(a >= 119 && a <=122){str[i] = '9';}
            }else if(isupper(str[i])){
                if(str[i] == 'Z'){
                    str[i] = 'a';
                }else{
                    str[i] = str[i] + 33;
                }
            }
        }
        cout << str << endl;
    }
    
    return 0;
}

22.汽水瓶_

#include <iostream>

using namespace std;

int main(){
    int n;
    while(cin >> n){
        if(n == 0){ break;}
        int count = 0;
        while( n >= 3){
            int junk = n/3;
            int rem = n%3;
            n = junk + rem;
            count += junk;
        }
        if(n == 2){
            count++;
        }
        cout<<count<<endl;
    }
    
    return 0;
    
}

23.删除字符串中出现次数最少的字符_

#include <iostream>
#include <map>
#include <limits.h>

using namespace std;

int main(){
    
    string str;
    while(cin >> str){
        map<char,int> mp;
        for(auto s : str){
            ++mp[s];
        }
        int min_mp = INT_MAX;
        for(auto s : str){
            min_mp = min(min_mp,mp[s]);
        }
        string tmp = str;
        for(int i = 0; i < str.length();++i){
            if(mp[str[i]] == min_mp){
                int pos = tmp.find(str[i]);
                tmp.erase(pos,1);
            }
        }
        cout<< tmp<<endl;
    }
    return 0;
}

25.数据分类处理_

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

using namespace std;

int main()
{
    int m, n;
    while (cin >> m)
    {
        vector<int> I;
        vector<int> R;
        while (m--)
        {
            int val;
            cin >> val;
            I.push_back(val);
        }
        int Isize = I.size();
        cin >> n;
        while (n--)
        {
            int val;
            cin >> val;
            R.push_back(val);
        }
        sort(R.begin(), R.end());
        auto unique_end = unique(R.begin(), R.end());
        R.erase(unique_end, R.end());
        int Rsize = R.size();
        vector<int> output;
        for (int i = 0; i < Rsize; ++i)
        {
            vector<int> tmp;
            for (int j = 0; j < Isize; ++j)
            {
                if (string::npos != to_string(I[j]).find(to_string(R[i])))
                {
                    tmp.push_back(j);
                    tmp.push_back(I[j]);
                }
            }
            if (!tmp.empty())
            {
                output.push_back(R[i]);
                output.push_back(tmp.size() / 2);
                output.insert(output.end(), tmp.begin(), tmp.end());
            }
        }
        if (!output.empty())
        {
            cout << output.size() << " ";
            for (int i = 0; i < output.size() - 1; ++i)
            { // 注意最后一个数字后不跟空格, 而是换行
                cout << output[i] << " ";
            }
            cout << output[output.size() - 1] << endl;
        }
    }
    return 0;
}

26.字符串排序_

#include <iostream>
#include <vector>
using namespace std;
string String_Sorting(string str)
{
    int len = str.size();
    vector <char> vec;
    for (int j = 0; j < 26; j++)
    {
        for (int i = 0; i < len; i++)
        {
            if ((str[i] - 'a' == j) || (str[i] - 'A' == j))
            {
                vec.push_back(str[i]); //将符合规则的字母字符先后写入向量
            }
        }
    }
    //规则三:非英文字母的其它字符保持原来的位置。
    for(int i = 0,k = 0;(i < len) && (k < vec.size()); i++)
    {
        if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
        {
            str[i] = vec[k++];
        }
    }
    return str; //返回按规则排序好后的字符串
}
//主函数
int main()
{
    string str;
    while (getline(cin, str))
    {
        cout << String_Sorting(str) << endl;
    }
    return 0;
}

27.查找兄弟单词_

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

using namespace std;

bool is_brother(string a,string b){
    sort(a.begin(),a.end());
    sort(b.begin(),b.end());
    return a == b;
}

int main(){
    
    int n;
    while(cin>>n){
        vector<string> vec;
        int k;
        while(n--){
            string str;
            cin >> str;
            vec.push_back(str);
        }
        string x;
        cin >> x >> k;
        sort(vec.begin(),vec.end());
        string xx;
        int count = 0;
        for(auto v : vec){
            if(v!=x){
                if(is_brother(x, v)){
                    if(++count == k){
                        xx = v;
                    }
                }
            }
        }
        cout << count << endl;
        if(count >= k){
            cout << xx << endl;
        }
    }
    return 0;
}

29.字符串加解密_

#include <iostream>

using namespace std;

int main(){
    string str,rhs;
    while(cin >> str >> rhs){
        int len = str.length();
        for(int i = 0; i < len; ++i){
            if(isalpha(str[i])){
                if(islower(str[i])){
                    if(str[i] == 'z'){ str[i] = 'A';}
                    else{ str[i] = str[i] -31;}
                }else{
                    if(str[i] == 'Z'){ str[i] = 'a';}
                    else{ str[i] = str[i] + 33;}
                }
            }
            if(isdigit(str[i])){
                if(str[i] == '9'){str[i] = '0';}
                else{str[i] += 1;}
            }
        }
        cout << str <<endl;
        
        int rlen = rhs.length();
        for(int i = 0; i < rlen; ++i){
            if(isalpha(rhs[i])){
                if(islower(rhs[i])){
                    if(rhs[i] == 'a'){rhs[i] = 'Z';}
                    else{rhs[i] = rhs[i] - 33;}
                }else{
                    if(rhs[i] == 'A'){rhs[i] = 'z';}
                    else{rhs[i] = rhs[i] + 31;}
                }
            }
            if(isdigit(rhs[i])){
                if(rhs[i] == '0'){rhs[i] = '9';}
                else{rhs[i] -= 1;}
            }
        }
        cout << rhs <<endl;
    }
    
    return 0;
}

30.字符串合并处理_

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

using namespace std;

string sortstr(string str){
    vector<char> str1,str2;
    int len = str.length();
    for(int i = 0; i < len; ++i){
        if(i % 2 == 0){
            str1.push_back(str[i]);
        }else{
            str2.push_back(str[i]);
        }
    }
    sort(str1.begin(),str1.end());
    sort(str2.begin(),str2.end());
    int j1 = 0,j2 = 0;
    for(int i = 0; i < len; ++i){
        if(i % 2 == 0){
            str[i] = str1[j1++];
        }else{
            str[i] = str2[j2++];
        }
    }
    return str;
}

int main(){
    string str1,str2;
    char Intput[] = {"0123456789abcdefABCDEF"};
    char Output[] = {"084C2A6E195D3B7F5D3B7F"};
    while(cin >> str1 >> str2){
        string strs = str1 + str2;
        string str = sortstr(strs);
        for(int i = 0; i < str.length(); ++i){
            if(isdigit(str[i])){
                str[i] = Output[str[i] - '0'];
            }else if(str[i] >= 'A' && str[i] <= 'F'){
                str[i] = Output[str[i] - 'A' +16];
            }else if(str[i] >= 'a' && str[i] <= 'f'){
                str[i] = Output[str[i] - 'a' +10];
            }
        }
        cout << str << endl;
    }
    
    
    return 0;
}

32.密码截取_

#include <iostream>

using namespace std;

int main(){
    string str;
    while(cin >> str){
        int len = str.length();
        int maxlen = 0;
        for(int i = 0; i < len; ++i){
            int l = i - 1,r = i;
            while(str[l] == str[r] && l >=0 && r < len){
                l--;r++;
            }
            maxlen = max(maxlen,r-l-1);
            l = i - 1; r = i + 1;
            while(str[l] == str[r] && l >=0 && r < len){
                l--;r++;
            }
            maxlen = max(maxlen,r-l-1);
        }
        cout << maxlen << endl;
    }
    return 0;
}

33.整数与IP地址间的转换_

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

using namespace std;

void strTolong(string str){
    int len = str.length();
    string tmp;
    vector<int> vec;
    for(int i = 0; i < len; ++i){
        if(str[i] == '.'){
            vec.push_back(atoi(tmp.c_str()));
            tmp.clear();
        }else{
            tmp += str[i];
        }
        if(i == len-1){
            vec.push_back(atoi(tmp.c_str()));
            tmp.clear();
        }
    }
    long ipNum = 0;
    for(auto v : vec){
        ipNum = ipNum * 256 + v;
    }
    cout << ipNum << endl;
}

void longTostr(long ipNum){
    stack<int> s;
    while(ipNum > 0){
        s.push(ipNum % 256);
        ipNum /= 256;
    }
    string str;
    while(s.size() > 0){
        if (s.size() == 1) {
            cout << s.top() << endl ;
        }else
            cout << s.top() << ".";
        s.pop();
    }
}

int main(){
    string str;
    long ipNum;
    while(cin >> str >> ipNum ){
        strTolong(str);
        longTostr(ipNum);
    }
    return 0;
}

34.图片整理_

#include <iostream>
#include <algorithm>

using namespace std;

int main(){
    string str;
    while(cin >> str){
        sort(str.begin(),str.end());
        cout << str << endl;
    }
    return 0;
}

35.蛇形矩阵_

#include <iostream>

using namespace std;

int main(){
    int N;
    while(cin >> N){
        int n = 1;
        for(int i = 1; i <= N; ++i){
            cout << n;
            int tmp = n;
            for(int j = i+1; j <= N; ++j){
                tmp += j;
                cout << ' ' << tmp;
            }
            n += i;
            cout << endl;
        }
    }
    
    return 0;
}

36.字符串加密_

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

int main()
{
    
    string s,key;
    while(cin>>s>>key){
        vector<char> miyao;
        for(int i=0;i<s.size();i++){
            if(s[i]>='a'&&s[i]<='z') s[i]=s[i]+'A'-'a';
            auto it = find(miyao.begin(), miyao.end(), s[i]);
            if(it==miyao.end()){//说明不重复
                miyao.push_back(s[i]);
            }
        }
        //将A到Z剩余字符存入密钥
        for(char i='A';i<='Z';i++){
            auto it = find(miyao.begin(), miyao.end(), i);
            if(it==miyao.end())  miyao.push_back(i);
        }
        char out;
        //编码,大小写统一对应
        for(int i=0;i<key.size();i++){
            if(key[i]>='a'&&key[i]<='z'){
                out=miyao[key[i]-'a']+32;
            }else {
                out=miyao[key[i]-'A'];
            }
            cout<<out;
        }
        cout<<endl;
        
    }
    
    return 0;
    
}

37.统计每个月兔子的总数_

#include <iostream>

using namespace std;

int main(){
    int mon;
    while(cin >> mon){
        int r1 = 1,r2 = 0,r3 = 0;
        while(--mon){
            r3 += r2; //处于第三个月的兔子 即可以生兔子
            r2 = r1; //处于第二个月的兔子
            r1 = r3; //处于第一个月的兔子
        }
        cout << r1 + r2 + r3 << endl;
    }
    
    return 0;
}

38.求小球落地5次后所经历的路程和第5次反弹的高度_

#include <iostream>

using namespace std;

int main(){
    double start;
    while(cin >> start){
        int n = 4;
        double total = start,last;
        while(n--){
            total += start;
            start /= 2;
        }
        last = start / 2;
        cout << total << endl << last << endl;
    }
    
    return 0;
}

40.统计字符_

#include <iostream>

using namespace std;

int main(){
    string str;
    while(getline(cin,str)){
        int len = str.length();
        int l = 0,n = 0,b = 0,o = 0;
        for(auto s : str){
            if(isalpha(s)){
                l++;
            }
            else if(isalnum(s)){
                n++;
            }
            else if(s == ' '){
                b++;
            }
            else{
                o++;
            }
        }
        cout << l << endl << b << endl << n << endl << o << endl;
    }
    return 0;
}

41.称砝码_

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

int numofdif(vector<int> w,vector<int> n){
    int size = w.size();
    int maxw = 0;
    
    for(int i = 0; i < size; ++i){
        maxw += w[i] * n[i];
    }
    int v[20000] = {0};
    v[0] = 1;
    for(int i = 0; i < size; ++i){
        for(int j = maxw; j >=0; --j){
            for(int k = 1; k <= n[i]; ++k){
                if(v[j] == 1 && j+k*w[i] <= maxw){
                    v[j+k*w[i]] = 1;
                }
            }
        }
    }
    int count = 0;
    for(int i = 0; i <= maxw; ++i){
        if(v[i] == 1){
            count++;
        }
    }
    return count;
}

int main(){
    int n;
    while(cin >> n){
        vector<int> weight;
        vector<int> num;
        int m = n;
        while(m--){
            int w;
            cin >> w;
            weight.push_back(w);
        }
        m = n;
        while(m--){
            int number;
            cin >> number;
            num.push_back(number);
        }
        cout << numofdif(weight,num) << endl;
    }
    return 0;
}

42.学英语_

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

unordered_map<int, string> numbers = 
{
    {0,""},
    {1, "one"},{2, "two"},{3,"three"},{4,"four"},{5,"five"},{6,"six"},{7,"seven"},
    {8,"eight"},{9,"nine"},{10,"ten"},
    {11,"eleven"},{12,"twelve"},{13,"thirteen"},{14,"fourteen"},{15, "fifteen"},
    {16,"sixteen"},{17,"seventeen"},{18,"eighteen"},{19,"ninteen"},
    {20, "twenty"},{30,"thirty"},{40,"forty"},{50,"fifty"},{60,"sixty"},
    {70, "seventy"},{80,"eighty"},{90,"ninety"}
};

string output(int in)
{
    string out = "";
    if( in >= 100 )
    {
        out = numbers[in/100] + " hundred";
        if(in%100 != 0)
        {
            out += " and ";
        }
    }
    in = in%100;
    if(in >= 20)
    {
        out += numbers[(in/10)*10];
        if(in %10!=0)
        {
            out+=" " + numbers[in%10];
        }
    }
    else
    {
        out+=numbers[in];
    }
    return out;
}

int main()
{
    vector<int> inputs;
    int single_input;
    while(cin >> single_input)
    {
        inputs.emplace_back(single_input);
    }
    for(auto &input:inputs)
    {
        string out_str = "";
        if(input >=1000000000 || input <=0) cout << "error" << endl;
        if(input >= 1000000)
        {
            out_str += output(input/1000000) + " million";
            if(input % 1000000 != 0) out_str += " ";
        }
        input = input % 1000000;
        if(input >= 1000)
        {
            out_str += output(input/1000) + " thousand";
            if(input % 1000 != 0) out_str += " ";
        }
        input = input % 1000;
        out_str += output(input);
        cout << out_str << endl;
    }
    
}

43.迷宫问题_

#include<bits/stdc++.h>

using namespace std;

vector<pair<int,int>> ans;

void dfs(vector<vector<int>> &dp,int row,int col,int a,int b,vector<pair<int,int>> &temp)
{
    temp.push_back({a,b});
    dp[a][b] = 1;
    if(a == row - 1 && b == col - 1)
    {
        ans = temp;
        return;
    }
    
    if(a+1 < row && dp[a+1][b] == 0) dfs(dp,row,col,a+1,b,temp);//向下
    if(b+1 < col && dp[a][b+1] == 0) dfs(dp,row,col,a,b+1,temp);//向右
    if(a-1 >= 0 && dp[a-1][b] == 0) dfs(dp,row,col,a-1,b,temp);//向上
    if(b-1 >= 0 && dp[a][b-1] == 0) dfs(dp,row,col,a,b-1,temp);//向左
    
    temp.pop_back();
    dp[a][b] = 0;
}

int main()
{
    int n,m;
    while(cin >> n >> m)
    {
        vector<vector<int>> dp(n,vector<int>(m,0));
        for(int i = 0;i < n;i++)
        {
            for(int j = 0;j < m;j++)
            {
                cin >> dp[i][j];
            }
        }
        vector<pair<int,int>> temp;
        dfs(dp,n,m,0,0,temp);
        for(auto it:ans)
        {
            cout << '(' << it.first << ',' << it.second << ')' << endl;
        }
        ans.clear();
    }
    return 0;
}

45.名字的漂亮度_

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

int main(){
    int N;
    while(cin >> N){
        while(N--){
            string str;
            cin >> str;
            map<char,int> mp;
            for(auto s : str){
                ++mp[s];
            }
            vector<int> vec;
            sort(str.begin(),str.end());
            auto unique_end = unique(str.begin(), str.end());
            str.erase(unique_end, str.end());
            for(int i = 0; i < str.length(); ++i){
                vec.push_back(mp[str[i]]);
            }
            sort(vec.begin(),vec.end());
            int sum = 0;
            int size = vec.size();
            int n = 27-size;
            for(int i = 0; i < size; ++i){
                sum += vec[i] * n;
                n++;
            }
            cout << sum <<endl;
        }
    }
    return 0;
}

46.截取字符串_

#include <iostream>

using namespace std;

int main(){
    string str;
    int k;
    while(cin >> str >> k){
        string tmp = str.substr(0,k);
        cout << tmp << endl;
    }
    return 0;
}

48.从单向链表中删除指定值的节点_

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

int main(){
    int n,m;
    while(cin >> n >> m){
        n = n - 1;
        vector<int> vec;
        vec.push_back(m);
        while(n--){
            int a,b;
            cin >> a >> b;
            auto it = find(vec.begin(),vec.end(),b);
            vec.insert(it+1, a);
        }
        int del;cin >> del;
        auto itd = find(vec.begin(),vec.end(),del);
        vec.erase(itd);
        for(auto v : vec){
            cout << v << ' ';
        }
        cout << endl;
    }
    return 0;
}

51.输出单向链表中倒数第k个结点_

#include <iostream>

using namespace std;

struct ListNode{
    int m_nKey;
    ListNode* m_pNext;
    ListNode (int value) :m_nKey(value),
        m_pNext(nullptr) {}
};

ListNode* kval(ListNode* head){
    ListNode* fast = head;
    ListNode* low = head;
    int k; cin >> k;
    if(k>0){
        while(k--){
            fast = fast->m_pNext;
        }
        while(fast){
            fast = fast->m_pNext;
            low = low->m_pNext;
        }
        return low;
    }else{
        return nullptr;
    }
}

int main(){
    int n;
    while(cin >> n){
        ListNode* head = nullptr;
        ListNode* p;
        while(n--){
            int val; cin >> val;
            ListNode *tmp = new ListNode(val);
            if(head == nullptr){
                head = tmp;
                p = tmp;
            }else{
                p->m_pNext = tmp;
                p = tmp;
            }
        }
        ListNode* re = kval(head);
        if(re){
            cout << re->m_nKey << endl;
        }else{
            cout << 0 <<endl;
        }
    }
    return 0;
}

52.计算字符串的距离_

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

int main(){
    string str1,str2;
    while(cin >> str1 >> str2){
        if(str1 == str2){
            cout << 0 << endl;
        }else{
            vector<vector<int>> dp(str1.size()+1, vector<int>(str2.size()+1, 0));
            for(int i = 1; i <= str2.length(); ++i) { dp[0][i] = i;}
            for(int i = 1; i <= str1.length(); ++i) { dp[i][0] = i;}
            for(int i = 1; i <= str1.length();++i){
                for(int j = 1; j <= str2.length(); ++j){
                    if(str1[i-1] == str2[j-1]){
                        dp[i][j] = dp[i-1][j-1];
                    }else{
                        dp[i][j] = min(dp[i-1][j-1],min(dp[i][j-1],dp[i-1][j])) + 1;
                    }
//                     int min1 = min(dp[i-1][j],dp[i][j-1]) + 1;
//                     dp[i][j] = min((str1[i - 1] == str2[j - 1] ? 0 : 1) + dp[i - 1][j - 1], min1);
                }
            }//dp[i-1][j],dp[i][j-1],dp[i-1][j-1]
            cout << dp[str1.length()][str2.length()] <<endl;
        }
    }
    
    return 0;
}

53.杨辉三角的变形_

#include <iostream>

using namespace std;

int main(){
    int n;
    while(cin >> n){
        if(n == 1 || n == 2){
            cout << -1 << endl;
        }else if(n % 4 == 1 || n % 4 == 3){
            cout << 2 << endl;
        }else if(n % 4 == 0){
            cout << 3 << endl;
        }else if(n % 4 == 2){
            cout << 4 << endl;
        }
    }
    return 0;
}

54.表达式求值_

#include<iostream>
#include<string>
#include<sstream>
#include<stack>
using namespace std;
string mp="+-*/)]}";
//判断运算符的优先级
bool priority(char c1,char c2)
{
    if(c1=='(')
        return false;
    else if((c1=='+'||c1=='-')&&(c2=='*'||c2=='/'))
        return false;
    else
        return true;
}
//运算加减乘除
void calc(stack<double>&st,stack<char>&so)
{
    double b=st.top();
    st.pop();
    double a=st.top();
    st.pop();
    char c=so.top();
    so.pop();
    if(c=='+')
        a=a+b;
    else if(c=='-')
        a=a-b;
    else if(c=='*')
        a=a*b;
    else if(c=='/')
        a=a/b;
    st.push(a);
    return;
}
int main()
{
    string s;
    while(getline(cin,s))
    {
        stack<double>st;
        stack<char>so;
        so.push('(');
        s+=')';
        int l=s.size();
        bool next=false;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]=='{'||s[i]=='('||s[i]=='[')
            {
                so.push('(');
            }
            else if(s[i]=='}'||s[i]==']'||s[i]==')')
            {
                while(so.top()!='(')
                    calc(st,so);
                so.pop();
            }
            else if(next)
            {
                while(priority(so.top(),s[i]))
                    calc(st,so);
                so.push(s[i]);
                next=false;
            }
            else
            {
                int j=i;
                if(s[j]=='-'||s[j]=='+')
                    i++;
                while(mp.find(s[i])==mp.npos)
                    i++;
                string t=s.substr(j,i-j);
                st.push((double)stoi(t));
                i--;
                next=true;
            }
           
        }
         cout<<st.top()<<endl;
    }
        return 0;
    }

55.挑7_

#include <iostream>

using namespace std;

int main(){
    int n;
    while(cin >> n){
        int count = 0;
        for(int i = 1; i <= n; ++i){
            if(i % 7 == 0){ count++; continue;}
            int j = i;
            while(j > 0){
                if(j % 10 == 7){
                    count++;
                    break;
                }
                j /= 10;
            }
        }
        cout << count << endl;
    }
    
    return 0;
}

56.完全数计算_

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

int main(){
    int n;
    while(cin >>n){
        int count = 0;
        stack<int> s;
        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= i/2; ++j){
                if(i % j == 0){
                    s.push(j);
                }
            }
            int num = 0;
            while(!s.empty()){
                num += s.top();
                s.pop();
            }
            if(num  == i){
                count++;
            }
        }
        cout << count <<endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值