Arya and Bran & Game of the Rows & Multiple Clocks

题目一:Arya and Bran

错解:

#include<cstdio> 

int main()
{
	int n,k,candy;
	int left = 0,day = 0,toGive = 0,yes = 0;
	scanf("%d %d",&n,&k);
	for(int i = 0;i < n;i ++)
	{
		scanf("%d",&candy);
		candy += left;
		if(candy > 8)
		{
			left = candy - 8;
			candy = 8;
		}
		
		if(candy + toGive >= k)
		{
			yes ++;
			day ++;
			if(yes == 1)
			break;

		}
		else 
		{
			day ++;
			toGive += candy;
		}
	}
	printf("%d\n",yes == 0 ? -1:day);
	return 0;
}

错因分析:left 存储的是剩下的糖果,但是当candy小于8的时候,虽然糖果分出去了但是left并没有减少。

AC代码:(虽然通过了,但显得冗杂,更好地版本,只用了三个变量,当前总糖果数,要给的糖果数,还要给的糖果数,left并到k里面去了)

#include<cstdio> 

int main()
{
	int n,k,candy;
	int left = 0,day = 0,toGive = 0,yes = 0;
	scanf("%d %d",&n,&k);
	for(int i = 0;i < n;i ++)
	{
		scanf("%d",&candy);
		candy += left;
		left = candy;
		if(candy > 8)
		{
			candy = 8;
		}
		
		if(candy + toGive >= k)
		{
			yes ++;
			day ++;
			if(yes == 1)
			break;

		}
		else 
		{
			day ++;
			toGive += candy;
			left -= candy;
		}
	}
	printf("%d\n",yes == 0 ? -1:day);
	return 0;
}


题目二:Game of the Rows

错解:

#include<cstdio>

int main()
{
	int n,k,G;
	int numF,numT,numY = 0,numE = 0;
	scanf("%d %d",&n,&k);
	numF = n;
	numT = n*2;
	int flag = 1;
	for(int i= 1;i <= k;i ++)
	{
		scanf("%d",&G);
		int f = G/4;
		if(numF >= f)
		{
			numF -= f;
			G -= 4*f;
		}
		if(f != 0 && f > numF)
		{
			G -= numF*4;
			numF = 0;
		}
		int t;
		if(G % 2)
		t = G/2 + 1;
		else t = G/2;
		if(t > numT)
		{
			G -= numT*2;
			numT = 0;
			if(G > 4)
			{
				flag = 0;
				continue;
			}
			else
			{
				if(numY && G == 1)
				{
					numY --;
					G --;
				}
				if(numE && G == 1)
				{
					numE --;
					G --;
				}
				if(numE && G == 2)
				{
					numE --;
					G -= 2;
				}
				if(numF)
				{
					switch(G)
					{
						case 1:numE ++;numF --;G --;break;
						case 2:numY ++;numF --;G -= 2;break;
						case 3:numF --;G-= 3;break;
						default:break;
					}
				}
				
			}
			if(G > 0)
			flag = 0;
		}
		else numT -= t;
	}
	printf("%s\n",flag == 1?"YES":"NO");
	return 0;
}

错因分析:写得太混乱了,不想看下去了。。。

别人使用have数组就比我使用numX变量代码精简很多

都是先分4个座位的,我是先计算出要多少个,然后考虑够不够,够怎么办,不够怎么办,每一步都在if

别人是一个一个地分,不够再分2个座位的,小于三个人的时候先用cnt保留下来。最后先分两个人一组的士兵,先分两个人的座位,再拆分4个人的座位(还有的话),最后看一个人的作为够不够。

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e2 + 17;

int n, k, have[5], cnt[5];
int main(){
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> n >> k;
	have[2] = 2 * n, have[4] = n;
	for(int i = 0; i < k; i++){
		int x;
		cin >> x;
		while(x >= 3)
			if(have[4] > 0)
				x -= 4, have[4]--;
			else if(have[2] > 0)
				x -= 2, have[2]--;
			else
				return cout << "NO" << '\n', 0;
		if(x > 0)
			cnt[x]++;
	}
	while(cnt[2])
		if(have[2] > 0)
			cnt[2]--, have[2]--;
		else if(have[4] > 0)
			cnt[2]--, have[4]--, have[1]++;
		else
            cnt[2]--, cnt[1] += 2;
	if(cnt[1] > have[1] + have[2] + have[4] * 2)
		return cout << "NO" << '\n', 0;
	cout << "YES" << '\n';
	return 0;
}

题目三: Multiple Clocks


AC代码:

#include<cstdio>
#include<algorithm>
using namespace std;
 
int main()
{
	int N;
	long long t;
	scanf("%d",&N);
	scanf("%lld",&t);
	long long gb = t;
	for(int i = 1;i < N;i ++)
	{
		scanf("%lld",&t);
		long long a = max(gb,t);
		long long b = min(gb,t);
		while(b)
		{
			long long c = a%b;
			a = b;
			b = c;
		}
		gb = gb/a*t; //gb先跟t相乘数据太大
	}
	printf("%lld\n",gb);
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值