using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List_L1 = new List ();
int x = 0, y = 0, z = 0;
for (int i = 1; i < 10; i++) _L1.Add(i);
foreach (int a in _L1)
{
List_L2 = new List (_L1);
_L2.Remove(a);
foreach (int b in _L2)
{
List_L3 = new List (_L2);
_L3.Remove(b);
foreach (int c in _L3)
{
x = a * 100 + b * 10 + c;
List_L4 = new List (_L3);
_L4.Remove(c);
foreach (int d in _L4)
{
List_L5 = new List (_L4);
_L5.Remove(d);
foreach (int e in _L5)
{
List_L6 = new List (_L5);
_L6.Remove(e);
foreach (int f in _L6)
{
y = d * 100 + e * 10 + f;
List_L7 = new List (_L6);
_L7.Remove(f);
foreach (int g in _L7)
{
List_L8 = new List (_L7);
_L8.Remove(g);
foreach(int h in _L8)
{
List_L9 = new List (_L8);
_L9.Remove(h);
z = g * 100 + h * 10 + _L9[0];
if (y == x * 2 && z == x * 3)
Console.WriteLine(x + "," + y + "," + z);
}
}
}
}
}
}
}
}
Console.ReadKey();
}
}
}
没写那么细 ,数字又不多 直接简单一点穷举了就行了
要么也可以这么干,你自己选择吧,下面的这个算起来不上面快很多,写起来也简单,但是。。你懂得。。
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List_L1 = new List ();
for (int i = 1; i < 10; i++) _L1.Add(i);
foreach (int x in _L1)
{
List_L2 = new List (_L1);
_L2.Remove(x);
foreach (int y in _L2)
{
List_L3 = new List (_L2);
_L3.Remove(y);
foreach (int z in _L3)
{
int i = x * 100 + y * 10 + z;
int ii = i * 2;
char[] cii = ii.ToString().ToCharArray();
if (cii[0] == cii[1] || cii[0] == cii[2] || cii[1] == cii[2]) continue;
List_L4 = new List (_L3);
_L4.Remove(z);
foreach (char c in cii)
{
if (!_L4.Contains(int.Parse(c.ToString())))goto H1;
}
int iii = i * 3;
char[] ciii=iii.ToString().ToCharArray();
if (ciii[0] == ciii[1] || ciii[0] == cii[2] || ciii[1] == ciii[2]) continue;
List_L5 = new List (_L4);
foreach (char c in cii) _L5.Remove(int.Parse(c.ToString()));
foreach (char c in ciii)
{
if (!_L5.Contains(int.Parse(c.ToString())))
goto H1;
}
Console.WriteLine(i + "," + ii + "," + iii);
H1: ;
}
}
}
Console.ReadKey();
}
}
}
哪有什么随机.就一个循环..
List
for (int k = 111; k <= 333; k++)
{
list.Add(k);
}
foreach (int s in list)
{
Console.WriteLine(s.ToString()+" "+(s*2).ToString()+" "+(s*3).ToString());
}
List
for (int i = 100; i <= 333; i++)
{
int a = i;
int b = i * 2;
int c = i * 3;
string d = a + "\t" + b + "\t" + c + "\t";
bool flag = true;
for (int j = 1; j <= 9; j++)
{
if (d.Contains(j.ToString()[0]) == false)
{
flag = false;
break;
}
}
if (flag)
{
okStr.Add(d);
}
}
foreach (string item in okStr)
{
Console.WriteLine(item);
}
主函数中:
List
int[] t = new int[9] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
GetPermutation(ref list, t, 0, 8);
方法:
private static void GetPermutation(ref List
{
if (startIndex == endIndex)
{
if (list == null)
{
list = new List
}
int[] temp = new int[t.Length];
t.CopyTo(temp, 0);
string tempi = "";
foreach (int i in temp)
{
tempi += i.ToString();
}
int first = Convert.ToInt32(tempi.Substring(0, 3));
int middle = Convert.ToInt32(tempi.Substring(3, 3));
int last = Convert.ToInt32(tempi.Substring(6, 3));
if ((middle == 2 * first) && (last == 3 * first))
{
list.Add(Convert.ToInt32(tempi));
}
}
else
{
for (int i = startIndex; i <= endIndex; i++)
{
Swap(ref t[startIndex], ref t[i]);
GetPermutation(ref list, t, startIndex + 1, endIndex);
Swap(ref t[startIndex], ref t[i]);
}
}
}
public static List
{
if (startIndex < 0 || endIndex > t.Length - 1)
{
return null;
}
List
GetPermutation(ref list, t, startIndex, endIndex);
return list;
}
public static void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
192 384 576
219 438 657
327 654 981
编码不是主要的,主要的是清楚里面的逻辑,你能把问题叙述清楚就可以编码,有什么难的,加减乘除,条件判断,循环,