c# 窗体猴子选大王
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{/*顺序表应用*/
int n,m, d, i, count,pos=0; string t=""; //n代表猴子个数,m代表要淘汰的报数,d代表淘汰的猴子个数,count代表报数记录
textBox3.Clear(); textBox4.Clear();
n = int.Parse(textBox1.Text); m = int.Parse(textBox2.Text);
int[] A = new int[n];
for (i=0;i<n;i++) A[i]=i+1;
//出队前:
for (i = 0; i < n; i++) t = t + A[i]+" "; textBox3.Text = t;
//出队后:
count=0;
d=0; /*d记录退出圈外的猴子个数*/
while (d<n)
for (i=0;i<n;i++)
if (A[i]!=0)
{
count++;
if (count==m) /*第i个猴子退出*/
{
textBox4 .Text =textBox4 .Text + A[i].ToString ()+" ";
count=0; /*计数器重置为0*/
d++; pos = i;
}
}
label5.Text = "祝贺" + (pos+1) + "号!";
}
}
}
各位大哥看下我的代码错哪了啊。。。。。急啊
[解决办法]
本来要是4号的 可是不懂代码哪错了
[解决办法]
报3的没被真正淘汰,还在队里参与报数呢。
[解决办法]
经典的约瑟夫环问题,一般用链表解决
[解决办法]
好老师,不说是约瑟夫环问题,说是“猴子选大王”,“猪八戒选媳妇”,看你上哪里百度去。
[解决办法]