code.club
標題:
用亂數選出不重複的數字陣列
[打印本頁]
作者:
2266
時間:
2021-1-23 10:43
標題:
用亂數選出不重複的數字陣列
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]);
}
}
作者:
2266
時間:
2021-1-26 18:15
本帖最後由 2266 於 2021-6-27 18:12 編輯
好像這是更簡單的寫法
static void randomPickNoRepeat(int[] A, int n)
{
Random rnd = new Random();
int x = 0;
bool OK = true; //OK 做為檢查該數字是否可以放入陣列的旗標
for (int i = 0; i < n; i++)
{
OK = true; //預設是true
x = rnd.Next(n);//亂數給值從0到n-1
for(int k=0;k<i;k++)//再從頭檢查新出的是否和已有的數字一樣
if(A[k]==x)//如果檢查到已有該數字,則i退回一個,OK變成false,並中斷此迴圈
{
i--;
OK = false;
break;
}
if (OK) A[i] = x;//如果以上檢查完成並未被中斷,且OK依然為true,表示x的數字從未出現過,所以可以加到陣列中
}
}
複製代碼
作者:
2266
時間:
2021-1-30 12:15
本帖最後由 2266 於 2021-6-27 18:13 編輯
最簡單!
void randomPickNoRepeat(int[] a)
{
Random rnd = new Random();
for(int i=0;i<a.Length;i++)
{
a[i] = rnd.Next(a.Length);
//Console.WriteLine("a[" + i + "] is " + a[i]);
for(int j=0;j<i;j++)
if(a[j]==a[i]) //如果陣列中已有數字和目前選出來的數字一樣的話
{
//Console.WriteLine("Because a[" + i + "] is as the same as a[" + j + "] so i is going back to " + i + " and will ++ again.");
i--; // i 減去一個
break; // 立即中斷回到上面再次迴圈
}
}
}
複製代碼
作者:
2266
時間:
2021-6-27 17:31
用亂數打亂原有陣列之元素排序:
要
using System.Linq;
Random rnd = new Random;
int[] n = new int[10]{0,1,2,3,4,5,6,7,8,9};//要先有元素
n = n.OrderBy(x=>rnd.Next()).ToArray();//x可以隨意取,例如k,z什麼都行,不必先宣告。
此時n的陣列排序已亂排了。
複製代碼
作者:
csharp
時間:
2021-6-30 09:56
本帖最後由 csharp 於 2021-6-30 13:35 編輯
nothing
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
namespace WindowsFormsApp28
{
public partial class Form1 : Form
{
Panel panel = new Panel();
PictureBox[] pb = new PictureBox[12];
PictureBox[] pb1 = new PictureBox[12];
PictureBox[] pb2 = new PictureBox[12];
PictureBox[] pb3 = new PictureBox[12];
PictureBox[] pb4 = new PictureBox[12];
Label[] table = new Label[12];
PictureBox[] pbIcon = new PictureBox[4];
SoundPlayer touch = new SoundPlayer(Properties.Resources.quick000);
SoundPlayer ending = new SoundPlayer(Properties.Resources.ending001);
int oldX, oldY;
int[] ary = new int[12];
bool isDown;
bool[] matched = new bool[12];
PictureBox howto = new PictureBox();
public Form1()
{
InitializeComponent();
setPanel();
setIcon();
setTable();
setPicturebox();
howto.Image = Properties.Resources.howtoplay;
howto.Location = new System.Drawing.Point(Width / 3 * 2, 15);
howto.Size = new Size(120,20);
panel.Controls.Add(howto);
}
void setPanel()
{
Rectangle screen = Screen.FromControl(this).Bounds;//這三行是求得整個電腦視窗的長寬值
Width = screen.Width;//這三行是求得整個電腦視窗的長寬值
Height = screen.Height;//這三行是求得整個電腦視窗的長寬值
panel = new Panel();
panel.Location = new System.Drawing.Point(35, 5);
panel.Size = new Size(Width / 10 * 9, Height / 100 * 95);
//panel.BackgroundImage = Properties.Resources._111;
//panel.BackgroundImageLayout = ImageLayout.Stretch;
panel.BackColor = Color.AliceBlue;
Controls.Add(panel);
}
void setPicturebox()
{
for(int i = 0; i < 12; i++)
{
//先將物件陣列實體化
pb[i] = new PictureBox();
pb1[i] = new PictureBox();
pb2[i] = new PictureBox();
pb3[i] = new PictureBox();
pb4[i] = new PictureBox();
//將各圖檔尺寸定出來
pb[i].Size = new Size(100, 100);
pb1[i].Size = new Size(100, 100);
pb2[i].Size = new Size(100, 100);
pb3[i].Size = new Size(100, 100);
pb4[i].Size = new Size(100, 100);
//除掉主要的陣列之外,將其它陣列給予圖片
pb1[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("a"+i);
pb2[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("b"+i);
pb3[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("d"+i);
pb4[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("k"+i);
//將主要陣列賦予位置,並且將其置入panel中
pb[i].Location = new System.Drawing.Point(30+i%6*110,325+i/6*103);
pb[i].BackColor = Color.Transparent;
panel.Controls.Add(pb[i]);
pb[i].MouseDown += new MouseEventHandler(pb_mousedown);
pb[i].MouseMove += new MouseEventHandler(pb_mousemove);
pb[i].MouseUp += new MouseEventHandler(pb_mouseup);
}
}
void pb_mousedown(object sender, MouseEventArgs e)
{
isDown = true;
oldX = e.X;
oldY = e.Y;
}
void pb_mousemove(object sender, MouseEventArgs e)
{
PictureBox pb = (PictureBox)sender;
if (isDown)
{
pb.Left += e.X - oldX;
pb.Top += e.Y - oldY;
pb.BringToFront();
}
}
void pb_mouseup(object sender, MouseEventArgs e)
{
isDown = false;
closeToBorder(sender, table);
isThisPictureCorrect(pb, table);
if (allMatched())
{
for (int i = 0; i < 12; i++)
{
pb[i].Enabled = false;
}
ending.Play();
}
}
void setIcon()
{
for(int i = 0; i < 4; i++)
{
pbIcon[i] = new PictureBox();
pbIcon[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("icon" + (i + 1));
pbIcon[i].Size = new Size(120, 90);
pbIcon[i].SizeMode = PictureBoxSizeMode.StretchImage;
pbIcon[i].Location = new System.Drawing.Point(Width/3*2,20+i*110+30);
panel.Controls.Add(pbIcon[i]);
pbIcon[i].Click += new System.EventHandler(pb_click);
}
}
void pb_click(object sender, EventArgs e)
{
PictureBox p = (PictureBox)sender;
pbRestart();
if (p == pbIcon[0])
randomPb(pb, pb1);
else if (p == pbIcon[1])
randomPb(pb, pb2);
else if (p == pbIcon[2])
randomPb(pb, pb3);
else if (p == pbIcon[3])
randomPb(pb, pb4);
}
void setTable()
{
for (int i = 11; i >= 0; i--)
{
table[i] = new Label();
table[i].Size = new Size(100, 100);
table[i].BorderStyle = BorderStyle.FixedSingle;
table[i].BackColor = Color.Transparent;
table[i].Location = new System.Drawing.Point(150+(11 - i) % 4 * 100,10+ (11 - i) / 4 * 100);
panel.Controls.Add(table[i]);
}
}
void closeToBorder(object sender, Label[] lbl)
{
PictureBox p = (PictureBox)sender;
for (int i = 0; i < 12; i++)
{
if (Math.Abs(p.Left - lbl[i].Left) < 30 && Math.Abs(p.Top - lbl[i].Top) < 30)
{
p.Left = lbl[i].Left;
p.Top = lbl[i].Top;
touch.Play();
}
}
}
void isThisPictureCorrect(PictureBox[] p, Label[] lbl)
{
for (int i = 0; i < 12; i++)
for(int j=0;j<12;j++)
{
if ((p[i].Top == lbl[j].Top) && (p[i].Left == lbl[j].Left)&&(pbValue[i]==j))
matched[i] = true;
else matched[i] = false;
}
}
bool allMatched()
{ bool ok=true;
for (int i = 0; i < 12; i++)
{
if (matched[i] == false) ok== false;
}
return ok;
}
void randomPickNoRepeat(int[] a)
{
Random rnd = new Random();
for (int i = 0; i < a.Length; i++)
{
a[i] = rnd.Next(a.Length);
for (int j = 0; j < i; j++)
if (a[j] == a[i])
{
i--;
break;
}
}
}
void pbRestart()
{
for (int i = 0; i < 12; i++)
{
pb[i].Enabled = true;
pb[i].Location = new System.Drawing.Point(30 + i % 6 * 110, 330 + i / 6 * 103);
}
}
int[] a = new int[12] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
int[] pbValue = new int[12];
void randomPb(PictureBox[] pb, PictureBox[] p1)
{
randomPickNoRepeat(a);
for (int i = 0; i < 12; i++)
{
pb[i].Image = p1[a[i]].Image;
pbValue[i]=a[i];
}
}
}
}
複製代碼
歡迎光臨 code.club (https://code.club/)
Powered by Discuz! X3.2