Guest User

Untitled

a guest
Oct 22nd, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5.  
  6. namespace 選數
  7. {
  8. public partial class Form1 : Form
  9. {
  10. private Dictionary<int, int> numberFrequency;
  11.  
  12. public Form1()
  13. {
  14. InitializeComponent();
  15. numberFrequency = new Dictionary<int, int>();
  16. textBox1.Text = "1"; // 預設最小值為1
  17. }
  18.  
  19. private void button1_Click(object sender, EventArgs e)
  20. {
  21. // 檢查範圍輸入
  22. if (!int.TryParse(textBox1.Text, out int minValue))
  23. {
  24. MessageBox.Show("請輸入有效的最小值");
  25. return;
  26. }
  27.  
  28. if (!int.TryParse(textBox2.Text, out int maxValue) || maxValue <= minValue)
  29. {
  30. MessageBox.Show("請輸入有效的最大值(必須大於最小值)");
  31. return;
  32. }
  33.  
  34. // 檢查要挑幾個數字的輸入
  35. if (!int.TryParse(textBox5.Text, out int count) || count <= 0)
  36. {
  37. MessageBox.Show("請輸入有效的挑選數量(必須為正整數)");
  38. return;
  39. }
  40.  
  41. // 計算範圍內的總數字量
  42. int rangeSize = maxValue - minValue + 1;
  43.  
  44. // 檢查範圍內是否有足夠的數字可以選
  45. if (count > rangeSize)
  46. {
  47. MessageBox.Show("範圍內的數字數量不足以選出指定數量的隨機數字,請調整範圍或減少挑選數量。");
  48. return;
  49. }
  50.  
  51. // 取得要剃除的數字陣列
  52. var excludedNumbers = GetExcludedNumbers(textBox3.Text);
  53.  
  54. // 產生符合條件的隨機數字
  55. int[] randomArray = GenerateRandomNumbers(excludedNumbers, minValue, maxValue, count);
  56.  
  57. // 將結果排序
  58. Array.Sort(randomArray);
  59.  
  60. // 將結果轉換為字串格式
  61. string result = string.Join("\t", randomArray);
  62.  
  63. // 如果 TextBox4 中已有內容,先空一行再顯示新結果
  64. if (!string.IsNullOrEmpty(textBox4.Text))
  65. {
  66. textBox4.AppendText(Environment.NewLine);
  67. }
  68.  
  69. // 顯示結果
  70. textBox4.AppendText(result);
  71.  
  72. // 更新數字出現次數
  73. UpdateFrequency(randomArray);
  74.  
  75. // 顯示更新後的頻率
  76. DisplayFrequencies();
  77. }
  78.  
  79. private void button2_Click(object sender, EventArgs e)
  80. {
  81. // 清空 TextBox4 和 ListBox1 的內容
  82. textBox4.Clear();
  83. listBox1.Items.Clear();
  84. numberFrequency.Clear(); // 清空數字頻率的記錄
  85. }
  86.  
  87.  
  88. private void UpdateFrequency(int[] numbers)
  89. {
  90. foreach (var number in numbers)
  91. {
  92. if (numberFrequency.ContainsKey(number))
  93. {
  94. numberFrequency[number]++;
  95. }
  96. else
  97. {
  98. numberFrequency[number] = 1;
  99. }
  100. }
  101. }
  102.  
  103. private void DisplayFrequencies()
  104. {
  105. // 清空之前的結果
  106. listBox1.Items.Clear();
  107.  
  108. // 依次數從高到低排序並顯示結果
  109. foreach (var kvp in numberFrequency.OrderByDescending(kvp => kvp.Value).ThenBy(kvp => kvp.Key))
  110. {
  111. listBox1.Items.Add($"Number {kvp.Key}: {kvp.Value} times");
  112. }
  113. }
  114.  
  115. private int[] GetExcludedNumbers(string input)
  116. {
  117. // 以空白、逗號、頓號區隔輸入的數字,並轉換成整數陣列
  118. var numbers = input.Split(new char[] { ' ', ',', '、' }, StringSplitOptions.RemoveEmptyEntries)
  119. .Select(s => int.Parse(s))
  120. .ToArray();
  121. return numbers;
  122. }
  123.  
  124. private int[] GenerateRandomNumbers(int[] excludedNumbers, int minValue, int maxValue, int count)
  125. {
  126. Random rnd = new Random();
  127. List<int> selectedNumbers = new List<int>();
  128.  
  129. // 產生亂數直到選擇到 count 個不在被剃除清單中的數字,且在範圍內
  130. while (selectedNumbers.Count < count)
  131. {
  132. int randomNumber = rnd.Next(minValue, maxValue + 1); // 使用範圍內的亂數
  133. if (!excludedNumbers.Contains(randomNumber) && !selectedNumbers.Contains(randomNumber))
  134. {
  135. selectedNumbers.Add(randomNumber);
  136. }
  137. }
  138.  
  139. return selectedNumbers.ToArray();
  140. }
  141. }
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment