Search Here

Tuesday, June 30, 2015

UVa - 1583 ( Digit Generator Solution )

Try Yourself First. For help Scroll Down .

Clearance : Another easy problem . Although it suffers much while I was trying to solve it . All you have to do is to pre-calculate the results for all number .....  Take an Array and Initially assign all the array value as Zero . Run a Loop from 1 to 100000 and Check whether the number's digit sum is equal to ZERO or Less than or Equal to any other result and then store it .

Code :


#include<bits/stdc++.h>

using namespace std;

int res[100000]={0};
int main()
{
    for(int i=1; i<=100000; i++)
    {
        int num = i + (i/10000) + (i/1000)%10 + (i/100)%10 + (i/10)%10 + i%10;
        if(res[num]==0 || i<res[num])
            res[num] = i;
    }
    int n, m;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>m;
        cout<<res[m]<<endl;
    }
    return 0;
}

=> Question ? Leave a Comment .

No comments:

Post a Comment