锻炼下,看谁能弄出来,C#编程技巧!
问:1 2 4 8 16
求 其中的任意一个数 和 其他的任意多个或者一个数的和 的所有值
比如 跟 1有关的一个或者多个数的和为:1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31
跟2 4 6 8 16 的各是多少,
谁可以用编程做出来啊,呵呵,分全部给他,估计高手们一看便会啊!
[解决办法]
没看明白,理解ing
[解决办法]
其实就是求包含某一个元素的组合方式有多少种,当然,要把和一样的过滤掉。
[解决办法]
没看懂题。。。。
[解决办法]
啊~~~明白了!!!看懂了看懂了
[解决办法]
class Program { public static List<int> GetValues(int iFix) { return Enumerable.Range(1, 31).Where(x => (x & iFix) == iFix).ToList<int>(); } static void Main(string[] args) { Console.WriteLine("全部:"); foreach (int value in GetValues(0)) { Console.Write("{0} ", value); } Console.WriteLine(); int[] fixes = new int[] { 1, 2, 4, 8, 16 }; foreach (int fix in fixes) { Console.WriteLine("{0}相关:", fix); foreach (int value in GetValues(fix)) { Console.Write("{0} ", value); } Console.WriteLine(); } Console.ReadKey(); } }
[解决办法]
var array = new int[]{1, 2, 4, 8, 16};var seed = 1; for (int i = 0; i < array.Length; i++){ if (array[i] == seed) continue; for (int j = 0; j < array.Length ; j++) { if (array[j] == seed) continue; Console.WriteLine(array.Skip(i).Take(j).Aggregate(seed, (a, b) => a + b)); }}
[解决办法]