C++贪心算法(4)

过河的最短时间

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a[1010]={0};
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a+0,a+n);
	int x=n-2;
	int y=n-1;
	int tmp=a[1];
	while(true)
	{
		int tmp1=a[0]+a[y]+a[1]+a[1];
		int tmp2=a[0]+a[y]+a[0]+a[x];
		if(tmp1<tmp2)tmp=tmp+tmp1;
		else tmp=tmp+tmp2;
		x=x-2;
		y=y-2;
		if(x==0||x==1)break;
	}
	if(x==1)tmp=tmp+a[0]+a[3];
	cout<<tmp;
	
	
	return 0;
}

 

 特殊的密码锁


#include<bits/stdc++.h>
using namespace std;
void func(bool,int);
int main()
{
	int n;
	cin>>n;
	bool f1=true;
	bool f2=true;
	bool a1[110]={0};
	bool a2[110]={0};
	bool b[110]={0};
	string q,p;
	cin>>q>>p;
	int l=q.size();
	int cnt1=0;
	int cnt2=0;
	for(int i=0;i<l;i++)
	{
		a1[i]=q[i]-48;
		a2[i]=q[i]-48;
		b[i]=p[i]-48;
	}
	for(int i=0;i<l;i++)
	{
		if(b[i]!=a1[i])
		{
			func(a1,i+1);
			cnt1++;
		}
	}
	if(a2[l-1]!=b[l-1])
	{
		f1=false;
	}
	if(a2[0]==b[0])
	{
		func(a2,1);
		cnt2++;
	}
	if(a2[1]==b[1])
	{
		func(a2,2);
		cnt2++;
	}
	for(int i=2;i<l-1;i++)
	{
		if(b[i]!=a1[i])
		{
		    func(a2,i+1);
			cnt2++;
		}
	}
	if(a2[l-1]==b[l-1])
	{
		cnt2++;
	}
	else
	{
		f2=false;
		if(f1==false&&f2==false)
	    {
		    cout<<"impossible";
		    return 0;
	    }
	}
	cout<<min(cnt1,cnt2);
	
	
	return 0;
}
void f(bool a[],int n)
{
	if(a[n]==0)a[n]++;
	else a[n]--;
	if(a[n-1]==0)a[n-1]++;
	else a[n-1]--;
	if(a[n+1]==0)a[n+1]++;
	else a[n+1]--;
}

接水问题

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a[110]={0};
	int n,m;
	cin>>m>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	int b[110]={0};
	for(int i=0;true;i++)
	{
		b[i]=b[i]+a[i];
		if(i>=n)break;
		bool f=true;
		for(int j=0;j<m-1;j++)
		{
		    i++;
		    b[j]=b[j]+a[i];
		    if(i>=n)
		    {
		    	f=false;
		    	break;
			}
		}
		if(f==false)break;
	}
	cout<<max(b[0],max(b[1],b[2]));
	
	
	return 0;
}

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值