Uva 10879 - Code Refactoring

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

#define sf scanf
#define pf printf

using namespace std;

typedef long long LL;

LL ar[5+2];

void soln(LL n)
{
    int chk=2,len=0;

    for(LL i=2; i*i<=n; i++)
    {
        if(!(n % i))
        {
            if(chk < 5)
            {
                if( (n/i) != i)
                {
                    chk+=2;
                    //pf("%lld  %lld\n", i, n/i);
                    ar[len++]=i;
                    ar[len++]=n/i;
                }
            }
        }
    }

    pf("%lld = ",n);

    if(len < 4)
    {
        pf("%lld * %lld = %lld * %lld\n",ar[0],ar[1],ar[1],ar[0]);
    }
    else
    {
        bool f=false;

        for(int i=0;i<len;i++)
        {
            if(i == 2)
            {
                pf(" = %lld",ar[i]);
                continue;
            }
            if(!f)
            {
                pf("%lld",ar[i]);
                f=true;
                continue;
            }
            else
            {
                pf(" * %lld",ar[i]);
            }
        }
    }

    puts("");
}

int main()
{
    int t,tc=0;
    sf("%d",&t);
    while(t--)
    {
        LL num;
        sf("%lld",&num);
        soln(num);
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number