这是我用集合类ArrayList写的约瑟夫环问题的算法。
界面:
代码:
C# Code:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
Code
namespace Test1
{
/**//// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
public void outPerson(int totPerson, int spaPerson,int startIndex) //这个方法是算法主体
{
ArrayList totaPerson = new ArrayList();
for (int i = 0; i < totPerson; i++)
{
totaPerson.Add(i+1);
}
startIndex -= 1;
do
{
int temp1=0,temp2 = 0;
if ((startIndex + spaPerson - 1) < totaPerson.Count - 1)
{
temp1 = startIndex + spaPerson - 1;
sacrificerList.Text += "第" + totaPerson[temp1].ToString() + "个勇士走出队列" + "\r\n";
totaPerson.RemoveAt(temp1);
startIndex += spaPerson - 1;
}
if ((startIndex + spaPerson - 1) >=totaPerson.Count - 1)
{
temp2 = (startIndex + spaPerson - 1) % totaPerson.Count;
sacrificerList.Text += "第" + totaPerson[temp2].ToString() + "个勇士走出队列" + "\r\n";
totaPerson.RemoveAt(temp2);
startIndex = temp2;
}
}
while (totaPerson.Count>=spaPerson);
sacrificerList.Text += "******************************"+"\r\n";
foreach (int i in totaPerson)
{
sacrificerList.Text += "第" + i + "个战士留下来了"+"\r\n";
}
}
private void GameStart_Click(object sender, RoutedEventArgs e)
{
sacrificerList.Text = "";
try
{
int allPerson = int.Parse(totalNumber.Text);
int spaceNo = int.Parse(spaceNumber.Text);
int startLoc = int.Parse(startLoca.Text);
if (spaceNo > allPerson)
{
sacrificerList.Text += "请输入正确的数值,注意步长要小于人数!";
}
else if (spaceNo <= allPerson)
{
outPerson(allPerson, spaceNo, startLoc);
}
}
catch (FormatException fEx)
{
sacrificerList.Text += fEx.Message;
}
catch (InvalidCastException iCe)
{
sacrificerList.Text += iCe.Message;
}
catch (OverflowException oFe)
{
sacrificerList.Text += oFe.Message;
}
}
private void Quit_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
namespace Test1
{
/**//// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
public void outPerson(int totPerson, int spaPerson,int startIndex) //这个方法是算法主体
{
ArrayList totaPerson = new ArrayList();
for (int i = 0; i < totPerson; i++)
{
totaPerson.Add(i+1);
}
startIndex -= 1;
do
{
int temp1=0,temp2 = 0;
if ((startIndex + spaPerson - 1) < totaPerson.Count - 1)
{
temp1 = startIndex + spaPerson - 1;
sacrificerList.Text += "第" + totaPerson[temp1].ToString() + "个勇士走出队列" + "\r\n";
totaPerson.RemoveAt(temp1);
startIndex += spaPerson - 1;
}
if ((startIndex + spaPerson - 1) >=totaPerson.Count - 1)
{
temp2 = (startIndex + spaPerson - 1) % totaPerson.Count;
sacrificerList.Text += "第" + totaPerson[temp2].ToString() + "个勇士走出队列" + "\r\n";
totaPerson.RemoveAt(temp2);
startIndex = temp2;
}
}
while (totaPerson.Count>=spaPerson);
sacrificerList.Text += "******************************"+"\r\n";
foreach (int i in totaPerson)
{
sacrificerList.Text += "第" + i + "个战士留下来了"+"\r\n";
}
}
private void GameStart_Click(object sender, RoutedEventArgs e)
{
sacrificerList.Text = "";
try
{
int allPerson = int.Parse(totalNumber.Text);
int spaceNo = int.Parse(spaceNumber.Text);
int startLoc = int.Parse(startLoca.Text);
if (spaceNo > allPerson)
{
sacrificerList.Text += "请输入正确的数值,注意步长要小于人数!";
}
else if (spaceNo <= allPerson)
{
outPerson(allPerson, spaceNo, startLoc);
}
}
catch (FormatException fEx)
{
sacrificerList.Text += fEx.Message;
}
catch (InvalidCastException iCe)
{
sacrificerList.Text += iCe.Message;
}
catch (OverflowException oFe)
{
sacrificerList.Text += oFe.Message;
}
}
private void Quit_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}