#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 << '_';
}
}
}
}
试一试断点,我猜测是有重复遍历的字符
找规律的题目,还是很考验临场综合能力的,这一题我也找到了规律,计算比例数组,但是没想到比例数组竟然有超过整型的数,大意了....定义为long long int型就可以通过了~当然,看了书上的才发现.....我想的还是复杂了,还是书上的简单....