• PAT乙级1014


    题目链接

    https://pintia.cn/problem-sets/994805260223102976/problems/994805308755394560

    题解一(部分正确)

    这是我的方法,第2个测试点没有过,和正确的代码比较,目前没比较出来错误,可能是我map用错了?

    需要注意的点:

    1. 第一对是相同的大写字母A-G
    2. 第二对是相同的数字0-9和A-N
    3. 小时和分钟输出宽度为2,不足2位用零填充
    4. 不用map也行,可以用ASCII码和字符的对应关系
    5. 判断大小写字母、数字等函数C++已自带,不用自己写
    // PAT BasicLevel 1014
    // https://pintia.cn/problem-sets/994805260223102976/problems/994805308755394560
    
    #include <iostream>
    #include <string>
    #include <map>
    using namespace std;
    
    bool isUpperCase(char);
    bool isLowerCase(char);
    bool isAlpha(char);
    bool isNumber(char);
    bool isDay(char);
    bool isHour(char);
    
    int main()
    {
        // 获取四个字符串
        string strs[4];
        for(int i=0;i<4;++i){
            cin >> strs[i];
        }
    
        // 字母与周几的映射
        map<char,string> dayMap;
        dayMap['A'] = "MON";dayMap['B'] = "TUE";dayMap['C'] = "WED";
        dayMap['D'] = "THU";dayMap['E'] = "FRI";dayMap['F'] = "SAT";dayMap['G'] = "SUN";
    
        // 字母(或数字)与小时的映射
        map<int, int> hourMap;
        hourMap[0] = 0;hourMap[1] = 1;hourMap[2] = 2;hourMap[3] = 3;hourMap[4] = 4;
        hourMap[5] = 5;hourMap[6] = 6;hourMap[7] = 7;hourMap[8] = 8;hourMap[9] = 9;
        hourMap['A'] = 10;hourMap['B'] = 11;hourMap['C'] = 12;hourMap['D'] = 13;
        hourMap['E'] = 14;hourMap['F'] = 15;hourMap['G'] = 16;hourMap['H'] = 17;
        hourMap['I'] = 18;hourMap['J'] = 19;hourMap['K'] = 20;hourMap['L'] = 21;
        hourMap['M'] = 22;hourMap['N'] = 23;
    
        // 遍历前两个字符串
        int index;
        int minLen1 = strs[0].length() < strs[1].length() ? strs[0].length() : strs[1].length();
        for (int i = 0; i < minLen1; ++i){
            if (strs[0][i] == strs[1][i] && isDay(strs[1][i])){
                cout << dayMap[strs[1][i]] << ' ';
                index=i;
                break;
            }
        }
    
        for(int i=index+1;i<minLen1;i++){
            if (strs[0][i] == strs[1][i] && isHour(strs[1][i])){
                printf("%02d:", hourMap[strs[1][i]]);
                break;
            }
        }
    
        // 遍历后两个字符串
        int minLen2 = strs[2].length() < strs[3].length() ? strs[2].length() : strs[3].length();
        for (int i = 0; i < minLen2; ++i){
            if (strs[2][i] == strs[3][i] && isAlpha(strs[3][i])){
                printf("%02d", i);
                break;
            }
        }
    
        //system("pause");
        return 0;
    }
    
    bool isDay(char c){
        // A-G
        return c >= 'A' && c <= 'G';
    }
    
    bool isHour(char c){
        // 0-9 A-N
        return isNumber(c) || (c >= 'A' && c <= 'N');
    }
    
    bool isUpperCase(char c){
        // A-Z
        return c >= 'A' && c <= 'Z';
    }
    
    bool isLowerCase(char c){
        // a-z
        return c >= 'a' && c <= 'z';
    }
    
    bool isAlpha(char c){
        // a-z A-Z
        return isLowerCase(c)||isUpperCase(c);
    }
    
    bool isNumber(char c){
        // 0-9
        return c >= '0' && c <= '9';
    }
    

    题解二

    网上找的,和我看起来思路一样啊……

    参考链接:https://blog.csdn.net/supremebuct/article/details/83105861

    #include <iostream> //9.16
    #include <stdlib.h>
    #include <string>
    #include <cctype>
    using namespace std;
    
    void deal(string ch1, string ch2);
    void deal1(string ch3, string ch4);
    
    int main()
    {
        string ch1, ch2, ch3, ch4;
        cin >> ch1 >> ch2 >> ch3 >> ch4;
        deal(ch1, ch2);
        deal1(ch3, ch4);
    
        return 0;
    }
    
    void deal(string ch1, string ch2)
    {
        string day[] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
        int num, num1, i = 0;
        while (i < ch1.length() && i < ch2.length())
        {
            if (ch1[i] == ch2[i] && (ch1[i] >= 'A' && ch1[i] <= 'G')) //一开始没有设定范围只是判断了其是否是大写,导致有的例子通不过
            {
                num1 = ch1[i] - 'A';
                break;
            }
            i++;
        }
        i++;
        cout << day[num1] << ' ';
        int num2;
        while (i < ch1.length() && i < ch2.length())
        {
            if (ch1[i] == ch2[i])
            {
                if (isdigit(ch1[i]))
                {
                    num2 = ch1[i] - '0';
                    break;
                }
                else if (ch1[i] >= 'A' && ch1[i] <= 'N') //一开始没有设定范围只是判断了其是否是大写,导致有的例子通不过
                {
                    num2 = 10 + (ch1[i] - 'A');
                    break;
                }
            }
            i++;
        }
        printf("%02d:", num2);
    }
    void deal1(string ch3, string ch4)
    {
        int i = 0;
    
        int num3;
        while (i < ch3.length() && i < ch4.length())
        {
            if (ch3[i] == ch4[i] && isalpha(ch3[i]))
            {
                num3 = i;
                break;
            }
            i++;
        }
        printf("%02d", num3);
    }
    

    作者:@臭咸鱼

    转载请注明出处:https://www.cnblogs.com/chouxianyu/

    欢迎讨论和交流!


  • 相关阅读:
    css3 box-shadow属性 鼠标移动添加阴影效果
    从客户端(txtContent="<p>1</p>")中检测到有潜在危险的 Request.Form 值
    JS 点击按钮后弹出遮罩层,有关闭按钮
    htm5 手机自适应问题 文本框被激活(获取焦点)时,页面会放大至原来尺寸。
    !important 语法
    如何控制table中td内的文本位置
    点击超链接不跳转,不刷新页面
    学以致用三十二-----python中函数的括号使用
    学以致用三十一-----IPAddressField has been removed
    学以致用三十-----pycharm创建django项目忘记添加app
  • 原文地址:https://www.cnblogs.com/chouxianyu/p/11303645.html
Copyright © 2020-2023  润新知