poj3210: the gain of very easy problem

Is this the easiet problem in poj? Enter a number, even numbers have no solution, odd numbers are reduced by one output.

Intention:
Snoopy has n coins. After x flips, all coins can be front or back facing up, and you need to get the minimum value of x. (That is to flip x times, so that you can achieve the goal in different situations. In some cases, you only need fewer times, but you must also ensure that the target is achieved after x times)

Analysis:
1 If there are even numbers of coins, there are odd numbers of coins up (down) + odd number of coins down (upper) and even number of coins up (down) + even number of coins down (up). If it is the former, Snoopy can make all the coins unified direction after an odd number of flips. If it is the latter, Snoopy can make all the coins unify direction after even flippings.
For example, if there are 4 coins now, the initial statement is 0001, 0011, 0111, … (0 for downwards and 1 for upwards). If it is 0001, Snoopy can be flipped 1 or 3 times; if it is 0011, Snoopy can be flipped twice to achieve; if it is 0111, Snoopy can be flipped 3 times or 1 time to achieve. Assuming that the value of x is now 3, then the initial statement is 0001, the coin of 0111 can be realized, but if it is 0011, after flipping twice, the coin has already met the target, and then it will be the third time. Therefore, it will break the statement. In summary, when n is even, there will be no exact x value to achieve the target.

2 If there are odd numbers of coins, there are even cases of up (down) + odd down (up). Then Snoopy can make all the coins meet the target state after an even number of flips.
For instance, now there are 5 coins, the initial statement is 00001,00011, … If it is 00001, Snoopy can flip 1 or 4 times; if it is 00011, Snoopy flip 2 or 3 times to achieve the target. If we find the minimum value of x, we can see if the state with the maximum can satisfy other statements. Here we can see that the time of flips is up to 4, the statement is 00001, we look at the other state of 00011, we can achieve 00011 -> 00000 for two times, the remaining two times can be used to flip the same coin to achieve the target. The target statement is not destroyed here because we can always achieve the target through an even times of flips, and then we can flip the same coin to keep the target state after the even number of flips (a coin flips even times is not the original state) _).

To sum up, when n is even, the answer is “No Solution!”; when n is odd, the answer is x = n - 1. What’s more,be careful for the output, such as there may be typing errors in “No Solution!”.

Code in c++:
#include
using namespace std;
int main()
{
int n;
while(scanf("%d",&n))
{
if(n0)
break;
if(n%2
0)
printf(“No Solution!\n”);
else if(n%2!=0)
printf("%d\n",n-1);
}
system(“pause”);
return 0;
}

wibesite: poj.org

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值