急急急!!!在线等,由1-9九位数字组成的三个随机数字,其比例是1:2:3用c#

将1到9共9个数分为三组,组成3个三位数,且使这三个三位数构成1:2:3的比例,例如:3个三位数192,384,576=1:2:3,试求出所有满足条件的3个三位数... 将1到9共9个数分为三组,组成3个三位数,且使这三个三位数构成1:2:3的比例,例如:3个三位数192,384,576=1:2:3,试求出所有满足条件的3个三位数 展开
 我来答
百度网友7695b6b
2013-09-23 · TA获得超过165个赞
知道小有建树答主
回答量:126
采纳率:0%
帮助的人:129万
展开全部
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> _L1 = new List<int>();
            int x = 0, y = 0, z = 0;
            for (int i = 1; i < 10; i++) _L1.Add(i);
            foreach (int a in _L1)
            {
             List<int> _L2 = new List<int>(_L1);
             _L2.Remove(a);
             foreach (int b in _L2)
             {
              List<int> _L3 = new List<int>(_L2);
              _L3.Remove(b);
              foreach (int c in _L3)
              {
               x = a * 100 + b * 10 + c;
              List<int> _L4 = new List<int>(_L3);
              _L4.Remove(c);
              foreach (int d in _L4)
              {
               List<int> _L5 = new List<int>(_L4);
               _L5.Remove(d);
               foreach (int e in _L5)
               {
               List<int> _L6 = new List<int>(_L5);
               _L6.Remove(e);
               foreach (int f in _L6)
               {
               y = d * 100 + e * 10 + f;
               List<int> _L7 = new List<int>(_L6);
               _L7.Remove(f);
               foreach (int g in _L7)
               {
               List<int> _L8 = new List<int>(_L7);
               _L8.Remove(g);
               foreach(int h in _L8)
               {
               List<int> _L9 = new List<int>(_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<int> _L1 = new List<int>();
        for (int i = 1; i < 10; i++) _L1.Add(i);
        foreach (int x in _L1)
        {
        List<int> _L2 = new List<int>(_L1);
        _L2.Remove(x);
        foreach (int y in _L2)
        {
        List<int> _L3 = new List<int>(_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<int> _L4 = new List<int>(_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<int> _L5 = new List<int>(_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();
        }
    }
}
丶没好名字
2013-09-23 · TA获得超过143个赞
知道小有建树答主
回答量:374
采纳率:0%
帮助的人:248万
展开全部
哪有什么随机.就一个循环..
List<int> list = new List<int>();
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());
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
紫色浪漫眼
2013-09-23 · TA获得超过247个赞
知道小有建树答主
回答量:577
采纳率:100%
帮助的人:167万
展开全部
List<string> okStr = new List<string>();
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);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
祎正谊25
2013-09-23 · TA获得超过157个赞
知道小有建树答主
回答量:430
采纳率:75%
帮助的人:234万
展开全部
主函数中:
List<int> list = null;
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<int> list, int[] t, int startIndex, int endIndex)
{
if (startIndex == endIndex)
{
if (list == null)
{
list = new List<int>();
}
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<int> GetPermutation(int[] t, int startIndex, int endIndex)
{
if (startIndex < 0 || endIndex > t.Length - 1)
{
return null;
}
List<int> list = new List<int>();
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;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
你给我站着
2013-09-23 · TA获得超过134个赞
知道答主
回答量:163
采纳率:0%
帮助的人:81.3万
展开全部
192 384 576
219 438 657
327 654 981
追问
要完整的控制台程序
追答
程序:
program l_1(input,output);
var
a:array[0..9] of integer;
f:boolean;
i,ii,iii,j:integer;
procedure fl(x:integer);
begin
while x0 do
begin
a[x mod 10]:=a[x mod 10]+1;
x:=x div 10;
end;
end;
begin
for i:=123 to 321 do
begin
fillchar(a,sizeof(a),0)
ii:=i*2;
iii:=i*3;
fl(i);
fl(ii);
fl(iii);
f:=true;
for j:=0 to 9 do
if a[j]=0 then f:=false;
if f=true then writeln(i:5,ii:5,iii:5);
end;
end.
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-09-23
展开全部
编码不是主要的,主要的是清楚里面的逻辑,你能把问题叙述清楚就可以编码,有什么难的,加减乘除,条件判断,循环,
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(4)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式