一道程序设计打击帮忙解答,孙子给爷爷磕头了
--------------------编程问答-------------------- 这是典型的约瑟夫环问题 --------------------编程问答----------------------------------------编程问答--------------------
typedef struct node {
int data;
struct node *next;
} Lnode;
Lnode * creat(int n)
{ /*创建具有 n个结点的单循环链,n 为人数,此函数返回值为该循环链表的头指针h */
int s1=0;
Lnode *p1=(Lnode *)(malloc(sizeof(Lnode)));
p1->data=1;
Lnode *h=p1;
for(s1=1;s1<n;s1++)
{
p1->next=(Lnode *)(malloc(sizeof(Lnode)));
p1=p1->next;
p1->data=s1+1;
}
p1->next=h;
return h;
}
void jeseph(Lnode *p,int m)
{ /*解约瑟夫问题 ,p为环的指针参数,m 为报数值 */
Lnode *p1=p;
Lnode * tmp1=NULL;
int s1=1;
while(p1->next!=p1)
{
if (s1==m-1)
{
tmp1=p1->next->next;
printf("%d\n",p1->next->data);
free(p1->next);
p1->next=tmp1;
s1=0;
}
p1=p1->next;
s1++;
}
}
void main()
{ Lnode * h;
int m,n;
printf("\n input n,m=");
scanf("%d,%d",&n,&m);
h=creat(n);
jeseph(h,m);
}
/// <summary>--------------------编程问答-------------------- --------------------编程问答--------------------
///
/// </summary>
/// <param name="n">n>=1</param>
/// <param name="s">s>=1&&s<=n</param>
/// <param name="m">m>=1&&m<=n</param>
static void PrintProcesCricle(int n, int s, int m)
{
List<int> lst = Enumerable.Range(1, n).ToList();
int pos = s - 1;
for (var i = 0; i < n - 1; i++)
{
pos = (pos + m) % lst.Count;
Console.WriteLine("排除 {0}", lst[pos]);
lst.RemoveAt(pos);
}
Console.WriteLine("最后的序号是:{0}", lst[0]);
}
+1 --------------------编程问答-------------------- 典型的约瑟夫问题 循环链表 还有 数组都可以
--------------------编程问答-------------------- http://topic.csdn.net/u/20120224/19/a11cbb43-a228-4e42-89e0-f77dc51f6180.html
变换下又不会了?
还有注释两个字都写不对,这么多天过去了,还写不对?
我真服你了。我看你还是练习练习磕头吧,装孙子可能是更适合你的职业, --------------------编程问答-------------------- 你这+1是啥意思啊?
--------------------编程问答-------------------- 这代码看不懂,是不是有问题啊?
--------------------编程问答-------------------- 约瑟夫环 --------------------编程问答-------------------- 就是支持三楼的意思。三楼的解答正确。你前几天提的问题是这三个数是确定的。今天提的是可以设置的。所以写了个函数解决。明白了? --------------------编程问答-------------------- ///是C#对方法块的注释,调用该方法时效果就像你用.net的标准方法那样给出说明。 --------------------编程问答--------------------
这个程序是耶稣找叛徒的那个程序,该怎么改成这个程序呢?主要的不懂的地方就是上次那个题目是从1号开始报数,这个是从s号开始报数?这个程序该怎么办?请你帮忙我解答哈。谢谢咯。
class Program
{
static void Main(string[] args)
{
GetTheThreePeople();
Console.ReadKey();
}
public static void GetTheThreePeople()
{
int peopleCount = 13;
List<People> peopleList = new List<People>(peopleCount);
int iIndex = 1;
//赋默认值1-13的编号
while (iIndex <= peopleCount)
{
peopleList.Add(new People() { Index = iIndex });
iIndex++;
}
//记录每一次的1-3的序号
int numIndex = 1;
//最多三个为一组
int breakNumber = 3;
//为每三个编一个号1-3,循环编号
while (peopleList.Count > 1)
{
//为教徒列表编号1-3
peopleList.ForEach(p =>
{
if (numIndex > breakNumber)
{
numIndex = 1;//过了3之后重新开始1
}
//编号
p.NumberIndex = numIndex++;
});
//获取去除编号3的之后,递归调用
peopleList = RemovePeople(peopleList);
}
//只剩一个了,获取索引
Console.WriteLine(peopleList[0].Index);
}
//忽略编号3的,得到新列表
private static List<People> RemovePeople(List<People> peopleList)
{
List<People> retList = new List<People>();
peopleList.ForEach(p =>
{
//如果不是3就添加到新列表中
if (!p.IsRemove)
{
retList.Add(p);
}
});
return retList;
}
private class People
{
/// <summary>
/// 实际位置
/// </summary>
public int Index { get; set; }
/// <summary>
/// 处于1-3的位置
/// </summary>
public int NumberIndex { get; set; }
/// <summary>
/// 是否该移除,3为移除
/// </summary>
public bool IsRemove { get { return NumberIndex == 3; } }
}
} --------------------编程问答--------------------
补充:.NET技术 , C#