移动01

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<string>

using namespace std;
void print(vector<int>& a) {
for (auto& x : a) {
cout << x <<" ";
}
}
const int N = 1024 + 1;
int n;
vector<int>v(N);
int res = 0;
vector<pair<int, int>>result;
int main() {
cin >> n;
string str;
getline(cin,str);
getline(cin, str);
int cnt = 0;
int cnt0 = 0;
for (int i = 2; i < str.size() && cnt <= n; i++) {
if (str[i] == ' ') {
i = i + 2;//为什么--跳过0x
continue;
}
int temp = 0;
cnt += 4;//记录什么的--十进制??分离0和1
int tempcnt = cnt;
if (str[i] >= '0' && str[i] <= '9') {
temp = str[i] - '0';
}
else {
temp = str[i] - 'A' + 10;
}
for (int j = tempcnt - 1; j >= cnt - 4; j--) {//将每一位数字取出来
if (temp & 1 == 1)
v[j] = 1;
else {
v[j] = 0;
if (j < n) {//为什么是小于n--后面是无关位数了
cnt0++;//记录0的个数
}
}
temp = temp >> 1;
}
}
if (cnt0 == 0) {//也就是没有损坏
cout << 0;
return 0;
}
int max_ = 0;
int min_ = INT_MAX;
for (int j = 0; j < n; j++) {
int sum = 0;
for (int i = 0; i < n; i++) {
if (v[i] == 0 && (i - j) >= 0 && v[i - j] == 1)//所以j是移动的位数吗---模拟--把每一位0都照顾到
sum++;//应该是可以补偿小孔的数量--确实应该是尽量多,值的范围永远是小于等于坏孔数
}
if (sum > max_) {
min_ = j;
max_ = sum;
}
}
if (max_ == cnt0)
result.push_back({ min_,max_ });
max_ = 0;
min_ = INT_MAX;
for (int j = 0; j < n; j++) {
int sum = 0;
for (int i = 0; i < n; i++) {
if (v[i] == 0 && (i + j) < n && v[i + j] == 1)
sum++;
}
if (sum > max_) {
min_ = j;
max_ = sum;
}
}
if (max_ == cnt0) {
result.push_back({ min_ * -1,max_ });
}
cout << result.size() << endl;
for (int i = 0; i < result.size(); i++) {//磕磕绊绊的明白
if (result[i].first > 0) cout << '+';
cout << result[i].first << endl;
for (int j = 0; j < n; j++) {
int temp = j+ result[i].first;//为什么要加j--temp代表什么--0~n-1位数---代表第几位
if (temp >= 0 && temp < n) {
if (v[j] == 1) {
if (v[temp] == 0) {//temp是要补偿的小孔
cout << 1;
}
else {
cout << 0;
}
}
else {
cout << 0;
}
}
else cout << 0;
}
cout << endl;
}
cout << sizeof(int) << endl;
return 0;
}

posted @ 2023-05-10 16:28  OUKK  阅读(9)  评论(0编辑  收藏  举报