none
请帮忙看下这个程序的逻辑错误,自己找不到哪里错了,谢了 RRS feed

  • 问题

  • 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;
    namespace WindowsFormsApplication6
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
       
                int[,] q = new int[5, 5] {{0,1,0,1,0},{1,0,0,0,1},{0,0,1,1,0},{1,0,1,0,1},{1,0,1,0,0}};
                int[,] a = new int[5, 5]; int k = 0; int[] s = new int[6] { 0, 0, 0, 0, 0, 0 };
                int[] c = new int[5]{0,0,0,0,0};
                Random random = new Random();
                for(int i=0;i<5;i++)
                    for (int j = 0; j < 5; j++)
                        a[i, j] = q[i, j];
                for (int i = 0; i < 5; i++)
                {
                   
                    for (int j = 0; j < 5; j++)
                    {
                        if (i == 0)
                        {
                            if (a[i, j] == 1)
                            {
                                c[j] = random.Next(1, 10);
                                k++; s[k] = j;
                                if (k > 1)
                                {
                                    for (int b = 1; b < k; b++)
                                    {
                                        if (c[s[k]] == c[s[b]])
                                        {
                                            int flag = 1;
                                            while (flag == 1)
                                            {
                                                int u = 0;
                                                c[s[k]] = random.Next(1, 10);
                                                for (int r = 1; r < k; r++)
                                                {
                                                    if (c[s[k]] == c[s[r]])
                                                        u = 1;
                                                }
                                                if (u == 0)
                                                    flag = 0;
                                            }
                                            if (flag == 0)
                                                break;

                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                                    int fl = 0;
                                    if (a[i, j] == 1)
                                    {
                                        for (int z = 1; z <= k; z++)
                                        {
                                            if (a[z, j] == 1)
                                                fl = 1;
                                        }
                                        if (fl == 0)
                                        {
                                            k++; s[k] = j; c[j] = random.Next(1, 10);
                                            for (int g = 1; g < k; g++)
                                            {
                                               
                                                if (c[j] == c[s[g]])
                                                {
                                                    int fla = 1;
                                                    while (fla == 1)
                                                    {
                                                        int u = 0;
                                                        c[j] = random.Next(1, 10);
                                                        for (int r = 1; r < k; r++)
                                                        {
                                                            if (c[j] == c[s[r]])
                                                                u = 1;
                                                        }
                                                        if (u == 0)
                                                            fla = 0;
                                                    }
                                                    if (fla == 0)
                                                        break;
                                                }
                                            }
                                        }
                                    }
                            }
                    }

                }
                label1.Text = "";
                for (int i = 0; i < 5; i++)
                    label1.Text += c[i] + "\n";


            }

        }
    }

    2012年12月27日 6:41