for (int i = 1; i <= 3; ++i) // 400的三倍超过999了,肯定不行 { for (int j = 1; j <= 9; ++j) { if (j == i) continue; for (int k = 1; k <= 9; ++k) { if (k == i || k == j) continue; int n1 = i*100 + j*10 + k; int n2 = 2 * n1; int n3 = 3 * n1; int t; t = n2 % 10; // 个位数 if (t == i || t == j || t == k) continue; t = n2 % 100 / 10; // 十位数 if (t == i || t == j || t == k) continue; t = n2 / 100; // 百位数 if (t == i || t == j || t == k) continue; if (n3 > 999) continue; t = n3 % 10; if (t == i || t == j || t == k) continue; t = n3 % 100 / 10; if (t == i || t == j || t == k) continue; t = n3 / 100; if (t == i || t == j || t == k) continue; Console.WriteLine("{0} {1} {2}", n1, n2, n3); } } }
int x,y,z; int a,b,c;
for(x=1;x<4;x++) { for(y=1;y<10;y++) { if(x==y) continue; for(z=1;z<10;z++) { if(z==x||z==y) continue; a = x*100+y*10+z; b = 2*a; c = 3*a; int i = b/100; //百位数字 if(i==x||i==y||i==z) continue; int j = b%100/10; //十位数字 if(j==x||j==y||j==z) continue; //与 a 的所以数字比较 if(j==i||j==0) continue; //比较本身的数字 int k = b%100%10; //个位数字 if(k==x||k==y||k==z) continue; //与 a 的所有数字 比较 if(k==i||k==j||k==0) continue; //比较本身的数字 int t = c/100; if(t==x||t==y||t==z||t==i||t==j||t==k) continue; int m = c%100/10; if(m==x||m==y||m==z||m==i||m==j||m==k) continue; if(m==t||m==0) continue; int n = c%100%10; if(n==x||n==y||n==z||n==i||n==j||n==k) continue; if(n==t||n==m||n==0) continue; if(c>987) continue; this.richTextBox1.Text += a.ToString()+" "+b.ToString()+" "+c.ToString()+"\n"; } } }
晕了 -_-! 第一次写的代码错了 改了一下,得到结果 192 384 576 219 438 657 273 546 819 327 654 981 for (int i = 1; i <= 3; ++i) { for (int j = 1; j <= 9; ++j) { if (j == i) continue; for (int k = 1; k <= 9; ++k) { if (k == i || k == j) continue; int n1 = i*100 + j*10 + k; int n2 = 2 * n1; int n3 = 3 * n1; int a = n2 % 10; if (a == i || a == j || a == k || a == 0) continue; int b = n2 % 100 / 10; if (b == i || b == j || b == k || b == a || b == 0) continue; int c = n2 / 100; if (c == i || c == j || c == k || c == a || c == b || c == 0) continue; if (n3 > 999) continue; int d = n3 % 10; if (d == i || d == j || d == k || d == a || d == b || d == c || d == 0) continue; int e = n3 % 100 / 10; if (e == i || e == j || e == k || e == a || e == b || e == c || e == d || e == 0) continue; int f = n3 / 100; if (f == i || f == j || f == k || f == a || f == b || f == c || f == d || f == e || f == 0) continue; Console.WriteLine("{0} {1} {2}", n1, n2, n3); } } }
全排列的数法参考 木野狐的学习记录http://www.cnblogs.com/RChen/archive/2005/06/21/178268.html有//为本人所写,其他来自上述BLOG using System; using System.Collections.Generic; using System.Text;namespace Cons012001 { class Program { static void Main(string[] args) { char[] s = "123456789".ToCharArray(); TotalSort(s, 0); Console.ReadLine(); } static int resultCount = 0; public static void TotalSort(char[] list, int start) { int end = list.Length - 1;
if (start == end) { resultCount++; string ss =new String(list); //1 int a = Convert.ToInt16(ss.Substring(0, 3));//2 int b = Convert.ToInt16(ss.Substring(3, 3));//3 int c = Convert.ToInt16(ss.Substring(6, 3));//4 if (a * 2 == b) //5 { if (a * 3 == c) Console.WriteLine(a.ToString()+" "+b.ToString() +" "+c.ToString()); } //6 } else { for (int i = start; i <= end; i++) { char[] temp = new char[list.Length]; list.CopyTo(temp, 0); char tempc = temp[start]; temp[start] = temp[i]; temp[i] = tempc; if (Convert.ToInt16(new String(list).Substring(0, 1)) <=3 ) //7(关键) TotalSort(temp, start + 1); } } } } }
从他楼主的意思来说的话: 第一个数是从123到329,这样一来的话我就把这206个数做个FOR循环,循环的目的就是把这206个对应的第二,第三个数组成一个9位的数(123246369),再进行一个IF判断,把每一位的数相加看等于45不(1到9不重复的话相加就是45),这样就会得到21组每位相加为45的9位数。最后对这21组数进行一个IF判断,把每一位的数相乘看等于362880不(1到9不重复的话相加就是362880),最后得到的就是楼上的结果。 下面是我用VB写的一段代码,望大家指点指点: Private Sub Form_Load() Dim a, b, c, d, e, f, g As Integer End Sub Private Sub Form_Paint() For a = 123 To 329 g = 0 b = a * 2 c = a * 3 d = a & b & c For e = 1 To 9 g = g + Mid(d, e, 1) Next e If g = 45 Then If Mid(d, 1, 1) * Mid(d, 2, 1) * Mid(d, 3, 1) * Mid(d, 4, 1) * Mid(d, 5, 1) * Mid(d, 6, 1) * Mid(d, 7, 1) * Mid(d, 8, 1) * Mid(d, 9, 1) <> 362880 Then Else Print a; " " & b; " " & c End If Else End If Next a End Sub
线性代数...
矩阵方程...
设第一个数是x*100+y*10+z
第二个的各位就是(2*z>10?2*z-10:2*z)
类推...当然,更偷懒的方法就是一个循环
第一个数只能是102到325之间,做一个for,也能求出...
{
for (int j = 1; j <= 9; ++j)
{
if (j == i) continue;
for (int k = 1; k <= 9; ++k)
{
if (k == i || k == j) continue;
int n1 = i*100 + j*10 + k;
int n2 = 2 * n1;
int n3 = 3 * n1;
int t;
t = n2 % 10; // 个位数
if (t == i || t == j || t == k) continue;
t = n2 % 100 / 10; // 十位数
if (t == i || t == j || t == k) continue;
t = n2 / 100; // 百位数
if (t == i || t == j || t == k) continue; if (n3 > 999) continue;
t = n3 % 10;
if (t == i || t == j || t == k) continue;
t = n3 % 100 / 10;
if (t == i || t == j || t == k) continue;
t = n3 / 100;
if (t == i || t == j || t == k) continue;
Console.WriteLine("{0} {1} {2}", n1, n2, n3);
}
}
}
int a,b,c;
for(x=1;x<4;x++)
{
for(y=1;y<10;y++)
{
if(x==y) continue;
for(z=1;z<10;z++)
{
if(z==x||z==y) continue; a = x*100+y*10+z;
b = 2*a;
c = 3*a;
int i = b/100; //百位数字
if(i==x||i==y||i==z) continue; int j = b%100/10; //十位数字
if(j==x||j==y||j==z) continue; //与 a 的所以数字比较
if(j==i||j==0) continue; //比较本身的数字 int k = b%100%10; //个位数字
if(k==x||k==y||k==z) continue; //与 a 的所有数字 比较
if(k==i||k==j||k==0) continue; //比较本身的数字 int t = c/100;
if(t==x||t==y||t==z||t==i||t==j||t==k) continue; int m = c%100/10;
if(m==x||m==y||m==z||m==i||m==j||m==k) continue;
if(m==t||m==0) continue; int n = c%100%10;
if(n==x||n==y||n==z||n==i||n==j||n==k) continue;
if(n==t||n==m||n==0) continue; if(c>987) continue; this.richTextBox1.Text += a.ToString()+" "+b.ToString()+" "+c.ToString()+"\n";
}
}
}
改了一下,得到结果
192 384 576
219 438 657
273 546 819
327 654 981
for (int i = 1; i <= 3; ++i)
{
for (int j = 1; j <= 9; ++j)
{
if (j == i) continue;
for (int k = 1; k <= 9; ++k)
{
if (k == i || k == j) continue;
int n1 = i*100 + j*10 + k;
int n2 = 2 * n1;
int n3 = 3 * n1;
int a = n2 % 10;
if (a == i || a == j || a == k || a == 0) continue;
int b = n2 % 100 / 10;
if (b == i || b == j || b == k || b == a || b == 0) continue;
int c = n2 / 100;
if (c == i || c == j || c == k || c == a || c == b || c == 0) continue; if (n3 > 999) continue;
int d = n3 % 10;
if (d == i || d == j || d == k || d == a || d == b || d == c || d == 0) continue;
int e = n3 % 100 / 10;
if (e == i || e == j || e == k || e == a || e == b || e == c || e == d || e == 0) continue;
int f = n3 / 100;
if (f == i || f == j || f == k || f == a || f == b || f == c || f == d || f == e || f == 0) continue;
Console.WriteLine("{0} {1} {2}", n1, n2, n3);
}
}
}
木野狐的学习记录http://www.cnblogs.com/RChen/archive/2005/06/21/178268.html有//为本人所写,其他来自上述BLOG
using System;
using System.Collections.Generic;
using System.Text;namespace Cons012001
{
class Program
{
static void Main(string[] args)
{
char[] s = "123456789".ToCharArray();
TotalSort(s, 0);
Console.ReadLine(); }
static int resultCount = 0; public static void TotalSort(char[] list, int start)
{
int end = list.Length - 1;
if (start == end)
{
resultCount++;
string ss =new String(list); //1
int a = Convert.ToInt16(ss.Substring(0, 3));//2
int b = Convert.ToInt16(ss.Substring(3, 3));//3
int c = Convert.ToInt16(ss.Substring(6, 3));//4 if (a * 2 == b) //5
{ if (a * 3 == c) Console.WriteLine(a.ToString()+" "+b.ToString() +" "+c.ToString()); } //6
}
else
{
for (int i = start; i <= end; i++)
{
char[] temp = new char[list.Length];
list.CopyTo(temp, 0);
char tempc = temp[start];
temp[start] = temp[i];
temp[i] = tempc;
if (Convert.ToInt16(new String(list).Substring(0, 1)) <=3 ) //7(关键)
TotalSort(temp, start + 1);
}
}
} }
}
#include <conio.h>main()
{
int n1,n2,n3,n4,n5,n6,n7,n8,n9;
int x=0,y=0,z=0;
for(n1=1;n1<=9;n1++)
{
for(n2=1;n2<=9;n2++)
{
if(n2==n1)
continue;
for(n3=1;n3<=9;n3++)
{
if((n3==n1)||(n3==n2))
continue;
x=n1*100+n2*10+n3;
for(n4=1;n4<=9;n4++)
{
if((n4==n1)||(n4==n2)||(n4==n3))
continue;
for(n5=1;n5<=9;n5++)
{
if((n5==n1)||(n5==n2)||(n5==n3)||(n5==n4))
continue;
for(n6=1;n6<=9;n6++)
{
if((n6==n1)||(n6==n2)||(n6==n3)||(n6==n4)||(n6==n5))
continue;
y=n4*100+n5*10+n6;
for(n7=1;n7<=9;n7++)
{
if((n7==n1)||(n7==n2)||(n7==n3)||(n7==n4)||(n7==n5)||(n7==n6))
continue;
for(n8=1;n8<=9;n8++)
{
if((n8==n1)||(n8==n2)||(n8==n3)||(n8==n4)||(n8==n5)||(n8==n6)||(n8==n7))
continue;
for(n9=1;n9<=9;n9++)
{
if((n9==n1)||(n9==n2)||(n9==n3)||(n9==n4)||(n9==n5)||(n9==n6)||(n9==n7)||(n9==n8))
continue;
z=n7*100+n8*10+n9;
if((y==(2*x))&&(z==(3*x)))
{
printf("%d:%d:%d\r\n",x,y,z);
getch();
}
}
}
}
}
}
}
}
}
}
}
结果:
192 384 576
219 438 657
273 546 819
327 654 981
#include <conio.h>main()
{
int n1,n2,n3,n4,n5,n6,n7,n8,n9;
int x=0,y=0,z=0;
for(n1=1;n1<=9;n1++)
{
for(n2=1;n2<=9;n2++)
{
if(n2==n1)
continue;
for(n3=1;n3<=9;n3++)
{
if((n3==n1)||(n3==n2))
continue;
x=n1*100+n2*10+n3;
for(n4=1;n4<=9;n4++)
{
if((n4==n1)||(n4==n2)||(n4==n3))
continue;
for(n5=1;n5<=9;n5++)
{
if((n5==n1)||(n5==n2)||(n5==n3)||(n5==n4))
continue;
for(n6=1;n6<=9;n6++)
{
if((n6==n1)||(n6==n2)||(n6==n3)||(n6==n4)||(n6==n5))
continue;
y=n4*100+n5*10+n6;
for(n7=1;n7<=9;n7++)
{
if((n7==n1)||(n7==n2)||(n7==n3)||(n7==n4)||(n7==n5)||(n7==n6))
continue;
for(n8=1;n8<=9;n8++)
{
if((n8==n1)||(n8==n2)||(n8==n3)||(n8==n4)||(n8==n5)||(n8==n6)||(n8==n7))
continue;
for(n9=1;n9<=9;n9++)
{
if((n9==n1)||(n9==n2)||(n9==n3)||(n9==n4)||(n9==n5)||(n9==n6)||(n9==n7)||(n9==n8))
continue;
z=n7*100+n8*10+n9;
if((y==(2*x))&&(z==(3*x)))
{
printf("%d:%d:%d\r\n",x,y,z);
getch();
}
}
}
}
}
}
}
}
}
}
}
结果:
192 384 576
219 438 657
273 546 819
327 654 981
第一个数是从123到329,这样一来的话我就把这206个数做个FOR循环,循环的目的就是把这206个对应的第二,第三个数组成一个9位的数(123246369),再进行一个IF判断,把每一位的数相加看等于45不(1到9不重复的话相加就是45),这样就会得到21组每位相加为45的9位数。最后对这21组数进行一个IF判断,把每一位的数相乘看等于362880不(1到9不重复的话相加就是362880),最后得到的就是楼上的结果。
下面是我用VB写的一段代码,望大家指点指点:
Private Sub Form_Load()
Dim a, b, c, d, e, f, g As Integer
End Sub
Private Sub Form_Paint()
For a = 123 To 329
g = 0
b = a * 2
c = a * 3
d = a & b & c
For e = 1 To 9
g = g + Mid(d, e, 1)
Next e
If g = 45 Then
If Mid(d, 1, 1) * Mid(d, 2, 1) * Mid(d, 3, 1) * Mid(d, 4, 1) * Mid(d, 5, 1) * Mid(d, 6, 1) * Mid(d, 7, 1) * Mid(d, 8, 1) * Mid(d, 9, 1) <> 362880 Then
Else
Print a; " " & b; " " & c
End If
Else
End If
Next a
End Sub