[topcoder]CorrectMultiplicationTwo

http://community.topcoder.com/stat?c=problem_statement&pm=11609&rd=14547

http://apps.topcoder.com/wiki/display/tc/SRM+522

这道题学到的:1.如果要搜索或遍历,至少要知道边界,这里用了1*1==1找到了边界;然后的优化是遍历A的可能性时,B只要在c/A左右找就行了。如果整除了,当然是这种情况的最小,-1和1用来mitigate不整除的情况。如果B再偏多一些,比如1,那么C就会偏出A,abs肯定会更大。

import java.math.*;
public class CorrectMultiplicationTwo
{
	public int getMinimum(int a, int b, int c)
	{
		int res = (a + b + c) -3;
		int min = res;
		for (int A = 1; A <= (a+res); A++)
		{
			for (int o = -1; o <= 1; o++)
			{
				int B = c/A + o;
				if (B>=1)
				{
					int C = A * B;
					int tmp = Math.abs(A-a)+Math.abs(B-b)+Math.abs(C-c);
					if (tmp < min) min = tmp;
				}
			}
		}
		return min;
	}
}

  

原文地址:https://www.cnblogs.com/lautsie/p/3338428.html