Back and Forth

本文介绍了一个关于在两个牛奶仓库间运送牛奶的模拟算法问题。通过分析一周内不同运送策略,将问题分为三类:同一桶往返四次、两桶各往返两次及四桶各往返一次。利用数组记录所有可能的状态,最终统计出仓库可能的奶量种类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门

题面描述:

一个人有两个牛奶仓库,每个仓库有1000单位的牛奶和10个桶,他闲着没事就喜欢在两个牛奶仓库之间运送牛奶玩儿,周二从仓库1选一个桶装满带到仓库2,周三从仓库2选一个桶装满带到仓库1(可能是周二带过来的那个),周四从仓库1选一个桶装满带到仓库2(可能是周三带过来的那个),周五再从仓库2选一个桶装满带到仓库1(可能是周二或周四带过来的那个),周六的时候他想知道仓库1可能会有多少种奶量。

题目分析:

可以纯模拟做,也可以想方法小小优化一下。我的做法是对情况进行分类,对于这个人一整周的操作可以分成三类:

  1. 同一个桶来回拿4次
  2. 4次拿桶中有两次拿了同一个桶
  3. 4次拿的桶都不一样

这三种情况就包含所有了,然后建立一个大于2000的数组,按上面三种情况模拟,成立的情况就把状态改为1,最后遍历数组看有多少个1就有多少种情况。

代码:

#include<stdio.h> 
#include<algorithm>
#include<iostream>
#include<string.h> 
#include<queue>
#include<vector>
using namespace std;
int a[10],b[10],num[2005]={0};

int main(){
	int ans=0;
	for(int i=0;i<10;i++) cin>>a[i];
	for(int i=0;i<10;i++) cin>>b[i];
	num[1000]=1;//同一个桶来回拿4次,不变 
	for(int i=0;i<10;i++){//有两次拿了同一个桶
		for(int j=0;j<10;j++) num[1000-a[i]+b[j]]=1;
	}
	for(int tues=0;tues<10;tues++){//四次都没有拿同一个桶 
		for(int wed=0;wed<10;wed++){
			for(int thur=0;thur<10;thur++){
				if(thur==tues) continue;
				for(int fri=0;fri<10;fri++){
					if(fri==wed) continue;
					num[1000-a[tues]+b[wed]-a[thur]+b[fri]]=1;
				}
			}
		}
	}
	for(int i=0;i<=2000;i++) if(num[i]) ans++;
	cout<<ans;
}
问题 M: The Lost Cow【Easy】 时间限制: 1.000 Sec 内存限制: 128 MB 题目描述 Farmer John has lost his prize cow Bessie, and he needs to find her! Fortunately, there is only one long path running across the farm, and Farmer John knows that Bessie has to be at some location on this path. If we think of the path as a number line, then Farmer John is currently at position x and Bessie is currently at position y (unknown to Farmer John). If Farmer John only knew where Bessie was located, he could walk directly to her, traveling a distance of |x−y|. Unfortunately, it is dark outside and Farmer John can't see anything. The only way he can find Bessie is to walk back and forth until he eventually reaches her position. Trying to figure out the best strategy for walking back and forth in his search, Farmer John consults the computer science research literature and is somewhat amused to find that this exact problem has not only been studied by computer scientists in the past, but that it is actually called the "Lost Cow Problem" (this is actually true!). The recommended solution for Farmer John to find Bessie is to move to position x+1, then reverse direction and move to position x−2, then to position x+4, and so on, in a "zig zag" pattern, each step moving twice as far from his initial starting position as before. As he has read during his study of algorithms for solving the lost cow problem, this approach guarantees that he will at worst travel 9 times the direct distance |x−y| between himself and Bessie before he finds her (this is also true, and the factor of 9 is actually the smallest such worst case guarantee any strategy can achieve). Farmer John is curious to verify this result. Given x and y, please compute the total distance he will travel according to the zig-zag search strategy above until he finds Bessie. 输入 The single line of input contains two distinct space-separated integers x and y. Both are in the range 0…1,000. 输出 Print one line of output, containing the distance Farmer John will travel to reach Bessie. 样例输入 Copy 3 6 样例输出 Copy 9 模拟问题,用C++
最新发布
03-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yjonben

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值