Uva 10940 - Throwing cards away II

#include<cstdio>
#include<iostream>
#include<cmath>

#define sf scanf
#define pf printf

using namespace std;

long ans[500050];

void remaining_cards()
{
    ans[1] = ans[2] = 1;
    ans[3] = 2;

    for(int i=4; i<=500050; i++)
    {
        ans[i] = ans[i-1] + 2;

        if(ans[i] > i)
        {
            ans[i] = 2;
        }
    }

    //for(int i=4;i<=19;i++)pf("%d ",ans[i]);
}

int main()
{
    remaining_cards();
    int n;
    while(~sf("%d",&n) and n)
    {
        pf("%d\n",ans[n]);
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number