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();
}
}
------解决方案--------------------
C# code
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));
}
}
------解决方案--------------------