ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1014. Product of Digits

Correct solution on C++ (AC 0.015, Memory 225 kb)
Posted by htzfun 3 Jan 2012 17:19
The idea is to find divider for n in set between 2 and 9.
If there is no such dividers we'll have answer -1. If yes, we divide n by this divider and do it again, until n isn't equal 1.
Realization:

#include <iostream>
#include <math.h>
#include <cstdio>
#include <string.h>
using namespace std;
void main(void)
{
    long long int n;
    cin>>n;
    if (n==0)
        {
            cout<<"10" ;
            return;
        }
    if (n==1)
    {
        cout<<"1";
        return;
    }
    int *a = new int[14];
    for(int i = 0;i<14;i++)
    {
        a[i] = 0;
    }
    int counter  = 13;
    while(n>1)
    {
        for(int j = 9;j>=0; j--)
        {
            if (j==1)
                {
                    cout<<"-1";

                    return;
                }
            if (n%j==0)
            {
                a[counter] = j;
                counter--;
                n/=j;
                break;
            }
        }
    }
    for(int i = counter+1 ;i<14;i++)
    {
        cout<<a[i];
    }
    return;
}

Sorry, for my English and for some unnecessary includes or code=)