一道简单的贪心算法题

一直都不理解贪心算法的我,在做到题的时候,居然突然就懂了。
果然做题才是第一生产力。
上题:
Snuke has decided to play with a six-sided die. Each of its six sides shows an integer 1 through 6, and two numbers on opposite sides always add up to 7.
Snuke will first put the die on the table with an arbitrary side facing upward, then repeatedly perform the following operation:

Operation: Rotate the die 90° toward one of the following directions: left, right, front (the die will come closer) and back (the die will go farther). Then, obtain y points where y is the number written in the side facing upward.

For example, let us consider the situation where the side showing 1 faces upward, the near side shows 5 and the right side shows 4, as illustrated in the figure. If the die is rotated toward the right as shown in the figure, the side showing 3 will face upward. Besides, the side showing 4 will face upward if the die is rotated toward the left, the side showing 2 will face upward if the die is rotated toward the front, and the side showing 5 will face upward if the die is rotated toward the back.

Find the minimum number of operation Snuke needs to perform in order to score at least x points in total.
这个题中,贪心算法所讲究的局部最优解就是每一步能够走到的最大值,正好该题所解决的正是每一步的解法=,=所以还是比较直接的啦算骰子在最少步数的最高分, 其实也就是算骰子在移动过程中的变化区间啦。AC之后还是觉得这个题的求出最优解的局部方向还是很好想到的。

不过对于真蒟蒻的我来, 还是想了一会的。。。而且还打出了一个绝对会被hacked的写法。毕竟三个if的用法确实没考虑好
if (x/11!=0;
a=(x/11)*2;
if (x%11<=6&&x%11>0)
a+=1;
if (x%11>6)
a+=2;
可是这样居然给过了,我是真的佛了。。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值