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

2023-05-16

接上一篇:

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);

        }

        #endregion

        private 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);
                }
            }
            
        }
    }
}

窗体运行如下:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

  • vb.net中使用GetPrivateProfileString访问INI文件,解决中文路径问题

    在vb net2005 43 winxp中 xff0c 我使用GetPrivateProfileString读取一个ini文件 xff0c 如果文件路径中含有中文 xff0c 就会遇到一个奇怪的问题 xff1a 第一次读取正常 xff0c
  • CSDN史上最大的非法集资案

    短短 12 小时内发生了什么 xff1f 263231 分 xff0c 417 人次 xff0c 押宝游戏 疯狂大倒分 成为了 CSDN 历史上最大的非法集资 非法集资怎么操作的 xff1f 谁是幕后黑手 xff1f 集资的目的是什么 xf
  • 小帅和七个男友 ---第二章 一株含羞草

    第二章 一株含羞草 升入初三 xff0c 中考对我们这所国家重点中学来说 xff0c 只相当于一场普通的模拟考试 学习并不紧张 xff0c 我们还是该玩的玩 xff0c 该吃的吃 转瞬间就远去的椅子 xff0c 激起了我少女的情怀 我越来越
  • 20号你会黑屏吗?来验证一下你的正版XP和Office

    相当一部分企业都担心自己的电脑到时候会黑屏 这些企业并不是拿不出那几千块买套正版 xff0c 是没有习惯 其他社区也就罢了 xff0c 作为csdn xff0c 以软件开发人员集中的社区 xff0c 对使用正版软件这么抵触 xff0c 是不
  • 搜狗输入法 VS 拼音加加

    用了16年计算机 xff0c 一共用过四个输入法 xff1a 五笔 xff08 牌子忘记了 xff09 xff0c 智能ABC xff0c 拼音加加 xff0c 搜狗 放弃五笔的原因很简单 xff1a 我要学拼音 xff01 一说到高考 x
  • 苹果应用商店支持人民币信用卡(已验证,有图有真相)

    今日打开i4上的app store xff0c 惊奇发现货币单位已从 变为 xffe5 惊讶万分 xff0c 于是测试一下 xff0c 随机找了一个25元的航班追踪应用 屏幕弹出登录界面后 xff0c 输入appleid xff0c 随后出
  • 如何正确的报修

    先讲个故事 xff0c 今天接到一宗报修 xff0c 主题是 数据库连不上了 xff0c 我的天 xff0c 大事件啊 xff0c 习惯性地立即测试一下 xff0c 发现一切正常 随后开始追问谁说的 数据库连接不上 xff0c 原来是一个女
  • HTML5移动应用抓包

    有时候看到某个移动应用很不错 xff0c 想研究一下其HTML5源码 xff0c 样式表 xff0c 脚本什么的 xff0c 正常方法是不好得到的 通常可以用Safari伪造User Agent来欺骗目标网站 xff0c 让网站认为你是一个
  • python求无向图的连通子图

    参考链接 xff1a https blog csdn net u010330109 article details 89525729 使用networkx包求图的所有连通子图 span class token keyword import
  • 学习libpcap库,写例子代码--port_protocol.hpp

    ifndef PORT PROTOCOL define PORT PROTOCOL include lt string gt include lt netdb h gt for getservbyport include 34 file h
  • ubuntu18.04系统用户登录陷入登录循环

    问题 xff1a ubuntu用户登录时输入密码又跳回登录界面 解决 xff1a 在登录界面按下ctrl 43 alt 43 F2进入tty2终端 输入用户名和密码 xff08 报错显示login incorrect xff0c 用户名统一
  • 阿里云云效Maven制品仓库的ip白名单列表

    阿里云的云效提供了一系列的云开发工具 xff0c 其中包括 Maven 制品仓库 xff0c 可以提供便捷的 mvn 私库服务 但是因为公司基于安全考虑 xff0c 防火墙策略非常严格 xff0c 仅允许 ip 白名单列表内的数据包可以正常
  • lamp

    文章目录 1 LAMP架构介绍2 web服务器工作流程2 1 cgi与fastcgi2 2 httpd与php结合的方式2 3 web工作流程 3 lamp平台构建3 1 安装httpd3 2 安装mysql3 3 安装php3 4 配置a
  • 自适应中值滤波及MATLAB实现

    自适应中值滤波器是以m n的矩形窗口Sxy定义的滤波器区域内图像的统计特性为基础的 xff0c 可以处理具有更大概率的脉冲噪声如椒盐噪声 xff0c 在平滑非脉冲噪声时能保留细节 在Sxy定义的滤波器区域内定义如下变量 xff1a Zmin
  • Microsoft Visual C++ Runtime Library Runtime Error的解决办法

    打开浏览器时 xff0c 出现Microsoft Visual C 43 43 Runtime Library Runtime Error错误 xff0c 初步估计是软件冲突 xff0c 可能有多种出错的方式 xff0c 我的是浏览器自动关
  • 你应该掌握的——树和二叉树

    我在上课的时候 xff0c 由于各种原因 xff0c 上课老师讲的自己总不爱听 xff0c 现在到火烧眉毛了 xff0c 才知道这些基础知识的重要性 xff0c 现在想想 xff0c 也没有那么的困难 重在理解这些底层的概念 xff0c 然
  • 把学习由复杂变简单(二叉树和树)

    现在发现二叉树和树讲起来真的是没完没了 xff0c 刚发表博客之后发现 xff0c 那还不足以表述这颗大树 我们继续完善 树与二叉树遍历确实很重要 xff0c 但是还有一些你也许忘记的重要知识点 xff0c 我们再来看一下还有什么好玩的 x
  • 原来编译原理可以这么学

    最近对数据结构的研究又有了进展 xff0c 挺好玩的 xff0c 总结这些内容的同时 xff0c 希望也能帮助到大家 xff0c 这样的话 xff0c 达到双赢 xff0c 这才是写博客的目的 xff0c 接下来我们来轻松学习编译原理 xf
  • 流水线—你理解多少?

    流水线 xff1a 流水线是指在程序执行时多条指令重叠进行操作的一种准并行处理实现技术 各种部件同时处理是针对不同指令而言的 xff0c 它们可同时为多条指令的不同部分进行工作 xff0c 以提高各部件的利用率和指令的平均执行速度 概念我们

随机推荐

  • 生活中的PV操作

    之前写过操作系统的文章 xff0c 然后最近发现少了点什么 xff0c 仔细检查发现 xff0c 没写PV操作 xff0c 那么我们接下来就单独为PV操作写一篇博客 xff0c 让大家不再惧怕PV操作 xff0c 我们深入浅出的分析 xff
  • 精通CSS.DIV网页样式与布局(五) ——设置表格与表单样式

    表格和表单是网页中非常重要的两个元素 xff0c 我们这次来说说CSS如何设置表格和表单样式 我们先来看看CSS如何控制表格 首先表格中的标记 xff1a 我们看一下代码 xff1a lt html gt lt head gt lt tit
  • J2EE技术规范(七)——JTA(理解JTA,编写简单的事务客户程序)

    之前的内容中 xff0c 写了几篇关于J2EE规范的博客 xff0c 现在继续来完善这些内容 xff0c 这次内容主要补充上一篇博客 WebLogic Server使用JTA1 0 1a实现和管理事务 WebLogic Server提供以下
  • Android Studio(2.3.3)配置Kotlin笔记

    1 为AS装上Kotlin插件 xff0c 步骤如下 xff1a File gt Settings gt Plugins gt Browse Repositories中搜索 Kotlin gt 安装 xff08 Install xff09
  • J2EE技术规范(八)——JMS(消息,域)

    老样子 xff0c 继续完善J2EE技术规范 xff0c 这次内容主要是写个JMS 理解面向消息的中间件 定义 xff1a 消息 xff08 1 xff09 消息是可编程实现两端通信的机制 xff08 2 xff09 一些消息技术如 xff
  • J2EE技术规范(九)——JMS (JMS客户端)

    上篇博客写了JMS的一些内容 xff0c 后来觉得那篇博客的内容不够阐述JMS的内容 xff0c 所以这篇博客就继续完善JMS 在WebLogic Server 环境中配置JMS WebLogic Server的JMS特性 WebLogic
  • C++多线程项目 - 进程间通信实现(一)

    进程间通信实现 xff08 一 xff09 匿名管道有名管道匿名内存映射内存映射共享内存内存映射与共享内存比较 匿名管道 这里利用的是读时共享的策略 xff0c 因为只有读操作时 xff0c 子进程是共享父进程的资源 xff0c 那么我们进
  • KBQA的主要流程及部分Top竞赛方案总结

    一 KBQA的主要流程 1 1 什么是KBQA 给定自然语言问题 xff0c 通过对问题进行语义理解和解析 xff0c 进而利用知识库进行查询 推理得出答案 1 2 KBQA的实现范式 KBQA在技术上可以分成两种方案 xff0c 分别是一
  • Could not resolve host: github.com的解决方案

    描述 xff1a 新装的ubuntu2004 xff0c git clone命令时遇到标题描述问题 发现可以上网 xff0c 但是ping github com会出错 解决方法 xff1a 第一步 1 打开hosts文件 sudo vim
  • VirtualBox虚拟机网络怎么设置 VirtualBox虚拟机网络设置详细教程

    VirtualBox是国外的一款虚拟系统软件 xff0c 功能强大 xff0c 对于很多开发用户有所帮助 那么VirtualBox虚拟机网络该如何设置呢 相信很多用户会被这么一个问题所困扰 xff0c 下面小编来详细介绍下VirtualBo
  • C++库文件解析(conio.h)

    Conio h 控制台输入输出库 该文内容部分参照百度百科 Conio h 在C stanard library ISO C 和POSIX标准中均没有定义 Conio 是Console Input Output的简写 xff0c 其中定义了
  • [Qt] Linux环境下从源码编译Qt

    官网参考 xff1a Qt for Linux X11 Building from Source Qt 5 15 源码下载 xff1a Index of archive qt 5 15 5 15 0 submodules 这里使用的是各个模
  • Collections.max()方法不返回String类型的实际大小

    对于String类型的迭代器是按照字典序列排序的 xff0c 要让Collections max 方法返回实际的大小 xff0c 需要添加比较器 jdk8中对于Collections max xff09 方法有如下的说明 xff1a 样例
  • 简单三步,Github Pages自定义域名开启HTTPS

    登陆域名服务商后台增加 xff0c 域名解析记录 记录值格式为 xff1a username github io 登陆github xff0c 进行仓库设置 添加 自己的域名 xff0c 开启HTTPS
  • FtpClient.storeFile返回false解决方法

    原文地址为 xff1a FtpClient storeFile返回false解决方法 今天在利用FTP将客户端文件存储到服务器端时 xff0c 在调用ftpClient storeFile方法后 xff0c 总是返回false xff0c
  • 上班一个月,我的几点体会

    这篇博文其实在去年就已经在CSDN发过的 后来 xff0c 某次误操作不小心删除了 xff0c 今天找出来重新发一下 我是从3月1号开始上班的 xff0c 今天3月31号 xff0c 刚好一个月结束 xff0c 在这一个月里 xff0c 我
  • 我这一年写的博文

    总结2013 xff0c 展望2014 xff0c gt gt 我的2013年终总结 在苦与乐中成长 下面是我这一年所写的博客 xff0c 主要涉及C xff0c Net Framework xff0c SQL Server xff0c S
  • 我的2013年终总结——在苦与乐中成长

    写在前面 最近正好在三亚旅游 xff0c 空闲下来时 xff0c 便开始进行年终总结 由于去年年末较忙 xff0c 便错过了2012 年的年终总结 xff0c 所以本文将会对 2012 与 2013 两年一起进行总结 说说工作 学生 到 码
  • 走过2014,2015我将继续前行

    写在前面 一转眼 xff0c 一年时光就这么溜走了 在这辞旧迎新之际 xff08 这说法是不是很官方啊 xff0c 呵呵 xff01 xff09 xff0c 我将对即将过去的2014 年进行一番总结 xff0c 并对即将来临的 2015 年
  • C#使用随机数模拟器来模拟世界杯排名(二)

    接上一篇 xff1a C 使用随机数模拟器来模拟世界杯排名 一 C 使用随机数模拟器来模拟世界杯排名 一 斯内科的博客 CSDN博客 我们使用洗牌随机数算法来匹配世界杯对战国家 xff1a 新建洗牌随机数相关类RandomUtil 用于随机