题目链接
乍一看以为要排序,竟然是个思维题。
#include<bits/stdc++.h>
using namespace std;
/*
将和尽量变大,这样才能尽可能的短
*/
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
int pre_input = 0;
int cur_input = 0;
bool flag = false;
for (int i = 0; i < n; i++)
{
pre_input = cur_input;
cin >> cur_input;
if (i != 0 && cur_input != pre_input)
{
flag = true;
}
}
if (flag == true)
cout << 1 << endl;
else cout << n << endl;
}
return 0;
}
这个题写的没错,可是总是在一个测试样例中超时,可是看别的博客写的和我的一样😢
题目链接
#include<bits/stdc++.h>
using namespace std;
string a;
string b;
int jilu[1006];
int my_count;
void reverse_string(string &str, int len)
{
for (int i = 0; i < len; i++)
{
if (str[i] == '0')
str[i] = '1';
else
str[i] = '0';
}
reverse(str.begin(), str.begin() + len);
}
void transform_string(int len, string a, string b)
{
for (int i = len - 1; i >= 0; i--)
{
if (i == 0)
{
if (a[0] != b[0])
{
jilu[my_count++] = 1;
reverse_string(a, 1);
}
}
else
{
if (a[i] != b[i])
{
if (a[0] != a[i])
{
jilu[my_count++] = 1;
reverse_string(a, 1);
}
jilu[my_count++] = i + 1;
reverse_string(a, i + 1);
}
}
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
my_count = 0;
cin >> n;
cin >> a;
cin >> b;
transform_string(n, a, b);
cout << my_count;
for (int i = 0; i < my_count; i++)
{
cout << " "<< jilu[i];
}
cout << endl;
}
return 0;
}