Mathematical problem drinking beer out

Laboratory organization often gather dinners are bound to have a beer when dinner, Xiao Ming and soldier are a clown, a very good relationship, of course, often bickering lift each other, saying They just time for a party who should or should not drink the bickering, so the teacher with a question for them to do a referee, who should find out whether it is the answer to the question, whoever the right to require another person to drink or not drink. The teacher's question is this: n dollars to you, let you buy beer, beer unit price is $ 2 a bottle, drinking a bottle of beer, there will be an empty bottle and a cap, and now tell you two bottles or 4 caps can also exchange for a bottle of beer, the question now is up to drink a few bottles of beer.

Input
this problem a plurality of test data sets, each set of test data is only one line, i.e., the number of natural number n (0 <= n <= 10000) represents the amount of money.

Output
For each set of input, output and only one line, that is, the number of beer bottles up to drink.

Sample Input
2
4

Sample Output
1
3

#include<bits/stdc++.h>
using namespace std;
const int MAXN=100005;
int n,x,y,ans;
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        ans=n/2;
        x=n/2;
        y=n/2;
        while(x/2||y/4)
        {
            int temp=x/2+y/4;
            ans+=temp;
            x%=2;
            y%=4;
            x+=temp;
            y+=temp;
        }
        printf("%d\n",ans);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44061561/article/details/94657012