C#2X8 ARRAY放置类似物品 [英] C# 2X8 ARRAY Placing Similar Items‏

查看:52
本文介绍了C#2X8 ARRAY放置类似物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,







我有5种材料:A,B,C,D, E $>






这些材料的总金额16


我需要将它们分配给2x8阵列,但我需要尽可能远地放置类似的物品,如果可能的话,还有simetric的地方



例如,如果金额每个项目包含以下



A = 2,B = 2,C = 3,D = 4,E = 5



手动我可以按以下顺序放置它们



ECDABCED



DECBADEE



我随机尝试了太多东西,但程序挂起了循环



你能不能给我任何东西想法



谢谢



Velid。





Hello,



I have 5 materials: A,B,C,D,E



Total amount of these materials 16

I need to assign them to 2x8 array, but I need to place similar items far as possible and if possible simetric place

For example if the amount of each items contain following

A= 2, B= 2, C=3, D=4, E=5

Manually I can place them in the following order

E C D A B C E D

D E C B A D E E

I tried too many thing with random, but the program hangs in loop

Could you give me any idea

Thanks

Velid.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Login
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            Random rnd = new Random();
            richTextBox3.Text = "";
            // I have 5 kind of material, each of them have the following amount:
            // type A=2
            // type B=2
            // type C=3
            // type D=4
            // type E=5
            int[] materials = { 2, 2, 3, 4, 5 };
            int Xa = materials[0];  // material A amount
            int Xb = materials[1];  // material B amount
            int Xc = materials[2];
            int Xd = materials[3];
            int Xe = materials[4];
            //  I need to place them in 2 row * 8 column
            // when doing this assignment I should place the similar materials far away as possible
            // It possible symetric order
            string[,] array = new string[2,8];

            Array.Clear(array, 0, array.Length);
            array[0, 0] = "E"; // As beginnig I assigned to (0,0) the most biggest material(E)
            Xe = Xe - 1;
            array[1, 0] = "D";// For the second row (1,0) the second biggest material (D)
            Xd = Xd - 1;
            // first row
            for (int j = 1; j < 8; j++) // I used the first column for D and E material
                {
                    int random = rnd.Next(0, 5); // I randomly choose one of the material
                    // if the column is blank (not assigned before and
                    // If the random material is 0 (A type) and the amount of A is not 0
                    // and the previous column is not assigned with A type
                    if (array[0,j]=="" && random == 0 && Xa != 0 && array[0,j-1]!="A")
                    {
                            array[0,j] = "A"; // assign A to this column
                            Xa = Xa - 1; // reduce A amount
                            richTextBox3.Text += "A"; // write "A" in text box
                            richTextBox3.Text += " ";
                    }
                    // B type condition
                    else if (array[0,j]=="" && random == 1 && Xb != 0 && array[0,j-1]!="B")
                    {
                            array[0,j] = "B"; // assign B to this column
                            Xb = Xb - 1; // reduce B amount
                            richTextBox3.Text += "B"; // write "B" in text box
                            richTextBox3.Text += " "; 
                    }
                    // C type condition
                    else if (array[0, j] == "" && random == 2 && Xc != 0 && array[0, j - 1] != "C")
                    {
                        array[0, j] = "C"; // assign C to this column
                        Xc = Xc - 1; // reduce C amount
                        richTextBox3.Text += "C"; // write "C" in text box
                        richTextBox3.Text += " ";
                    }
                         // D type condition
                    else if (array[0, j] == "" && random == 3 && Xd != 0 && array[0, j - 1] != "D")
                    {
                        array[0, j] = "D"; // assign D to this column
                        Xd = Xd - 1; // reduce D amount
                        richTextBox3.Text += "D"; // write "D" in text box
                        richTextBox3.Text += " ";
                    }
                    // E type condition
                    else if (array[0, j] == "" && random == 4 && Xd != 0 && array[0, j - 1] != "E")
                    {
                        array[0, j] = "E"; // assign E to this column
                        Xe = Xe - 1; // reduce E amount
                        richTextBox3.Text += "E"; // write "C" in text box
                        richTextBox3.Text += " ";
                    }
                    else j = j - 1; // if all above not true try again
                }
            }

        // test eğer aynı karakterler yanyana gelmişmi

        private void button4_Click(object sender, EventArgs e)

        {
        }
        }
        }

推荐答案

是,使用调试器,但不是马上。首先,通过你的非代码和编写代码。即使在早期开发阶段,您也必须以或多或少的方式做事。这并不是真的比以这种凌乱的方式做得更难。



首先,不要硬编码ABCD。甚至不要硬编码或。 (顺便说一句,为什么空格,为什么不在所有情况下 string.Empty ?你必须重新考虑它。)



定义,例如,
Yes, use the debugger, but not right away. First, through out your non-code and write code. Even at early development stage, you have to do thing in more or less neat way. This is not really harder then doing it in such a messy way.

To start with, don't hard-code ABCD. Don't even hard-code " " or "". (By the way, why blank space, why not string.Empty in all cases? You have to rethink it.)

Define, say,
enum Materials = { A=2, B=2, C=3, D=4, E=5, }



然后你可以根据需要枚举它们。请参阅我的文章:​​ 枚举类型不会枚举!解决.NET和语言限制 。请注意,我在背景:枚举类之前的生活部分中给出的简单建议可能不起作用,因为2出现两次,因此您可以使用我的类。或者,在紧要关头,更简单。



现在,在生命周期中只创建一次随机实例应用程序并使用查看您可以从当前时间获取的值随机化它: https: //msdn.microsoft.com/en-us/library/ctssatww%28v=vs.110%29.aspx (请参阅使用时间的代码示例)。



重复随机构造函数是一个错误的常见错误,非常糟糕,它会在数据中产生不必要的关联,并且它实际上并不是真正的随机或伪随机。 br />


另外,不要立即生成 richTextBox3.Text 。开发只创建数据的抽象算法(不要将数据与表示数据的字符串混合)。最后,如果需要,可以从数据生成字符串。永远不要使用自动生成的名称,如 richTextBox3 。这些名称违反(良好)Microsoft命名约定,并非真正设计为永久使用它们将所有内容重命名为语义名称;使用重新分解引擎,它真的很容易。无语义名称使代码无法读取。



不要硬编码2,3等。准确编写代码。



完成所有这些后,它会变得更加清晰,所以如果不是,那么你的bug可能会显而易见。如果没有使用调试器。



对不起,我不能确切地告诉你如何安排你的阵列,原因很简单:你只给出了例子,没有正式指定要求。你应该这样做,即使是你自己。这样的配方可以是解决方案的一半,甚至更多。



并享受一些乐趣 - 今天是4月1日。恭喜!



我想利用这个机会邀请你(以及其他所有人)看到我4月份新出版的1并有一些乐趣: br />
Neuro的一些编程方法 -Linguistic Programming

特别鼓励在评论和讨论中参与此游戏。



乐趣和嘲讽应该与讽刺形式的讽刺形式相结合,关于我们如何进行讨论,提供和使用建议,添加参考和使用它们,使用批判性思维,学习和使用我们自己的大脑。



这些建议应该对你的工作有所帮助。



谢谢。

-SA


Then you can enumerate them if you need to. Please see my article: Enumeration Types do not Enumerate! Working around .NET and Language Limitations. Note that simple recommendation I gave in the section "Background: Life before Enumeration Class" might not work because 2 appears twice, so you might use my class. Or, in a pinch, something simpler.

Now, create Random instance only once in the lifetime of the application and randomize it using see value you can take from current time: https://msdn.microsoft.com/en-us/library/ctssatww%28v=vs.110%29.aspx (see code sample for using time).

Repeated Random constructor is a bad common mistake, very bad, it creates unwanted correlation in data and it make, essentially, not really random or pseudo-random.

Also, don't generate richTextBox3.Text right away. Develop abstract algorithm which creates just data (don't mix data with strings representing data). And at the very end, generated string from data, if you want it. Never ever use auto-generated names like richTextBox3. Such names violate (good) Microsoft naming conventions, not really designed to use them permanently Rename everything into semantic names; with re-factoring engine, it's really easy. No-semantic names make code unreadable.

Don't hard-code 2, 3, etc. Write code accurately.

After you do all that, it will become much clearer, so your bug may appear apparent, if not go at all. If not use the debugger.

Sorry, I cannot tell you exactly how to arrange your arrays, by one simple reason: you gave only the example, did not formally specify requirements. You should do it, even for yourself. Such formulation can be half of the solution, or even more.

And have some fun — today is April 1st. Congratulations!

I would like to use the occasion to invite your (and everyone else, of course) to see my new 1 of April publication and have some fun:
Some Programming Approaches to "Neuro-Linguistic Programming".
Participation in this game in Comments and Discussions is especially encouraged.

The fun and mocking is supposed to be combined with quite serious hints, in a satirical form, on how we can conduct our discussions, give and use advice, add references and use them, use critical thinking, learn and use our own brains.

These recommendations should really help in your work.

Thank you.

—SA


这篇关于C#2X8 ARRAY放置类似物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆