接上一篇:

C#使用随机数模拟器来模拟世界杯排名(一)

C#使用随机数模拟器来模拟世界杯排名(一)_斯内科的博客-CSDN博客

我们使用洗牌随机数算法来匹配世界杯对战国家:

新建洗牌随机数相关类RandomUtil

用于随机世界杯参赛国家的列表索引并分配:

洗牌算法的时间复杂度是 O(N)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace WorldCupRankingDemo
{public class RandomUtil {private static int[] nums;private static Random rand = new Random();/// <summary>/// 初始化数组索引/// </summary>/// <param name="indexArray"></param>public static void Init(int[] indexArray){nums = indexArray;}/// <summary>/// 洗牌算法,洗牌算法的时间复杂度是 O(N)/// 第一次有N种可能,第二次有N-1种可能,第三次有N-2种可能/// ...最后一次只有一种可能/// </summary>/// <returns></returns>public static int[] Shuffle(){int n = nums.Length;int[] copy = new int[n];Array.Copy(nums, copy, n);for (int i = 0; i < n; i++){// 生成一个 [i, n-1] 区间内的随机数int r = i + rand.Next(n - i);// 交换 nums[i] 和 nums[r],即获取第r个数【放到第i个位置】Swap(copy, i, r);}return copy;}/// <summary>/// 交换数组两个数的值/// </summary>/// <param name="nums"></param>/// <param name="i"></param>/// <param name="j"></param>private static void Swap(int[] nums, int i, int j){int temp = nums[i];nums[i] = nums[j];nums[j] = temp;}}
}

新建自定义控件UserControl:世界杯参赛国家对象控件UcCountry

控件UcCountry由一个panel(width:300,height:180)组成

panel由两个Label【国家,胜率】和一个PictureBox【设置:BackgroundImageLayout为Zoom】

自定义控件的设计器代码如下:

namespace WorldCupRankingDemo
{partial class UcCountry{/// <summary> /// 必需的设计器变量。/// </summary>private System.ComponentModel.IContainer components = null;/// <summary> /// 清理所有正在使用的资源。/// </summary>/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region 组件设计器生成的代码/// <summary> /// 设计器支持所需的方法 - 不要修改/// 使用代码编辑器修改此方法的内容。/// </summary>private void InitializeComponent(){this.panel2 = new System.Windows.Forms.Panel();this.lblWinningRatio = new System.Windows.Forms.Label();this.lblCountryName = new System.Windows.Forms.Label();this.picNationalFlag = new System.Windows.Forms.PictureBox();this.panel2.SuspendLayout();((System.ComponentModel.ISupportInitialize)(this.picNationalFlag)).BeginInit();this.SuspendLayout();// // panel2// this.panel2.Controls.Add(this.lblWinningRatio);this.panel2.Controls.Add(this.lblCountryName);this.panel2.Controls.Add(this.picNationalFlag);this.panel2.Location = new System.Drawing.Point(3, 3);this.panel2.Name = "panel2";this.panel2.Size = new System.Drawing.Size(300, 180);this.panel2.TabIndex = 2;// // lblWinningRatio// this.lblWinningRatio.AutoSize = true;this.lblWinningRatio.ForeColor = System.Drawing.Color.Green;this.lblWinningRatio.Location = new System.Drawing.Point(181, 7);this.lblWinningRatio.Name = "lblWinningRatio";this.lblWinningRatio.Size = new System.Drawing.Size(32, 17);this.lblWinningRatio.TabIndex = 5;this.lblWinningRatio.Text = "胜率";// // lblCountryName// this.lblCountryName.AutoSize = true;this.lblCountryName.ForeColor = System.Drawing.Color.Blue;this.lblCountryName.Location = new System.Drawing.Point(25, 7);this.lblCountryName.Name = "lblCountryName";this.lblCountryName.Size = new System.Drawing.Size(32, 17);this.lblCountryName.TabIndex = 4;this.lblCountryName.Text = "国家";// // picNationalFlag// this.picNationalFlag.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;this.picNationalFlag.Location = new System.Drawing.Point(25, 30);this.picNationalFlag.Name = "picNationalFlag";this.picNationalFlag.Size = new System.Drawing.Size(250, 150);this.picNationalFlag.TabIndex = 3;this.picNationalFlag.TabStop = false;// // UcCountry// this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.Controls.Add(this.panel2);this.Name = "UcCountry";this.Size = new System.Drawing.Size(306, 186);this.panel2.ResumeLayout(false);this.panel2.PerformLayout();((System.ComponentModel.ISupportInitialize)(this.picNationalFlag)).EndInit();this.ResumeLayout(false);}#endregionprivate Panel panel2;public PictureBox picNationalFlag;public Label lblWinningRatio;public Label lblCountryName;}
}

切换到窗体FormWorldCupRanking,在窗体的Load事件中随机分配世界杯国家比赛

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;namespace WorldCupRankingDemo
{public partial class FormWorldCupRanking : Form{public FormWorldCupRanking(){InitializeComponent();}private void FormWorldCupRanking_Load(object sender, EventArgs e){CountryUtil.InitCountry();int[] indexArray = new int[CountryUtil.ListWorldCup.Count];for (int i = 0; i < indexArray.Length; i++) {indexArray[i] = i;}RandomUtil.Init(indexArray);int[] destArray = RandomUtil.Shuffle();int rowCount = (destArray.Length + 1) / 2;for (int i = 0; i < rowCount; i++){UcCountry ucCountry1 = new UcCountry();ucCountry1.Name = $"ucCountry{i * 2 + 1}";Country country1 = CountryUtil.ListWorldCup[destArray[i * 2]];ucCountry1.lblCountryName.Text = country1.CountryName;ucCountry1.lblWinningRatio.Text = $"胜率:{country1.WinningRatio}";ucCountry1.picNationalFlag.BackgroundImage = country1.NationalFlag;ucCountry1.Tag = country1;ucCountry1.Location = new Point(5, i * 200 + 5);this.Controls.Add(ucCountry1);Button button = new Button();button.Name = $"button{i + 1}";button.Text = "VS";button.Font = new Font("宋体", 30, FontStyle.Bold);button.Size = new Size(100, 100);button.Location = new Point(330, i * 200 + 60);this.Controls.Add(button);if (i * 2 + 1 < destArray.Length) {//对奇数个世界杯比赛国家特殊处理,最后一个国家直接胜利UcCountry ucCountry2 = new UcCountry();ucCountry2.Name = $"ucCountry{i * 2 + 2}";Country country2 = CountryUtil.ListWorldCup[destArray[i * 2 + 1]];ucCountry2.lblCountryName.Text = country2.CountryName;ucCountry2.lblWinningRatio.Text = $"胜率:{country2.WinningRatio}";ucCountry2.picNationalFlag.BackgroundImage = country2.NationalFlag;ucCountry2.Tag = country2;ucCountry2.Location = new Point(455, i * 200 + 5);this.Controls.Add(ucCountry2);}}}}
}

窗体运行如下:

C#使用随机数模拟器来模拟世界杯排名(二)相关推荐

  1. C#使用随机数模拟器来模拟世界杯排名(三)

    接上篇 C#使用随机数模拟器来模拟世界杯排名(二)_斯内科的博客-CSDN博客 上一篇我们使用随机数匹配比赛的世界杯国家, 这一篇我们使用随机数以及胜率模拟器 决赛出 世界杯冠军.亚军. 我们在主界面 ...

  2. C#使用随机数模拟器来模拟世界杯排名(一)

    用技术记录世界杯 CSDN 2022卡塔尔世界杯已经决出八强,卡塔尔世界杯继续进行,随着1/8决赛战罢,本届世界杯8强球队全部产生,分别是荷兰.阿根廷.巴西.克罗地亚.英格兰.法国.葡萄牙和摩洛哥. ...

  3. python依照概率抽样_R语言之随机数与抽样模拟篇

    R语言生成均匀分布随机数的函数是runif() 句法是:runif(n,min=0,max=1)  n表示生成的随机数数量,min表示均匀分布的下限,max表示均匀分布的上限:若省略参数min.max ...

  4. R语言之随机数与抽样模拟篇

    转载自:http://blog.csdn.net/lilanfeng1991/article/details/18505723 3.1 随机数的产生 3.1.1 均匀分布随机数 R语言生成均匀分布随机 ...

  5. 【Java】设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声。

    题目要求:设计一个动物声音"模拟器",希望模拟器可以模拟许多动物的叫声.要求如下: (1)编写抽象类Animal Animal抽象类有2个抽象方法cry()和getAnimaNam ...

  6. 8in1模拟器v2模拟飞行_重新想象飞行模拟器:过去和现在

    8in1模拟器v2模拟飞行 本文是Microsoft的Web开发系列的一部分. 感谢您支持使SitePoint成为可能的合作伙伴. Apple于1980年推出了Flight Simulator的第一个 ...

  7. 设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声。要求如下:

    设计一个动物声音"模拟器",希望模拟器可以模拟许多动物的叫声.要求如下: (1)编写抽象类Animal Animal抽象类有2个抽象方法cry()和getAnimaName(),即 ...

  8. 如何在android模拟器中模拟sd卡,如何在Android模拟器中模拟SD卡

    如何在Android模拟器中模拟SD卡 简介 Android允许开发者创建一个SD卡镜像并在启动模拟器加载它, 用于模拟物理设备中的SD卡. 下面将介绍: 1. 如何创建一个SD卡镜像? 2. 如何拷 ...

  9. Java练习 | 设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声(附代码)

    问题:设计一个动物声音"模拟器",希望模拟器可以模拟许多动物的叫声. 要求如下: 编写抽象类Animal Animal抽象类有2个抽象方法cry()和getAnimaName(), ...

最新文章

  1. 折返(Reentrancy)VS线程安全(Thread safety)
  2. MTK方案下tee.img的打包方式的详细拆解
  3. 转 一个SMD 0805的LED的电流,电压,亮度关系表
  4. python论坛签到_论坛自动签到教程
  5. 小程序richtext_用于基于SWT的应用程序的RichText编辑器组件
  6. Virtools脚本语言(VSL)教程 - 枚举
  7. iphone 方法总结
  8. [Unity] 战斗系统学习 8:构建 TPS 框架 3:mono 组件
  9. 云计算学习路线图课件:云计算和虚拟机有什么区别?
  10. 猜数游戏--MOOC中习题
  11. mfc中句柄与指针的区别
  12. 在windows下如何批量转换pvr,ccz为png或jpg
  13. 用Python写个魂斗罗,另附30个Python小游戏源码
  14. USB-HID鼠标键盘驱动
  15. RSA算法详解与举例
  16. python量化策略——改进的美林时钟代码(代码版)
  17. 最佳实践 | 如何提高落地页的转化率?这里有4个策略
  18. 使用飞信机器人发短信需要开放的端口
  19. Live Messenger ,Gmail ,Orkut ,Wallop
  20. matlab空间重采样,matlab重采样函数

热门文章

  1. 无线路由的beacon interval
  2. Java池化技术你了解多少?
  3. CyclicBarrier让多线程齐步走
  4. 金融IC卡全“芯”起航
  5. android 序列化存储对象,android中对象序列化存储
  6. 远程教学,微信群里就能上课、培训
  7. 拥有ISO26262认证的软件工具清单
  8. power_supply子系统
  9. No power supply specified for netVCC in Power Rail Confiquration.
  10. python怎么编写对称图案_如何使用opencvpython识别图像的形状是对称的还是不对称的?...