[HW] OJ记录20题之四

简介: 1 表示数字#include <iostream>#include <string>using namespace std;int main(){ string str; while (cin>>str) { string res = ""; for (int i =

1 表示数字

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str;
    while (cin>>str)
    {
        string res = "";
        for (int i = 0; i < str.size(); i++)
        {
            if (str[i] >= '0' && str[i] <= '9')
            {
                if (i == 0 || str[i - 1] < '0' || str[i - 1] > '9')
                {
                    res += '*';
                }
                res += str[i];
                if (i == str.size() - 1 || (str[i + 1] < '0' || str[i + 1] > '9'))
                {
                    res += '*';
                }
            }
            else
            {
                res += str[i];
            }
        }
        cout<<res.c_str()<<endl;
    }
    return 0;
}

2 查找二进制整数中1的个数

#include <iostream>
using namespace std;
int main()
{
    int n;
    while (cin>>n)
    {
        int cnt = 0;
        while (n)
        {
            ++cnt;
            n &= n - 1;
        }
        cout<<cnt<<endl;
    }
    return 0;
}

3 字符串通配符

#include <iostream>
#include <string>
#include <cmath>
#include <vector>
using namespace std;
void fun(string pat, string dest)
{
    int patLen = pat.size();
    int destLen = dest.size();
    vector<vector<bool>> dp(patLen + 1, vector<bool>(destLen + 1, 0));
    dp[0][0] = 1;
    for (int i = 1; i <= patLen; i++)
    {
        for (int j = 1; j <= destLen; j++)
        {
            if (pat[i - 1] == '?' || abs(pat[i - 1] - dest[j - 1]) == 0 || abs(pat[i - 1] - dest[j - 1]) == 32)
            {
                dp[i][j] = dp[i - 1][j - 1];
            }
            else if (pat[i - 1] == '*')
            {
                dp[i][j] = dp[i - 1][j - 1] || dp[i][j - 1] || dp[i - 1][j];
            }
        }
    }
    if (dp[patLen][destLen])
    {
        cout<<"true"<<endl;
    }
    else
    {
        cout<<"false"<<endl;
    }
}
int main()
{
    string pat, str;
    while (cin>>pat>>str)
    {
        fun(pat, str);
    }
    return 0;
}

4 百钱买百鸡问题

#include <iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    for (int i = 0; i <= 100 / 5; i++)
    {
        for (int j = 0; j <= 100 / 3; j++)
        {
            int k = 100 - i - j;
            if (5 * i + 3 * j + k / 3.0 == 100)
            {
                cout<<i<<" "<<j<<" "<<k<<endl;
            }
        }
    }
    return 0;
}

5 尼科彻斯定理

#include <iostream>
using namespace std;
int main()
{
    int n;
    while (cin>>n)
    {
        int num = n * n - n + 1;
        cout<<num;
        for (int i = 1; i < n; i++)
        {
            cout<<"+"<<num + 2 * i;
        }
        cout<<endl;
    }
    return 0;
}

6 计算字符串的相似度

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

7 求最大连续bit数

#include <iostream>
using namespace std;
int main()
{
    int n;
    while (cin>>n)
    {
        int cnt = 0;
        int res = 0;
        for (int i = 0; i < 8; i++)
        {
            if (n & 0x1 << i)
            {
                ++cnt;
            }
            else
            {
                res = max(res, cnt);
                cnt = 0;
            }
        }
        res = max(res, cnt);
        cout<<res<<endl;
    }
}

8 合并表记录

#include <iostream>
#include <vector>
using namespace std;
//合并表记录
int main()
{
    int n;
    while (cin>>n)
    {
        vector<vector<int>> src(n, vector<int>(2, 0));
        for (int i = 0; i < n; i++)
        {
            cin>>src[i][0]>>src[i][1];
        }
        vector<vector<int>> dest;
        for (int i = 0; i < n; i++)
        {
            bool b = false;
            for (int j = 0; j < dest.size(); j++)
            {
                if (dest[j][0] == src[i][0])
                {
                    dest[j][1] += src[i][1];
                    b = true;
                }
            }
            if (!b)
            {
                dest.push_back(src[i]);
            }
        }
        for (int i = 0; i < dest.size(); i++)
        {
            cout<<dest[i][0]<<endl<<dest[i][1]<<endl;
        }
    }
}

9 密码强度等级

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str;
    while (getline(cin, str))
    {
        int len = str.size();
        int small = 0;
        int big = 0;
        int number = 0;
        int mark = 0;
        for (int i = 0; i < len; i++)
        {
            if (str[i] >= 'a' && str[i] <= 'z')
            {
                small++;
            }
            else if (str[i] >= 'A' && str[i] <= 'Z')
            {
                big++;
            }
            else if (str[i] >= '0' && str[i] <= '9')
            {
                number++;
            }
            else if ((str[i] >= 0x21 && str[i] <= 0x2F)
                     || (str[i] >= 0x3A && str[i] <= 0x40)
                     || (str[i] >= 0x5B && str[i] <= 0x60)
                     || (str[i] >= 0x7B && str[i] <= 0x7E))
            {
                mark++;
            }
        }
        int score = 0;
        if (len <= 4)
        {
            score += 5;
        }
        else if (len <= 7)
        {
            score += 10;
        }
        else
        {
            score += 25;
        }
        if (big != 0 && small != 0)
        {
            score += 20;
        }
        else if (big != 0 || small != 0)
        {
            score += 10;
        }
        if (number == 1)
        {
            score += 10;
        }
        else if (number > 1)
        {
            score += 20;
        }
        if (mark == 1)
        {
            score += 10;
        }
        else if (mark > 1)
        {
            score += 25;
        }
        if (score >= 90)
        {
            cout<<"VERY_SECURE"<<endl;
        }
        else if (score >= 80)
        {
            cout<<"SECURE"<<endl;
        }
        else if (score >= 70)
        {
            cout<<"VERY_STRONG"<<endl;
        }
        else if (score >= 60)
        {
            cout<<"STRONG"<<endl;
        }
        else if (score >= 50)
        {
            cout<<"AVERAGE"<<endl;
        }
        else if (score >= 25)
        {
            cout<<"WEAK"<<endl;
        }
        else
        {
            cout<<"VERY_WEAK"<<endl;
        }
    }
}

10 201301 JAVA题目0-1级

#include <iostream>
#include <vector>
using namespace std;
void fun(vector<int> num, int sum, int pos, bool& stat)
{
    if (pos < 0)
    {
        return;
    }
    if (num[pos] == sum)
    {
        stat = true;
    }
    fun(num, sum - num[pos], pos - 1, stat);
    fun(num, sum, pos - 1, stat);
}
int main()
{
    int n;
    while (cin>>n)
    {
        vector<int> num;
        int sum = 0;
        int sum3 = 0;
        int sum5 = 0;
        int temp;
        for (int i = 0; i < n; i++)
        {
            cin>>temp;
            sum += temp;
            if (temp % 5 == 0)
            {
                sum5 += temp;
            }
            else if (temp % 3 == 0)
            {
                sum3 += temp;
            }
            else
            {
                num.push_back(temp);
            }
        }
        if (sum % 2 != 0 || sum3 > sum / 2 || sum5 > sum / 2)
        {
            cout<<"false"<<endl;
            continue;
        }
        else if (sum3 == sum / 2 || sum5 == sum / 2)
        {
            cout<<"true"<<endl;
            continue;
        }
        int partial = sum / 2 - max(sum3, sum5);
        bool stat = false;
        fun(num, partial, num.size() - 1, stat);
        if (stat)
        {
            cout<<"true"<<endl;
        }
        else
        {
            cout<<"false"<<endl;
        }
    }
}
//3 2 0 0
//4 11 17 19 13
//3 1 2 3
//3 1 -2 3
//4 0 0 1 -1

11 计票统计

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int can;
    while (cin>>can)
    {
        vector<char> arr(can, 0);
        vector<int> res(can + 1, 0);
        for (int i = 0; i < can; i++)
        {
            cin>>arr[i];
        }
        int vote;
        cin>>vote;
        char ch;
        for (int i = 0; i < vote; i++)
        {
            cin>>ch;
            bool b = false;
            for (int j = 0; j < can; j++)
            {
                if (arr[j] == ch)
                {
                    res[j]++;
                    b = true;
                }
            }
            if (!b)
            {
                res[can]++;
            }
        }

        for (int i = 0; i < can; i++)
        {
            cout<<arr[i]<<" : "<<res[i]<<endl;
        }
        cout<<"Invalid : "<<res[can]<<endl;
    }
}

12 等差数列

#include <iostream>
using namespace std;
int main()
{
    int n;
    while (cin>>n)
    {
        cout<<1.5 * n * n + 0.5 * n<<endl;
    }
}

13 求解立方根

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
double fun(double d)
{
    if (d < 0)
    {
        return -fun(-d);
    }
    double left = 0;
    double right = d;
    while (left < right)
    {
        double mid = (left + right) / 2;
        if (mid * mid * mid < d - 1e-6)
        {
            left = mid;
        }
        else if (mid * mid * mid > d + 1e-6)
        {
            right = mid;
        }
        else
        {
            return mid;
        }
    }
    return left;
}
int main()
{
    double d;
    while (cin>>d)
    {
        printf("%.1f\n", fun(d));
    }
    return 0;
}

14 求最小公倍数

#include <iostream>
using namespace std;
int yue(int n, int m)
{
    if (n < m)
    {
        swap(n, m);
    }
    int a = n % m;
    while (a)
    {
        n = m;
        m = a;
        a = n % m;
    }
    return m;
}
int bei(int n, int m)
{
    return n * m / (yue(n, m));
}
int main()
{
    int n, m;
    while (cin>>n>>m)
    {
        cout<<bei(n, m)<<endl;
    }
    return 0;
}

15 明明的随机数

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    while (cin>>n)
    {
        vector<int> num(n, 0);
        for (int i = 0; i < n; i++)
        {
            cin>>num[i];
        }
        sort(num.begin(), num.end());
        int i = 0;
        for (int j = 1; j < num.size(); j++)
        {
            if (num[j] != num[i])
            {
                num[++i] = num[j];
            }
        }
        for (int j = 0; j <= i; j++)
        {
            cout<<num[j]<<endl;
        }
    }
    return 0;
}

16 数字颠倒

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str;
    while (getline(cin, str))
    {
        for (int i = 0, j = str.size() - 1; i < j; i++, j--)
        {
            char ch = str[i];
            str[i] = str[j];
            str[j] = ch;
        }
        cout<<str.c_str()<<endl;
    }
    return 0;
}

17 查找兄弟单词

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
bool isBrother(string str1, string str2)
{
    if (str1.compare(str2) == 0 || str1.size() != str2.size())
    {
        return false;
    }
    int b[26] = {0};
    for (int i = 0; i < str1.size(); i++)
    {
        b[str1[i] - 'a']++;
    }
    for (int i = 0; i < str2.size(); i++)
    {
        b[str2[i] - 'a']--;
    }
    for (int i = 0; i < 26; i++)
    {
        if (b[i] != 0)
        {
            return false;
        }
    }
    return true;
}
int main()
{
    int n;
    while (cin>>n)
    {
        vector<string> words(n, "");
        for (int i = 0; i < n; i++)
        {
            cin>>words[i];
        }
        vector<vector<int>> brothers;
        for (int i = 0; i < n; i++)
        {
            vector<int> bro;
            for (int j = 0; j < n; j++)
            {
                if (j == i)
                {
                    continue;
                }
                if (isBrother(words[i], words[j]))
                {
                    bro.push_back(j);
                }
            }
            brothers.push_back(bro);
        }

        //sort
        for (int i = 0; i < n; i++)
        {
            int len = brothers[i].size();
            for (int j = 0; j < len - 1; j++)
            {
                for (int k = 0; k < len - 1 - j; k++)
                {
                    if (words[k].compare(words[k + 1]) > 0)
                    {
                        swap(brothers[i][k], brothers[i][k + 1]);
                    }
                }
            }
        }
        string str;
        cin>>str;
        int index;
        cin>>index;
        for (int i = 0; i < n; i++)
        {
            if (str.compare(words[i]) == 0)
            {
                cout<<brothers[i].size()<<endl<<words[brothers[i][index - 1]]<<endl;
                break;
            }
        }
    }
    return 0;
}

18 素数伴侣

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
bool isPrime(int n)
{
    for (int i = 2; i <= n / 2; i++)
    {
        if (n % i == 0)
        {
            return false;
        }
    }
    return true;
}
int main()
{
    int n;
    while (cin>>n)
    {
        vector<int> num(n , 0);
        for (int i = 0; i < n; i++)
        {
            cin>>num[i];
        }
        vector<int> dp(n + 1, 0);
        for (int i = 1; i <= n; i++)
        {
            for (int j = i + 1; j <= n; j++)
            {
                if (isPrime(num[i - 1] + num[j - 1]))
                {
                    dp[i] = max(dp[i], dp[i - 1] + dp[j - 1] - dp[j] + 1);
                }
                else
                {
                    dp[i] = max(dp[i], dp[i - 1]);
                }
            }
        }
        cout<<dp[n-2]<<endl;
    }
    return 0;
}

19 字符串合并处理

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
char map[16] ={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char fun(char ch)
{
    int n;
    if (ch >= '0' && ch <= '9')
    {
        n = ch - '0';
    }
    else if (ch >= 'a' && ch <= 'f')
    {
        n = ch - 'a' + 10;
    }
    else if (ch >= 'A' && ch <= 'F')
    {
        n = ch - 'A' + 10;
    }
    n = (n & 1) << 3 | (n & 1 << 1) << 1 | (n & 1 << 2) >> 1 | (n & 1 << 3) >> 3;
    return map[n];
}
int main()
{
    string str1, str2;
    while (cin>>str1>>str2)
    {
        string str = str1 + str2;
        for (int i = 0; i < str.size() - 1; i++)
        {
            for (int j = 0; j < str.size() - 2 - i; j++)
            {
                if (str[j] > str[j + 2])
                {
                    swap(str[j], str[j + 2]);
                }
            }
        }
        for (int i = 0; i < str.size(); i++)
        {
            if (str[i] >= '0' && str[i] <= '9' || str[i] >= 'a' && str[i] <= 'f'
                || str[i] >= 'A' && str[i] <= 'F')
            {
                str[i] = fun(str[i]);
            }
        }
        cout<<str.c_str()<<endl;
    }
    return 0;
}

20 计算日期到天数转换

#include <iostream>
using namespace std;

int m[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
bool fun(int year)
{
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
    {
        return true;
    }

    return false;
}

int main()
{
    int year, month, day;
    while (cin>>year>>month>>day)
    {
        int sum = 0;
        for (int i = 0; i < month - 1; i++)
        {
            sum += m[i];
        }
        sum += day;
        if (fun(year) && sum > 59)
        {
            sum++;
        }
        cout<<sum<<endl;
    }
}
目录
相关文章
|
3月前
【bug记录】旋转链表与力扣报错:member access within null pointer of type ‘struct ListNode‘
【bug记录】旋转链表与力扣报错:member access within null pointer of type ‘struct ListNode‘
|
6月前
|
机器学习/深度学习 存储 测试技术
[C++/PTA] 队列操作
[C++/PTA] 队列操作
49 0
|
索引
华为机试HJ8:合并表记录
华为机试HJ8:合并表记录
|
测试技术
华为机试HJ48:从单向链表中删除指定值的节点
华为机试HJ48:从单向链表中删除指定值的节点
|
机器学习/深度学习 测试技术 C++
C++/PTA 队列操作
请实现一个MyQueue类,实现出队,入队,求队列长度.
150 0
【每日一题Day103】LC1669合并两个链表 | 模拟
思路:遍历链表找到list1中的第a−1个节点和第b+1个节点,然后将第a−1个节点指向list2链表的初始节点,list2链表的尾节点指向list1中的第b+1个节点
83 0
【每日一题Day103】LC1669合并两个链表 | 模拟
AC牛客 BM4 合并两个排序的链表
AC牛客 BM4 合并两个排序的链表
95 0
AC牛客 BM4 合并两个排序的链表
牛客hot100--BM6---判断链表中是否有环(简单难度)
牛客hot100--BM6---判断链表中是否有环(简单难度)
132 0
牛客hot100--BM6---判断链表中是否有环(简单难度)
|
数据格式
HDL-Bits 刷题记录 04
HDL-Bits 刷题记录 04 搬运本人博客
112 0
HDL-Bits 刷题记录 04
HDL-Bits 刷题记录 03
HDL-Bits 刷题记录 03
103 0
HDL-Bits 刷题记录 03