日期:2014-05-19  浏览次数:20754 次

求一个算法(取最合理的数值的问题)
input     output
1.6           2
6.0           6
7.1           8
36.5         40
72             80
91             100
156           200
450           450
789           800
901           950
980           1000
1156         1500
2420         2500
3726         4000
41234       45000
48912       50000
...           ...


请问,谁能给我一个比较好的算法,谢谢.


------解决方案--------------------
public class ConvertNumber
{
public int ConvertNum(double Num)
{
int TemInt = Convert.ToInt32(Num);
if (TemInt < 10)
{
if (Num > TemInt)
{
return TemInt + 1;
}
else
{
return TemInt;
}
}
else
{
int Factor = 5;
int NumLen = TemInt.ToString().Length;
for (int i = 2; i < NumLen; i++)
{
Factor *= 10;
}
int Multiple = TemInt / Factor;
int Residue = TemInt % Factor;
if (TemInt > 0)
{
return (Multiple + 1) * Factor;
}
else
{
return Multiple * Factor;
}

}
}

}
------解决方案--------------------
public int ConvertNum(double num)
{
int factor=(int)Math.Pow(10,(int)Math.Log10(num));
if(factor> 10) factor/=2;
int multi=(int)num/factor;
return multi*factor <num?(multi+1)*factor:(int)num;
}