1029 旧键盘 pat 测试点四通不过,求原因


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main(){
    char a1[100],a2[100];
    int aa[40]={0};
    int temp=0;
    char b;
    scanf("%s%s",a1,a2);
    for(unsigned int i=0;i<strlen(a2);i++){
        if(a2[i]>='0'&&a2[i]<='9'){
            temp=a2[i]-'0';
            aa[temp]++;
        }
        else if(a2[i]>='A'&&a2[i]<='Z'){
            temp=a2[i]-'A'+10;
            aa[temp]++;
        }
        else if(a2[i]>='a'&&a2[i]<='z'){
            temp=a2[i]-'a'+10;
            aa[temp]++;
        }
        else if(a2[i]=='_'){
            aa[39]++;
        }
        else{
            continue;
        }
    }
    for(unsigned int i=0;i<strlen(a1);i++){
        if(a1[i]>='0'&&a1[i]<='9'){
            temp=a1[i]-'0';
        }
        else if(a1[i]>='A'&&a1[i]<='Z'){
            temp=a1[i]-'A'+10;
        }
        else if(a1[i]>='a'&&a1[i]<='z'){
            temp=a1[i]-'a'+10;
        }
        else if(a1[i]=='_'){
            temp=39;
        }
        else{
            continue;
        }
        if(aa[temp]==0){
            aa[temp]++;
            if(temp<10){
                b=temp+'0';
                cout<<b;
            }
            else if(temp>10&&temp<39){
                b=temp-10+'A';
                cout<<b;
            }
            else if(temp==39){
                cout<<'_';
            }
        }
    }
}

第54行: else if(temp>10&&temp<39){ 应修改为: else if(temp>=10&&temp<39){
供参考:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main() {
    char a1[100], a2[100];
    int aa[40] = { 0 };
    int temp = 0;
    char b;
    scanf("%s%s", a1, a2);
    for (unsigned int i = 0; i < strlen(a2); i++) {
        if (a2[i] >= '0' && a2[i] <= '9') {
            temp = a2[i] - '0';
            aa[temp]++;
        }
        else if (a2[i] >= 'A' && a2[i] <= 'Z') {
            temp = a2[i] - 'A' + 10;
            aa[temp]++;
        }
        else if (a2[i] >= 'a' && a2[i] <= 'z') {
            temp = a2[i] - 'a' + 10;
            aa[temp]++;
        }
        else if (a2[i] == '_') {
            aa[39]++;
        }
        else {
            continue;
        }
    }
    for (unsigned int i = 0; i < strlen(a1); i++) {
        if (a1[i] >= '0' && a1[i] <= '9') {
            temp = a1[i] - '0';
        }
        else if (a1[i] >= 'A' && a1[i] <= 'Z') {
            temp = a1[i] - 'A' + 10;
        }
        else if (a1[i] >= 'a' && a1[i] <= 'z') {
            temp = a1[i] - 'a' + 10;
        }
        else if (a1[i] == '_') {
            temp = 39;
        }
        else {
            continue;
        }
        if (aa[temp] == 0) {
            aa[temp]++;
            if (temp < 10) {
                b = temp + '0';
                cout << b;
            }
            else if (temp >= 10 && temp < 39) {   //else if (temp > 10 && temp < 39) 修改
                b = temp - 10 + 'A';
                cout << b;
            }
            else if (temp == 39) {
                cout << '_';
            }
        }
    }
}

试一试断点,我猜测是有重复遍历的字符