作业帮 > 综合 > 作业

用C#语言将1,2,3...9九位数分成三组,组成3个三位数且满足1:2:3的比例,

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/07/20 05:15:48
用C#语言将1,2,3...9九位数分成三组,组成3个三位数且满足1:2:3的比例,
之前回答过同样的问题.这个就是给这道题写的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();
        }
    }
}