code.club

 找回密碼
 立即註冊
搜索
查看: 5391|回復: 4
打印 上一主題 下一主題

用亂數選出不重複的數字陣列

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2021-1-23 10:43:47 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
class Program
    {
        static int max =10;
        static Random r = new Random();
        static int[] a = new int[max];
        
        static bool checkOK(int n)
        {
            for(int i=0;i<max;i++)
            {
                if(n==a[i])
                {
                    return false;
                    
                }
              
            }
            return true;
        }
        static void Main(string[] args)
        {
            int x = 0;
            int i = 0;
            for (int k = 0; k < max; k++)
                a[k] = 999;
            while(i<max)
            {
                x = r.Next(1, max+1);
                if(checkOK(x))
                {
                    a[i] = x;
                    i++;
                }
            }

            for (int j = 0; j < max; j++) Console.WriteLine(a[j]);
        }

    }
回復

使用道具 舉報

沙發
 樓主| 發表於 2021-1-26 18:15:49 | 只看該作者
本帖最後由 2266 於 2021-6-27 18:12 編輯

好像這是更簡單的寫法

  1. static void randomPickNoRepeat(int[] A, int n)
  2.         {
  3.             Random rnd = new Random();
  4.             int x = 0;
  5.             bool OK = true; //OK 做為檢查該數字是否可以放入陣列的旗標
  6.             for (int i = 0; i < n; i++)
  7.             {
  8.                 OK = true; //預設是true
  9.                 x = rnd.Next(n);//亂數給值從0到n-1
  10.                 for(int k=0;k<i;k++)//再從頭檢查新出的是否和已有的數字一樣
  11.                     if(A[k]==x)//如果檢查到已有該數字,則i退回一個,OK變成false,並中斷此迴圈
  12.                     {
  13.                         i--;
  14.                         OK = false;
  15.                         break;
  16.                     }
  17.                 if (OK) A[i] = x;//如果以上檢查完成並未被中斷,且OK依然為true,表示x的數字從未出現過,所以可以加到陣列中
  18.                
  19.             }

  20.         }
複製代碼
回復 支持 反對

使用道具 舉報

板凳
 樓主| 發表於 2021-1-30 12:15:20 | 只看該作者
本帖最後由 2266 於 2021-6-27 18:13 編輯

最簡單!

  1. void randomPickNoRepeat(int[] a)
  2.         {
  3.             Random rnd = new Random();
  4.             for(int i=0;i<a.Length;i++)
  5.             {
  6.                 a[i] = rnd.Next(a.Length);
  7.                 //Console.WriteLine("a[" + i + "] is " + a[i]);
  8.                 for(int j=0;j<i;j++)
  9.                     if(a[j]==a[i]) //如果陣列中已有數字和目前選出來的數字一樣的話
  10.                     {
  11.                         //Console.WriteLine("Because a[" + i + "] is as the same as a[" + j + "] so i is going back to " + i + " and will ++ again.");
  12.                         i--; // i 減去一個
  13.                         break; // 立即中斷回到上面再次迴圈
  14.                     }
  15.             }
  16.         }
複製代碼
回復 支持 反對

使用道具 舉報

地板
 樓主| 發表於 2021-6-27 17:31:20 | 只看該作者
用亂數打亂原有陣列之元素排序:


  1. using System.Linq;

  2. Random rnd = new Random;
  3. int[] n = new int[10]{0,1,2,3,4,5,6,7,8,9};//要先有元素
  4. n = n.OrderBy(x=>rnd.Next()).ToArray();//x可以隨意取,例如k,z什麼都行,不必先宣告。
  5. 此時n的陣列排序已亂排了。
複製代碼
回復 支持 反對

使用道具 舉報

5#
發表於 2021-6-30 09:56:07 | 只看該作者
本帖最後由 csharp 於 2021-6-30 13:35 編輯

nothing
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Media;

  11. namespace WindowsFormsApp28
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         Panel panel = new Panel();
  16.         PictureBox[] pb = new PictureBox[12];
  17.         PictureBox[] pb1 = new PictureBox[12];
  18.         PictureBox[] pb2 = new PictureBox[12];
  19.         PictureBox[] pb3 = new PictureBox[12];
  20.         PictureBox[] pb4 = new PictureBox[12];
  21.         Label[] table = new Label[12];
  22.         PictureBox[] pbIcon = new PictureBox[4];
  23.         SoundPlayer touch = new SoundPlayer(Properties.Resources.quick000);
  24.         SoundPlayer ending = new SoundPlayer(Properties.Resources.ending001);
  25.         int oldX, oldY;
  26.         int[] ary = new int[12];
  27.         bool isDown;
  28.         bool[] matched = new bool[12];
  29.         PictureBox howto = new PictureBox();
  30.         
  31.         public Form1()
  32.         {
  33.             InitializeComponent();
  34.             
  35.             setPanel();
  36.             setIcon();
  37.             setTable();
  38.             setPicturebox();
  39.             howto.Image = Properties.Resources.howtoplay;
  40.             howto.Location = new System.Drawing.Point(Width / 3 * 2, 15);
  41.             howto.Size = new Size(120,20);
  42.             panel.Controls.Add(howto);
  43.             
  44.         }

  45.         


  46.         void setPanel()
  47.         {
  48.             Rectangle screen = Screen.FromControl(this).Bounds;//這三行是求得整個電腦視窗的長寬值
  49.             Width = screen.Width;//這三行是求得整個電腦視窗的長寬值
  50.             Height = screen.Height;//這三行是求得整個電腦視窗的長寬值
  51.             
  52.             panel = new Panel();
  53.             panel.Location = new System.Drawing.Point(35, 5);
  54.             panel.Size = new Size(Width / 10 * 9, Height / 100 * 95);
  55.             //panel.BackgroundImage = Properties.Resources._111;
  56.             //panel.BackgroundImageLayout = ImageLayout.Stretch;
  57.             panel.BackColor = Color.AliceBlue;
  58.             Controls.Add(panel);
  59.         }


  60.         void setPicturebox()
  61.         {
  62.             for(int i = 0; i < 12; i++)
  63.             {
  64.                 //先將物件陣列實體化
  65.                 pb[i] = new PictureBox();
  66.                 pb1[i] = new PictureBox();
  67.                 pb2[i] = new PictureBox();
  68.                 pb3[i] = new PictureBox();
  69.                 pb4[i] = new PictureBox();

  70.                 //將各圖檔尺寸定出來
  71.                 pb[i].Size = new Size(100, 100);
  72.                 pb1[i].Size = new Size(100, 100);
  73.                 pb2[i].Size = new Size(100, 100);
  74.                 pb3[i].Size = new Size(100, 100);
  75.                 pb4[i].Size = new Size(100, 100);

  76.                 //除掉主要的陣列之外,將其它陣列給予圖片
  77.                 pb1[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("a"+i);
  78.                
  79.                 pb2[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("b"+i);
  80.                
  81.                 pb3[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("d"+i);
  82.                
  83.                 pb4[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("k"+i);
  84.                
  85.                 //將主要陣列賦予位置,並且將其置入panel中
  86.                 pb[i].Location = new System.Drawing.Point(30+i%6*110,325+i/6*103);
  87.                 pb[i].BackColor = Color.Transparent;
  88.                 panel.Controls.Add(pb[i]);
  89.                
  90.                 pb[i].MouseDown += new MouseEventHandler(pb_mousedown);
  91.                 pb[i].MouseMove += new MouseEventHandler(pb_mousemove);
  92.                 pb[i].MouseUp += new MouseEventHandler(pb_mouseup);

  93.             }
  94.         }

  95.         
  96.         void pb_mousedown(object sender, MouseEventArgs e)
  97.         {
  98.             isDown = true;
  99.             oldX = e.X;
  100.             oldY = e.Y;
  101.         }

  102.         void pb_mousemove(object sender, MouseEventArgs e)
  103.         {
  104.             PictureBox pb = (PictureBox)sender;
  105.             if (isDown)
  106.             {
  107.                 pb.Left += e.X - oldX;
  108.                 pb.Top += e.Y - oldY;
  109.                 pb.BringToFront();
  110.             }
  111.         }

  112.         void pb_mouseup(object sender, MouseEventArgs e)
  113.         {
  114.             isDown = false;

  115.             closeToBorder(sender, table);
  116.                        
  117.             isThisPictureCorrect(pb, table);
  118.                        
  119.             if (allMatched())
  120.             {

  121.                 for (int i = 0; i < 12; i++)
  122.                 {
  123.                     pb[i].Enabled = false;
  124.                      }
  125.                 ending.Play();
  126.             }
  127.         }


  128.         void setIcon()
  129.         {
  130.             for(int i = 0; i < 4; i++)
  131.             {
  132.                 pbIcon[i] = new PictureBox();
  133.                 pbIcon[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("icon" + (i + 1));
  134.                 pbIcon[i].Size = new Size(120, 90);
  135.                 pbIcon[i].SizeMode = PictureBoxSizeMode.StretchImage;
  136.                 pbIcon[i].Location = new System.Drawing.Point(Width/3*2,20+i*110+30);
  137.                 panel.Controls.Add(pbIcon[i]);
  138.                
  139.                 pbIcon[i].Click += new System.EventHandler(pb_click);
  140.             }
  141.         }

  142.         
  143.                 void pb_click(object sender, EventArgs e)
  144.                 {
  145.                     PictureBox p = (PictureBox)sender;
  146.                     pbRestart();
  147.                     if (p == pbIcon[0])
  148.                         randomPb(pb, pb1);
  149.                     else if (p == pbIcon[1])
  150.                         randomPb(pb, pb2);
  151.                     else if (p == pbIcon[2])
  152.                         randomPb(pb, pb3);
  153.                     else if (p == pbIcon[3])
  154.                         randomPb(pb, pb4);
  155.                 }
  156.         

  157.         void setTable()
  158.         {
  159.             for (int i = 11; i >= 0; i--)
  160.             {
  161.                 table[i] = new Label();
  162.                 table[i].Size = new Size(100, 100);
  163.                 table[i].BorderStyle = BorderStyle.FixedSingle;
  164.                 table[i].BackColor = Color.Transparent;
  165.                 table[i].Location = new System.Drawing.Point(150+(11 - i) % 4 * 100,10+ (11 - i) / 4 * 100);
  166.                 panel.Controls.Add(table[i]);
  167.                
  168.             }
  169.         }


  170.         void closeToBorder(object sender, Label[] lbl)
  171.         {
  172.             PictureBox p = (PictureBox)sender;
  173.             for (int i = 0; i < 12; i++)
  174.             {
  175.                 if (Math.Abs(p.Left - lbl[i].Left) < 30 && Math.Abs(p.Top - lbl[i].Top) < 30)
  176.                 {
  177.                     p.Left = lbl[i].Left;
  178.                     p.Top = lbl[i].Top;
  179.                     touch.Play();
  180.                     
  181.                 }
  182.             }
  183.         }
  184.                
  185.                
  186.         void isThisPictureCorrect(PictureBox[] p, Label[] lbl)
  187.         {
  188.             for (int i = 0; i < 12; i++)
  189.                          for(int j=0;j<12;j++)
  190.             {
  191.                 if ((p[i].Top == lbl[j].Top) && (p[i].Left == lbl[j].Left)&&(pbValue[i]==j))
  192.                     matched[i] = true;
  193.                 else matched[i] = false;
  194.             }
  195.         }
  196.                
  197.         bool allMatched()
  198.         {        bool ok=true;
  199.             for (int i = 0; i < 12; i++)
  200.             {
  201.                 if (matched[i] == false) ok== false;
  202.             }
  203.             return ok;
  204.         }

  205.         
  206.         void randomPickNoRepeat(int[] a)
  207.         {
  208.             Random rnd = new Random();
  209.             for (int i = 0; i < a.Length; i++)
  210.             {
  211.                 a[i] = rnd.Next(a.Length);
  212.                
  213.                 for (int j = 0; j < i; j++)
  214.                     if (a[j] == a[i])
  215.                     {
  216.                         
  217.                         i--;
  218.                         break;
  219.                     }
  220.             }

  221.         }

  222.         void pbRestart()
  223.         {
  224.             for (int i = 0; i < 12; i++)
  225.             {
  226.                 pb[i].Enabled = true;
  227.                 pb[i].Location = new System.Drawing.Point(30 + i % 6 * 110, 330 + i / 6 * 103);
  228.             }
  229.         }
  230.                 int[] a = new int[12] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  231.         int[] pbValue = new int[12];
  232.                 void randomPb(PictureBox[] pb, PictureBox[] p1)
  233.         {
  234.             
  235.             randomPickNoRepeat(a);
  236.                        
  237.             for (int i = 0; i < 12; i++)
  238.             {
  239.                 pb[i].Image = p1[a[i]].Image;
  240.                                 pbValue[i]=a[i];
  241.                                
  242.                
  243.             }
  244.         }


  245.     }
  246. }
複製代碼
回復 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|手機版|Archiver|code.club  

GMT+8, 2024-4-25 06:02 , Processed in 0.082001 second(s), 16 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表