SRM454 Div2 Easy(250) MinimalDifference

MinimalDifference

#include <algorithm>

using namespace std;

class MinimalDifference
{
    int sum( int n );
public:
    int findNumber( int A, int B, int C );
};

int MinimalDifference::findNumber( int A, int B, int C )
{
    int diff = 999;
    int X;

    for ( int i=A; i<=B; i++ )
        if ( abs( sum(i) - sum(C) ) < diff )
            diff = abs( sum(i) - sum(C) ),
            X = i;

    return X;
}

int MinimalDifference::sum( int n )
{
    int s = 0;
    for ( ; n>0; n/=10 )
        s += n % 10;
    return s;
}