PAT 字符串处理专项之一

PAT 1001 A+B Format 思路见该博客。
PAT 1005 Spell It Right
PAT 1006 Sign In and Sign Out
PAT 1035 Password

PAT 1005 题解
  • 数据的数量级达到 10 0 100 100^{100} 100100,正常的整数类型不能够处理,因此要用字符串对整数进行处理。
#include<iostream>

using namespace std;

string english[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

int main(){

    string num;
    cin >> num;

    int len = num.size();
    int res = 0;

    for (int i = 0; i < len; i++){
        res += num[i] - '0';
    }

    string r = to_string(res);

    int len2 = r.size();
    for (int j = 0; j < len2 - 1; j++){
        cout << english[r[j] - '0'] << " ";
    }

    cout << english[r[len2-1] - '0'];

    return 0;
}

 

PAT 1006 题解
  • 切入点:寻找最早到达时间 unlock,最晚离开时间 lock
  • 时间具有这样的格式 HH:MM:SS,便于时间的比较。

时间比较可以采用字典序的比较,可以使用字典序比较的关键在于位数相同

#include<iostream>

using namespace std;

int main(){

    int n;
    cin >> n;

    // o: ID, 1: Time
    string unlock[2];
    string lock[2];
    
    for (int i = 0; i < n; i++){
        string a, b, c;
        cin >> a >> b >> c;

        if (!i || b < unlock[1]){
            unlock[0] = a;
            unlock[1] = b;
        }

        if (!i || c > lock[1]){
            lock[0] = a;
            lock[1] = c;
        }
    }

    cout << unlock[0] << " " << lock[0];

    return 0;
}

/* 一开始自己做题时候的思路,用这个函数去实现时间的比较。
bool compare(string a, string b){

    int i = (a[0] - '0') * 10 + (a[1] - '0');
    int j = (b[0] - '0') * 10 + (b[1] - '0');

    if (i > j) return false;
    else if(i < j) return true;
    else{
        int i = (a[3] - '0') * 10 + (a[4] - '0');
        int j = (b[3] - '0') * 10 + (b[4] - '0');
        if (i > j) return false;
        else if(i < j) return true;
        else{
            int i = (a[6] - '0') * 10 + (a[7] - '0');
            int j = (b[6] - '0') * 10 + (b[7] - '0');
            if (i > j) return false;
            else if(i <= j) return true;
        }
    }

}*/

 

PAT 1035 题解
  • no modified account.
  • 输出注意单复数
#include <iostream>

using namespace std;
#define MAXN 1010

string res[MAXN];

int main(){

    int n;
    cin >> n;
    int n_copy = n;

    int count = 0;
    while(n--){

        string id, passwd;
        cin >> id >> passwd;
        bool flag = false;

        for(int i = 0; i < int(passwd.size()); i++){
            /*
				change函数,转换后的字符与当前字符相比较。
			*/
            switch (passwd[i]){
                case '0': {passwd[i] = '%'; flag=true; break;}
                case '1': {passwd[i] = '@'; flag=true; break;}
                case 'O': {passwd[i] = 'o'; flag=true; break;}
                case 'l': {passwd[i] = 'L'; flag=true; break;}
            }
        }

        if (flag)
            res[count++] = id + " " + passwd;
    }

    if (count){
        cout << count << endl;
        for (int i = 0; i < count; i++)
            cout << res[i] << endl;
    }else{
        if (n_copy > 1)
            cout << "There are " << n_copy << " accounts and no account is modified" << endl;
        else
            cout << "There is 1 account and no account is modified" << endl;
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值