| | |
Help with list
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Well I see that you will need to delete all the members until you are left with one.
So you will need to do this.
I dint check the code. But it should work.
So you will need to do this.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <list> using namespace std; int main() { int n;//number of soldiers int k;//number skipped between cout << "Welcome to The Josephus Problem" << endl; cout << "Please enter the number of soldiers in the army." << endl; cin >> n; cout << "Please enter the number to be skipped between sucessive executions." << endl; cin >> k; list < int > soldiers; for ( int i = 0; i < n; i++ ) soldiers.push_back ( i + 1 ); list < int > :: iterator p; p = soldiers.begin (); while(soldiers.size()==1) { for ( int i = 0; i < k-1; i++ ) { p++; if ( p == soldiers.end() ) p = soldiers.begin (); } p = soldiers.erase ( p ); for ( p = soldiers.begin(); p != soldiers.end(); p++ ){ cout << *p; } } system("pause"); return 0; }
I dint check the code. But it should work.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <list> using namespace std; int main() { int n;//number of soldiers int k;//number skipped between cout << "Welcome to The Josephus Problem" << endl; cout << "Please enter the number of soldiers in the army." << endl; cin >> n; cout << "Please enter the number to be skipped between sucessive executions." << endl; cin >> k; list < int > soldiers; for ( int i = 0; i < n; i++ ) soldiers.push_back ( i + 1 ); list < int > :: iterator p; p = soldiers.begin (); while(soldiers.size()!=1) { for ( int i = 0; i < k-1; i++ ) { p++; if ( p == soldiers.end() ) p = soldiers.begin (); } p = soldiers.erase ( p ); for ( p = soldiers.begin(); p != soldiers.end(); p++ ){ cout << *p; } } system("pause"); return 0; }
I guess it is solved now.
![]() |
Similar Threads
- DropDown List (VB.NET)
- Help me make a list of database servers! (Database Design)
- How do I print a list of 'Favorites' ?? (Web Browsers)
- List Catagories (C)
- vB Sessions/Online list (PHP)
- Win98SE - clearing out startup list (Windows 95 / 98 / Me)
- A List of Acronyms (Geeks' Lounge)
- Posting List (DaniWeb Community Feedback)
Other Threads in the C++ Forum
- Previous Thread: Another problem
- Next Thread: Matrix Inversion help :'(
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





