算法每日一题——08.19

题目链接
乍一看以为要排序,竟然是个思维题。

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值