稍等,帮你写一个
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1, str2;
cin >> str1 >> str2;
if (str1.size() != str2.size()) {
cout << "-1" << endl;
return 0;
}
int cnt = 0;
for (int i = 0; i < str1.size(); i++) {
if (str1[i] != str2[i]) {
int j = i + 1;
while (j < str2.size() && str2[j] != str1[i]) {
j++;
}
if (j == str2.size()) {
cout << "-1" << endl;
return 0;
}
while (j > i) {
swap(str2[j], str2[j-1]);
j--;
cnt++;
}
}
}
cout << cnt << endl;
return 0;
}
至少两个字符串中0和1的数量相等才可能通过交换变成一样的
相等怎么替换啊
参考:https://jingyan.baidu.com/article/fedf0737b29e7535ad89775d.html
要清楚是以有符号进行存储还是无符号进行存储。
(1)、 如果是无符号存储,则其为一个正数。
(2)、若是有符号存储,则为补码存储。
(补码存储)需要看其最高位,最高位为0,为正数; 反之,为负数。
(3)、 如果仅仅是给了一堆二进制:比如10101010,那么它是没有正负概念的。
或者说10101010是原码或者补码,进而推断它的原始的值。