olivia_yangfan 2017-05-17 16:49 采纳率: 0%
浏览 1417
已结题

c# 运用顺序表解答约瑟夫环问题

问题:设n个人围坐在一个圆桌周围,现在从第s个人开始报数,数到第m个人,让他出局;然后从出局的下一个人重新开始报数,数到第m个人,再让他出局,循环至所有人全部出局为止。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 约瑟夫环顺序链表
{
class Program
{
static int[] Joseph()
{
int n;//人的总数
int m;//间隔的人数
int s;//从第s个开始数
int count = 0, j;//
Console.WriteLine("请您输入n,代表n个人");
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请您输入m,代表报数到m的人出圈");
m = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请您输入s,代表从第s个人开始报数");
s = Convert.ToInt32(Console.ReadLine());
int[] result = new int[n];//存储最后结果的数组
int[] start = new int[n];//存储运算过程中的数据
for (int i = 0; i < n ; i++)
{
start[i] = i;
}
for (int i = n; i >= 2; i--)
{
s = (s + m - 1) % i;//求最后自杀的人的编号
if (s == 0)
{
s = i;
}
result[count] = start[s];
count++;
for(j = s + 1; j <= i - 1;j++)
{
start[j - 1] = start[j];
}
}
result[count] = start[s];
return result;
}
static void Main(string[] args)
{
int[] Josephus = Joseph();
for (int i = 0; i < Josephus.Length; i++)
{
Console.Write("出圈序号为:");
Console.WriteLine(Josephus[i]);
}
Console.ReadLine();
}
}

    这个代码没有错误,但是运行的结果却有问题,想问一下到底在哪里出错了~

}

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-05-18 02:26
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog