The application of the ACM learning process 16--list Linked list Joseph problem

Source: Internet
Author: User

Joseph problem is one of the application of list lists, the problem description: N people in a circle, everyone has a unique number, numbering from 1 to N, numbering from 1 to start off, and then report K, number of people out of K, his next from 1 start off, until all the people are out, Ask for the sequence of this column. Combined with the previous blog, here is a list of lists, giving a workable answer code.

Note: N is the number of people, p is the number to be reported.

#include <iostream>
#include <list>

using namespace std;

int main ()
{
	int n,p;
	List<int> l;

	while (cin>>n>>p)
	{
		l.clear ();
		for (int i=1;i<=n;i++)
		{
			l.push_back (i);
		}

		List<int>::iterator It,pdel;

		It=l.begin ();
		while (L.size ()!=1)
		{
			//numbered, it points to the element to be deleted for
			(i=1;i<p;i++)
			{
				it++;
				if (It==l.end ())
					it=l.begin ();
			}
			Delete processing
			cout<<*it<< "->";
			Pdel=it;

			it++;
			It= (It==l.end ())? L.begin (): it;

			L.erase (Pdel);
		}
		cout<<*it<<endl;
	}
	return 0;
}

Java version of the simple Joseph Ring:

public class Test {public static void main (string[] args) {cyclink cyclink=new cyclink ();
		Cyclink.setlength (5);
		Cyclink.createlink ();
		Cyclink.printlink ();
		CYCLINK.SETK (2);
		Cyclink.setm (2);
	Cyclink.play ();
	class Child {int number;
	Child Nextchild;
	public child (int number) {this.number=number;
	}//Ring List class Cyclink {//Ring list size//point to first child application cannot move int length=0;
	int k=0;
	int m=0;
	Child Firstchild=null;
	Child Temp=null;
	Sets the list size public void setlength (int length) {this.length=length;
	public void setk (int k) {this.k=k;
	public void Setm (int m) {this.m=m; }//Initialize ring list public void Createlink () {for (int i=1;i<=length;i++) {///Create First child if (i==1) {Baby ch
			   Ild=new Child (i);
			   This.firstchild=child;
			This.temp=child;
				else {Child child=new child (i);
				This.temp.nextchild=child;
				This.temp=child;
if (i==this.length) {//Create the last child This.temp.nextchild=this.firstchild;				}}} public void Printlink () {child temp=this.firstchild;
    		do {System.out.print (temp.number+ "");
    	Temp=temp.nextchild;
    	}while (Temp!=firstchild);
    System.out.println ();
    	public void Play () {child temp=this.firstchild;
    	Find the number of nodes for (int i=1;i<k;i++) {temp=temp.nextchild; } while (this.length!=1) {//number m for (int j=1;j<m;j++) {Temp=temp.nextchild
        	;
        	//Find out the previous node of the Circle child Temp1=temp;
        	while (temp1.nextchild!=temp) {temp1=temp1.nextchild;
        	///delete the node of M to Temp1.nextchild=temp.nextchild;
        	Temp=temp.nextchild;
        	This.printlink ();
    	this.length--;
    }//Last node System.out.println (temp.number); }
}
Joseph problem There are other variants, such as starting from the first m off or when a person out of the team again to set a number of the value of a count, and so on, only to be on the basis of a C + + program to make changes can be.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.