更新——现在不重复总结试试这个
bool isClose(IEnumerable<decimal> list, decimal epislon) {
return isClose(Enumerable.Empty<decimal>(),list,0,list.Sum(),epislon);
}
// Define other methods and classes here
bool isClose(IEnumerable<decimal> left,IEnumerable<decimal> right, decimal leftSum,decimal rightSum, decimal epsilon) {
if (leftSum>=1-epsilon && leftSum<=1+epsilon) return true;
if (leftSum>1+epsilon) return false;
if (leftSum+right.Sum()< 1-epsilon) return false;
if (!right.Any()) return false;
for (var i=0;i<right.Count();i++) {
var skip=right.Skip(i);
var newItem=skip.First();
if (isClose(left.Concat(skip.Take(1)),skip.Skip(1),leftSum+newItem,rightSum-newItem,epsilon)) return true;
}
return false;
}
isClose(new[] {0.7m,0.7m,0.7m},0.001m); // returns false
isClose(new[] {0.7m,0.3m,0.7m},0.001m); //returns true
isClose(new[] {0.777777m,0.2m,0.1m},0.001m); //returns false
isClose(new[] {0.33333m,0.33333m,0.33333m},0.001m); //returns true
编辑第五次测试
isClose(new[] {0.4m, 0.5m, 0.6m, 0.3m},0.001m); //returns true