B题 WA求助
大佬们,提个问题,我的写法是遍历输入的整个字符串,如果某个位置上的字母与标准的字符串不同的话,就找到这个字符该在的位置,交换字母,直到遍历完整个字符串,结果是WA,请问是哪里出了问题啊
代码附上
#include<iostream>
using namespace std;
string str1 = "cocacola";
int num[26][3];
int main()
{
string str2;
cin >> str2;
num['c' - 'a'][0] = 1;
num['o' - 'a'][0] = 2;
num['c' - 'a'][1] = 3;
num['a' - 'a'][0] = 4;
num['c' - 'a'][2] = 5;
num['o' - 'a'][1] = 6;
num['l' - 'a'][0] = 7;
num['a' - 'a'][1] = 8;
int times = 0;
for(int i = 0;i < str2.length();i++)
{
if(str2[i] != str1[i])
{
for(int j = 0;j < 3;j++)
{
if(str2[num[str2[i] - 'a'][j] - 1] != str2[i] && num[str2[i] - 'a'][j] != 0)
{
swap(str2[i], str2[num[str2[i] - 'a'][j] - 1]);
times++;
break;
}
}
}
}
cout << times;
}
代码附上
#include<iostream>
using namespace std;
string str1 = "cocacola";
int num[26][3];
int main()
{
string str2;
cin >> str2;
num['c' - 'a'][0] = 1;
num['o' - 'a'][0] = 2;
num['c' - 'a'][1] = 3;
num['a' - 'a'][0] = 4;
num['c' - 'a'][2] = 5;
num['o' - 'a'][1] = 6;
num['l' - 'a'][0] = 7;
num['a' - 'a'][1] = 8;
int times = 0;
for(int i = 0;i < str2.length();i++)
{
if(str2[i] != str1[i])
{
for(int j = 0;j < 3;j++)
{
if(str2[num[str2[i] - 'a'][j] - 1] != str2[i] && num[str2[i] - 'a'][j] != 0)
{
swap(str2[i], str2[num[str2[i] - 'a'][j] - 1]);
times++;
break;
}
}
}
}
cout << times;
}