本人C#菜鸟一只,如有不到之处还请谅解!
问题如下:
Q1:一个楼道有n(>=3)个阶梯 有几种方法上去 例如 3 1 1 1 21 12
Q2:n(>=6)个数去掉其中的一个 成为最小 例如 133427 去掉4 是最小的
PS:简单明了的解答,谢谢您勒!C#算法问题大神
问题如下:
Q1:一个楼道有n(>=3)个阶梯 有几种方法上去 例如 3 1 1 1 21 12
Q2:n(>=6)个数去掉其中的一个 成为最小 例如 133427 去掉4 是最小的
PS:简单明了的解答,谢谢您勒!C#算法问题大神
int[i]=(int)(n.tostring().remove(i, 1));}
int[].min();
手机打字不行第二个我想是这样的
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("problem1(3):");
foreach (var item in problem1(3))
{
Console.WriteLine(string.Join(",", item.Select(x => x.ToString()).ToArray()));
}
Console.WriteLine("problem1(4):");
foreach (var item in problem1(4))
{
Console.WriteLine(string.Join(",", item.Select(x => x.ToString()).ToArray()));
}
Console.WriteLine("problem2(133427):");
Console.WriteLine(problem2(133427));
} static IEnumerable<IEnumerable<int>> problem1(int n)
{
return Enumerable.Range(1, n - 1)
.SelectMany(x => problem1(n - x).Select(y => new int[] { x }.Concat(y))).Concat(new int[][] { new int[] { n }});
} static int problem2(int n)
{
int[] data = n.ToString().Select(x => x - '0').ToArray();
var query = Enumerable.Range(0, data.Length)
.Select(x => new
{
k = data[x],
v = data.Select((y, i) => new { y, i }).Where(y => y.i != x).ToList().Select((y, i) => (int)Math.Pow(10.0, (double)(data.Length - 2 - i)) * y.y).Sum()
});
return query.OrderBy(x => x.v).First().k;
}
}
}problem1(3):
1,1,1
1,2
2,1
3
problem1(4):
1,1,1,1
1,1,2
1,2,1
1,3
2,1,1
2,2
3,1
4
problem2(133427):
4
Press any key to continue . . .