当前位置:   article > 正文

牛客——华为机试_华为机试合唱队python解析

华为机试合唱队python解析

T8. 合并表记录

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

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> k;
    unordered_map<int, int> hash;
    while(n--)
    {
        int index, value;
        cin >> index >> value;
        k.push_back(index);
        hash[index] += value;
    }
    sort(k.begin(), k.end());
    k.erase(unique(k.begin(), k.end()), k.end());
    for(auto x : k)
        cout << x << ' ' << hash[x] << endl;
    
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

T15. 求int型数据在内存中存储时1的个数

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int num = 0;
    for(int i = 0; i <= 32; i++)
    {
        if(n & (1 << i))
            num++;
    }
    
    cout << num << endl;
    
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
#include <iostream>
 
using namespace std;
 
int main()
{
    int n;
    cin >> n;
    int num = 0;
    while(n)
    {
        if(n % 2 == 1)
            num++;
        n /= 2;
    }
     
    cout << num << endl;
     
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

T16. 购物单

#include <iostream>
#include <vector>

using namespace std;
//总钱数小于32000
int dp[32000];

int main()
{
    int N, m;
    cin >> N >> m;
    
    //定义几个数组
    vector<int> zj(m+1), zjvw(m+1), fj1(m+1), fj1vw(m+1), fj2(m+1), fj2vw(m+1);
    
    for(int i = 1; i <= m; ++i)
    {
        int v, p, q;
        cin >> v >> p >> q;
        //主件
        if(q == 0)
        {
            zj[i] = v;
            zjvw[i] = v * p;
        }
        //附件1
        //不是i
        //q值对应的是从属的主件
        else if(fj1[q] == 0)
        {
            fj1[q] = v;
            fj1vw[q] = v * p;
        }
        //附件2
        else if(fj2[q] == 0)
        {
            fj2[q] = v;
            fj2vw[q] = v * p;
        }
    }
    
    //动态规划
    for(int i = 1; i <= m; ++i)
    {
        for(int j = N; j >= 1; j--)
        {
            //只要主件
            if(j >= zj[i])
                dp[j] = max(dp[j], dp[j-zj[i]] + zjvw[i]);
            //主件+附件1
            if(j >= zj[i] + fj1[i])
                dp[j] = max(dp[j], dp[j-zj[i]-fj1[i]] + zjvw[i] +fj1vw[i]);
            //主件+附件2
            if(j >= zj[i] + fj2[i])
                dp[j] = max(dp[j], dp[j-zj[i]-fj2[i]] + zjvw[i] +fj2vw[i]);
            //主件+附件1+附件2
            if(j >= zj[i] + fj1[i] + fj2[i])
                dp[j] = max(dp[j], dp[j-zj[i]-fj1[i]-fj2[i]] + zjvw[i] +fj1vw[i] +fj2vw[i]);
        }
    }
    cout << dp[N] << endl;
    
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

T17. 坐标移动

#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;
        }
        
        //对单个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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

T18. 识别有效的IP地址和掩码并进行分类统计

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

using namespace std;

int strtoint(string str)
{
    //68
    int sum = 0;
    for(int i = 0; i < str.size(); i ++)
        sum = sum * 10 + str[i] - '0';
    return sum;
}

vector<int> toint(string str)
{
    vector<int> res;
    //10.70.44.68
    int len = 0;
    for(int i = 0; i < str.size(); i ++)
    {
        while(str[i] != '.' && i < str.size())
            i ++, len ++;
        string nums = str.substr(i - len, len);
        int num = strtoint(nums);
        res.push_back(num);
        len = 0;
    }
    return res;
}

bool ismask(vector<int> mask)
{
    //255 254 255 0
    //长度 == 4
    //1在前
    if(mask.size() != 4)
        return false;
    
    if(mask[0] == 255)
    {
        if(mask[1] == 255)
        {
            if(mask[2] == 255)
            {
                //8位2进制
                //7个1  6个1 .。。 1个1  + 000
                if(mask[3] == 254 || mask[3] == 252 || mask[3] == 248 || mask[3] == 240 || mask[3] == 224 || mask[3] == 192 || mask[3] == 128 || mask[3] == 0)
                    return true;
                else
                    return false;
            }
            else{
                if(mask[2] == 254 || mask[2] == 252 || mask[2] == 248 || mask[2] == 240 || mask[2] == 224 || mask[2] == 192 || mask[2] == 128 || mask[2] == 0)
                    if(mask[3] == 0)
                        return true;
                    else
                        return false;
            }
        }
        else{
            if(mask[1] == 254 || mask[1] == 252 || mask[1] == 248 || mask[1] == 240 || mask[1] == 224 || mask[1] == 192 || mask[1] == 128 || mask[1] == 0)
                if(mask[3] == 0 && mask[2] == 0)
                    return true;
                else
                    return false;
            
        }
    }
    else{
        if(mask[0] == 254 || mask[0] == 252 || mask[0] == 248 || mask[0] == 240 || mask[0] == 224 || mask[0] == 192 || mask[0] == 128)
            if(mask[1] == 0 && mask[2] == 2 && mask[3] == 0)
                return true;
            else
                return false;
    }
    return false;
}

int main()
{
    vector<int> res(7,0);
    string str;
    while(cin >> str)
    {
        //10.70.44.68~255.254.255.0
        //分割ip mask
        string ips, masks;
        vector<string> temp;
        int len = 0;
        for(int i = 0; i < str.size(); i ++)
        {
            while(str[i] != '~' && i < str.size())
                i ++, len ++;
            string s = str.substr(i - len, len);
            temp.push_back(s);
            len = 0;
        }
        ips = temp[0];
        masks = temp[1];
        //cout << ips << ' ' << masks << endl;//分割成功
        
        //字符串 二进制 比较大小 转换成int
        vector<int> ip, mask;
        ip = toint(ips);
        mask = toint(masks);
        
        //子网掩码 11111 0000
        if(ismask(mask))
        {
            //判断ip
            
            //A类地址1.0.0.0~126.255.255.255;
            //B类地址128.0.0.0~191.255.255.255;
            //C类地址192.0.0.0~223.255.255.255;
            //D类地址224.0.0.0~239.255.255.255;
            //E类地址240.0.0.0~255.255.255.255
            //私网IP范围是
            //10.0.0.0~10.255.255.255
            //172.16.0.0~172.31.255.255
            //192.168.0.0~192.168.255.255
            if(ip[1] >= 0 && ip[1] <= 255 && ip[2] >= 0 && ip[2] <= 255 && ip[3] >= 0 && ip[3]<+ 255)
            {
                if(ip[0] >= 1 && ip[0] <=126)//A
                {
                    res[0] ++;
                    if(ip[0] == 10)//私有
                        res[6]++;
                }
                else if(ip[0] >= 128 && ip[0] <= 191)//b
                {
                    res[1] ++;
                    if(ip[0] == 172 && ip[1] >= 16 && ip[1] <= 31)//私有
                        res[6]++;
                }
                else if(ip[0] >= 192 && ip[0] <= 223)//c
                {
                    res[2] ++;
                    if(ip[0] == 192 && ip[1] == 168)//私有
                        res[6]++;
                }
                else if(ip[0] >= 224 && ip[0] <= 239)//d
                    res[3] ++;
                else if(ip[0] >= 240 && ip[0] <= 255)//e
                    res[4] ++;
            }
        }
        else
            res[5] ++;
    }
    
    //A~E  0~4
    //错误 5
    //私有 6
    cout << res[0] << ' ' << res[1] << ' ' << res[2] << ' ' << res[3] << ' ' << res[4] << ' ' << res[5] << ' '<< res[6] << endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158

T19. 简单错误记录

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

//文件名 行数 次数
struct record
{
    char s[100];
    int col;
    int count;
};

int main()
{
    char filename[100];
    int col;
    int cnt = 0;//总记录条数 8
    
    record rec[10000];
    while(cin >> filename >> col)
    {
        //分隔 /路径
        //E:\V1R2\product\fpgadrive.c   1325
        //fpgadrive.c
        char *p = strrchr(filename, '\\');
        p++;
        if(strlen(p) > 16)//截取后16位
            p = p + (strlen(p) - 16);
        
        int flag = 0;
        
        //查重
        for(int i = 0; i < cnt; ++i)
        {
            if(strcmp(rec[i].s, p) == 0 && rec[i].col == col)
            {
                rec[i].count ++;
                flag = 1;
                break;
            }
        }
        
        if(!flag)//没有当前错误记录,全部插进去
        {
            strcpy(rec[cnt].s, p);
            rec[cnt].col = col;
            rec[cnt].count = 1;
            cnt ++;
        }
    }
    
    //输出8条
    int i = 0;
    if(cnt > 8)
        i = cnt - 8;
    else i = 0;
    
    for(; i < cnt; ++i)
    {
        cout << rec[i].s << " " << rec[i].col << " " << rec[i].count << endl;
        
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

T20. 密码验证合格程序

#include <iostream> 
#include <vector>
#include <string.h>

using namespace std;

int main()
{
    vector<string> res;//OK NG
    string str;
    
    while(cin >> str)
    {
        int num[4] = {0};
        //1.长度超过8位
        if(str.size() <= 8)
            res.push_back("NG");
        else
        {
            //2.包括大小写字母.数字.其它符号,以上四种至少三种
            for(auto it = str.begin(); it != str.end(); it++)
            {
                if((*it) >= 'A' && (*it) <= 'Z') num[0] = 1;
                else if((*it) >= 'a' && (*it) <= 'z') num[1] = 1;
                else if((*it) >= '0' && (*it) <= '9') num[2] = 1;
                else num[3] = 1;
            }
            
            int count = 0;
            for(int i = 0; i < 4; i++)
            {
                count += num[i];
            }
            if(count < 3)
                res.push_back("NG");
            else
            {
                //3.不能有相同长度大于等于2的子串重复
                //查找字符串a是否包含子串b,不是用strA.find(strB) > 0
                //而是strA.find(strB) != string :: npos;
                //其中string :: npos是个特殊值,说明查找没有匹配
                //string::size_type pos = strA.find(strB);
                string::size_type pos;
                for(int i = 0; i < str.size(); i++)
                {
                    string sub = str.substr(i, 3);
                    pos = str.find(sub, i + 3);
                    if(pos != string::npos)
                        break;
                }
                if(pos == string::npos)
                    res.push_back("OK");
                else
                    res.push_back("NG");
                
            }
        }
    }
   
    for(auto x : res)
        cout << x << endl;
    
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

T22. 汽水瓶

#include <iostream>

using namespace std;

int main()
{
    int empty;//空瓶子数量
    int res = 0;//总共换了多少瓶
    
    while(cin >> empty)
    {
        int new1, hold;
        while(empty >= 2)
        {
            //只有2瓶
            //借一瓶 还3空
            if(empty == 2)
                res++, empty = 0;
            else
            {
                new1 = empty / 3;
                hold = empty % 3;
                res += new1;
                empty = new1 + hold;
            }
        }
        
        cout << res << endl;
        res = 0;
    }
    
    
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

T24. 合唱队

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

void calcu(vector<int> height, vector<int> &incnum)
{
    //计算以height为最高的递增队列人数
    for(int i = 1; i < height.size(); i++)
    {
        for(int j = i - 1; j >= 0; j--)
        {
            if(height[j] < height[i] && incnum[i] < incnum[j] + 1)
                incnum[i] = incnum[j] + 1;
        }
    }
}

int main()
{
    int n;
    while(cin >> n)
    {
        vector<int> height;
        vector<int> incnum(n, 1);
        vector<int> decnum(n, 1);
        vector<int> realnum;
        int h;
        for(int i = 0; i < n; i++)
            cin >> h, height.push_back(h);
        
        //求递增序列数组人数
        calcu(height, incnum);
        //求递减序列数组人数
        reverse(height.begin(), height.end());
        calcu(height, decnum);
        reverse(decnum.begin(), decnum.end());
        
        int maxnum = 0;
        for(int i = 0; i < n; ++i)
        {
            realnum.push_back(incnum[i] +decnum[i] - 1);//减掉自身的
            maxnum = max(maxnum, realnum[i]);
        }
        
        cout << n - maxnum << endl;
    }
    
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

T25. 数据分类处理

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

using namespace std;

bool match(int r, int i)
{
    string strr = to_string(r);
    string stri = to_string(i);
    int pos = stri.find(strr);
    
    if(pos != -1)
        return true;
    else
        return false;
}

int main()
{
    int Inum, Rnum;
    //15 123 456 786 453 46 7 5 3 665 453456 745 456 786 453 123
    //5 6 3 6 3 0
    while(cin >> Inum)
    {
        vector<int> I, R;
        for(int i = 0; i < Inum; i++)
        {
            int temp;
            cin >> temp;
            I.push_back(temp);
        }
        
        cin >> Rnum;
        for(int i = 0; i < Rnum; i++)
        {
            int temp;
            cin >> temp;
            R.push_back(temp);
        }
            
        
        //R排序、去重
        sort(R.begin(), R.end());
        R.erase(unique(R.begin(), R.end()), R.end());
        
        vector<int> value, cnt, index, index1;
        
        for(int i = 0; i < R.size(); i++)
        {
            int cnt1= 0;//记录R[i]在I里面出现的次数 子集数量
            for(int j = 0; j < I.size(); j++)
            {
                if(match(R[i], I[j]))
                {
                    cnt1 ++;
                    index.push_back(j);
                    value.push_back(I[j]);
                }
            }
            
            if(cnt1 != 0)
            {
                cnt.push_back(cnt1);
                index1.push_back(R[i]);
            }
            
        }
        
        int sum = value.size() + cnt.size() + index.size() + index1.size();
        cout << sum << " ";
        int j = 0;
        for(int i = 0; i < cnt.size(); i++)
        {
            cout << index1[i] << ' ' << cnt[i] << ' ' ;
            while((cnt[i]--) > 0)
            {
                cout << index[j] << ' ' << value[j];
                if(i == cnt.size() - 1 && cnt[i] == 0)
                    cout << endl;
                else
                    cout << " ";
                j ++;
            }
            
        }
        
    } 
    
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92

T26. 字符串排序

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    string str;
    
    
    while(getline(cin, str))
    {
        vector<char> alp;//字母顺序存储
        for(int i = 0; i < 26; i++)
            for(int j = 0; j < str.size(); j++)
            {
                if(str[j] - 'a' == i || str[j] - 'A' == i)
                    alp.push_back(str[j]);
            }
        
        for(int m = 0, n = 0; m < str.size() && n < alp.size(); m++)
        {
            if((str[m] >= 'a' && str[m] <='z') || (str[m] >= 'A' && str[m] <= 'Z'))
                str[m] = alp[n++];
        }
        cout << str << endl;
    }
    
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

T27. 查找兄弟单词

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

using namespace std;

int main()
{
    int n;//字典中单词个数
    
    while(cin >> n)
    {
        vector<string> dic;//字典
        string word;
        //字典初始化
        for(int i = 0; i < n; i ++)
        {
            cin >> word;
            dic.push_back(word);
        }
        
        string target;//目标单词
        cin >> target;
        
        //处理 搜索兄弟单词
        vector<string> res;//记录所有兄弟单词
        
        string temp = target;
        sort(temp.begin(), temp.end());//排序
        int lent = target.size();//目标单词的长度
            
        for(int i = 0; i < n; i ++)
        {
            int lendic = dic[i].size();
            
            //长度一直 单词不同
            if((lendic == lent) && target != dic[i])
            {
                string tempdic = dic[i];
                sort(tempdic.begin(), tempdic.end());
                if(temp == tempdic)
                    res.push_back(dic[i]);
            }
            
        }
        
        
        //所有兄弟单词都在res中
        int index;//res[index - 1]
        cin >> index;
        //坑 一定要字典排序
        sort(res.begin(), res.end());
        cout << res.size() << endl;
        //默认了index - 1  < res.size()
        if(index - 1 < res.size())
            cout << res[index - 1] << endl;
    }
    
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

T28. 素数伴侣

#include <iostream>
#include <vector>
#include <math.h>

//匈牙利算法

using namespace std;
int t[100][100] = {0};
vector<int> ji(100, -1);
vector<int> ou(100, -1);
vector<int> visit(100, 0);


bool isprime(int n)
{
    int flag = 0;
    for(int i = 2; i <= n / i; i ++)
        if(n % i == 0)
        {
            flag = 1;
            break;
        }
    if(flag == 0) return true;
    return false;
}

int dfs(int i)
{
    //找到 返回1  没找到返回0
    for(int j = 0; j < 100; j ++)
    {
        if(t[i][j] == 1 && !visit[j])
        {
            visit[j] = 1;// ij匹配
            if(ou[j] == -1 || dfs(ou[j]))
            {
                ji[i] = j;
                ou[j] = i;
                return 1;
            }
        }
    }
    
    return 0;
}

int main()
{
    int n;
    //素数  偶数+奇数
    //素数伴侣  一定是 偶数 + 奇数
    vector<int> cou, cji;
    
    while(cin >> n)
    {
        for(int i = 0; i < n; i ++)
        {
            int temp;
            cin >> temp;
            if(temp % 2 != 0) cji.push_back(temp);//奇数
            else cou.push_back(temp);//偶数
        }
        
        for(int i = 0; i < cji.size(); i ++)
            for(int j = 0; j < cou.size(); j ++)
                if(isprime(cji[i] + cou[j])) t[i][j] = 1;
        
        //匈牙利算法
        int res = 0;
        for(int i = 0; i < cji.size(); i ++)
        {
            if(ji[i] == -1)
            {
                for(int j = 0; j < visit.size(); j ++) visit[j] = 0;
                res += dfs(i);//增广路径 每多找到一组 +1
            }
        }
        
        cout << res << endl;
        //清空
        cji.clear();
        cou.clear();
        
        for(int i = 0; i < 100; i ++)
            for(int j = 0; j < 100; j ++)
                t[i][j] = 0;
        
        for(int i = 0; i < 100; i ++)
            ji[i] = ou[i] = -1;
    }
    
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93

T29. 字符串加解密

#include <iostream>
#include <string.h>

using namespace std;
string data1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string data2 = "BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza1234567890";

int main()
{
    string str1, str2;
    while(cin >> str1 >> str2)
    {
        for(int i = 0; i < str1.size(); i ++)
            str1[i] = data2[data1.find(str1[i])];
        
        for(int i = 0; i < str2.size(); i ++)
            str2[i] = data1[data2.find(str2[i])];
        
        cout << str1 << endl << str2 << endl;
    }
    
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

T30. 字符串合并处理

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

using namespace std;
const string helper1 = "0123456789abcdefABCDEF";
const string helper2 = "084C2A6E195D3B7F5D3B7F";

int main()
{
    string str1, str2, mergestr;
    while(cin >> str1 >> str2)
    {
        mergestr = str1 + str2;
        //奇数 偶数分别拆开
        string strji, strou;
        for(int i = 0; i < mergestr.size(); i++)
        {
            if( i % 2 == 0)
                strou += mergestr[i];
            else
                strji += mergestr[i];
        }
        
        //排序后合并
        sort(strji.begin(), strji.end());
        sort(strou.begin(), strou.end());
        int index = 0;
        for(int i = 0; i < mergestr.size(); i++)
        {
            if(i % 2 == 0)
                mergestr[i] = strou[i / 2];
            else
                mergestr[i] = strji[i / 2];
        }
        
        //加密
        for(int i = 0; i < mergestr.size(); i++)
        {
            int idx = helper1.find(mergestr[i]);
            if(idx != -1)
                mergestr[i] = helper2[idx];
        }
        
        cout << mergestr << endl;
    }
    
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

T37. 统计每个月兔子的总数

#include <iostream>

using namespace std;

int main()
{
    int month;
    while(cin >> month)
    {
        int first = 1, second = 0, third = 0;//初试第一二三月兔子总数
        //first 年龄1个月的兔子数量
        //second 年龄2个月的兔子数量
        //third  年龄3个月的兔子数量
        while(-- month)
        {
            third += second;
            second = first;
            first = third;
        }
        cout << first + second + third << endl;
    }
    
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

T50. 四则运算

T53. 杨辉三角的变形

#include<iostream>
#include<string>
#include<vector>
 
using namespace std;
int main()
{
    int n, m;
    while(cin>>n)
    {
        m = 2*n - 1;
        vector<vector<int>> dp(n, vector<int>(m,0));
        dp[0][n-1] = 1;
        for(int i=1; i<n; i++)
        {
            for(int j = 0; j<m; ++j)
            {
  
                dp[i][j] = dp[i-1][j-1] + dp[i-1][j] + dp[i-1][j+1];
            }
        }
        int k;
        for(k=0; k<m; k++)
        {
            if(dp[n-1][k]%2==0 && dp[n-1][k]!=0)
            {
                cout<<k+1<<endl;
                break;
            }
        }
        if(k == m)
            cout<<-1<<endl;
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

T54. 表达式求值

#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <sstream>
 
using namespace std;
 
void propocess(string &str)
{
    for(int i=0;i<str.size();i++)
    {
        if(str[i] == '{')
            str[i] = '(';
        else if (str[i] == '}')
            str[i] = ')';
        else if(str[i] == '[')
            str[i] = '(';
        else if (str[i] == ']')
            str[i] = ')';
        else if(str[i] == '-')
        {
            if(i==0)
                str.insert(0,1,0);
            else if(str[i-1] =='(')
                str.insert(i,1,0);
        }
    }
}
 
bool compriority(char top,char cur)
{
    if((top =='+'||top == '-') && (cur == '+'||cur == '-'))
        return true;
    if((top =='*'||top == '/') &&(cur =='+' || cur == '-' || cur =='*' || cur == '/'))
        return true;
    if (cur == ')')
        return true;
    return false;
}
 
vector<string> mid2post(string &str)
{
    vector<string> vstr;
    stack<char> cstack;
    for(int i=0;i<str.size();i++)
    {
        string temp = "";
        if(str[i]>='0' && str[i]<='9')
        {
            temp +=str[i];
            while(i+1<str.size() && str[i+1]>='0' && str[i+1]<='9')
            {
                temp += str[i+1];
                i++;
            }
            vstr.push_back(temp);
        }
        else if(cstack.empty() || str[i]=='(')
        cstack.push(str[i]);
        else if(compriority(cstack.top(),str[i]))
        {
            if(str[i] == ')')
            {
                while(!cstack.empty() && cstack.top() !='(')
                {
                temp +=cstack.top();
                cstack.pop();
                vstr.push_back(temp);
                temp = ""; 
                }
                cstack.pop();
            }
            else
            {
                while(!cstack.empty() && compriority(cstack.top(),str[i]))
                {
                    temp += cstack.top();
                    cstack.pop();
                    vstr.push_back(temp);
                    temp = "";
                }
                cstack.push(str[i]);
            }
        }
        else
            cstack.push(str[i]);
    }
    while(!cstack.empty())
    {
        string temp="";
        temp +=cstack.top();
        cstack.pop();
        vstr.push_back(temp);
    }
    return vstr;
}
 
int calcpostexp(vector<string> &vstr)
{
    int num,op1,op2;
    stack<int> opstack;
    for(int i=0;i<vstr.size();i++)
    {
        string temp = vstr[i];
        if(temp[0]>='0' && temp[0]<='9')
        {
            stringstream  ss;
            ss<<temp;
            ss>>num;
            opstack.push(num);
        }
        else if(vstr[i]=="+")
        {
            op2 = opstack.top();
            opstack.pop();
            op1 = opstack.top();
            opstack.pop();
            opstack.push(op1+op2);
        }
        else if(vstr[i]=="-")
        {
            op2 = opstack.top();
            opstack.pop();
            op1 = opstack.top();
            opstack.pop();
            opstack.push(op1-op2);
        }
        else if(vstr[i]=="*")
        {
            op2 = opstack.top();
            opstack.pop();
            op1 = opstack.top();
            opstack.pop();
            opstack.push(op1*op2);
        }
        else if(vstr[i]=="/")
        {
            op2 = opstack.top();
            opstack.pop();
            op1 = opstack.top();
            opstack.pop();
            opstack.push(op1/op2);
        }
    }
    return opstack.top();
}
 
void calcexp(string str)
{
    vector<string> vstr;
    propocess(str);
    vstr = mid2post(str);
    int res = calcpostexp(vstr);
    cout << res <<endl;
}
 
int main()
{
    string str;
    while(cin>>str)
    {
        calcexp(str);
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166

T61. 放苹果

#include <iostream>
#include <string>
 
using namespace std;
 
int Count(int m, int n)
{
    if(m<0)
        return 0;
    if(m==1||n==1)
        return 1;
    return Count(m,n-1)+Count(m-n,n);
}
 
int main()
{
    int m,n;
    while (cin>>m>>n)
    {
        cout << Count(m,n) << endl;
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

T66. 配置文件恢复

T73. 计算日期到天数的转换

#include<iostream>
using namespace std;
  
int main(){
    int M1[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int M2[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
    int year,mon,day;
    while(cin>>year>>mon>>day)
    {
        int res=day;
        if((year%100==0)&&(year%400==0))
        {
            for(int i=1;i<mon;i++)
               res+=M2[i];
        }
        else if(year%4==0&&year%100!=0)
        {
            for(int i=1;i<mon;i++)
                res+=M2[i];
        }
        else
        {
            for(int i=1;i<mon;i++)
                res+=M1[i];
        }
        
        cout<<res<<endl;
    }
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

T74. 参数解析

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str;
	while(getline(cin,str))     //手动输入,不能在此 str="xcopy /s "C:\program files" "d:\" "  双引号套双引号,错误
    {
        int cnt=0;
	    for(int i=0;i<str.size();i++)
	    {
            if(str[i]==' ')
                cnt++;
		    if(str[i]=='"')
		    {
                do 
                    i++;
                while(str[i]!='"');
            }
        }
        cout<<cnt+1<<endl;
        for(int i=0;i<str.size();i++)
        {
            if(str[i]!=' ' && str[i]!='"')
                cout<<str[i];
            if(str[i]==' ')
                cout<<endl;
        }
        cout<<endl;
    }
	//system("pause");
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

T75. 公共字串计算

#include<iostream>
#include<vector>
using namespace std;
int main(){
    string str1, str2;
    while(cin >> str1 >> str2){
        int len1 = str1.size();
        int len2 = str2.size();
        int max = 0;
        vector<vector<int>> dp(len1 + 1, vector<int>(len2 + 1, 0));
        for(int i = 1; i <= len1; ++i){
            for(int j = 1; j <= len2; ++j){
                if(str1[i-1] == str2[j-1])
                    dp[i][j] = dp[i-1][j-1] + 1;
                if(dp[i][j] > max)
                    max = dp[i][j];
            }
        }
        cout << max << endl;
    }
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/146547
推荐阅读
相关标签
  

闽ICP备14008679号