Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
f1101.cpp
Package: c.rar [view]
Upload User: puke2000
Upload Date: 2022-07-25
Package Size: 912k
Code Size: 2k
Category:
CSharp
Development Platform:
Visual C++
- //=====================================
- // f1101.cpp
- // josephus problem procedural solving
- //=====================================
- #include<iostream>
- using namespace std;
- //-------------------------------------
- struct Jose{ // 小孩结点
- int code; // 小孩编号
- Jose* next; // 指向下一个小孩结点
- };//-----------------------------------
- int n, s, m;
- Jose *pCur, *pivot;
- //-------------------------------------
- bool getValue();
- Jose* createRing(); // 创建环链表
- void countBoy(int m); // 数m个小孩
- void process(); // 排除n-1个小孩
- //-------------------------------------
- int main(){
- if(!getValue()) return 1;
- Jose* pJose = createRing();
- process();
- cout<<"nThe winner is "<<pCur->code<<"n";
- delete[] pJose;
- }//------------------------------------
- bool getValue(){
- cout <<"please input boyNumber, startPosition, intervalNumber:n";
- cin>>n>>s>>m;
- if(n>=2 && s>=1 && s<=n && m>=1 && m<=n) return true;
- cerr<<"failed in bad boyNumber or startPosition or intervalNumber.n";
- return false;
- }//------------------------------------
- Jose* createRing(){
- Jose* px = new Jose[n];
- for(int i=1; i<=n; ++i){
- px[i-1].next = &px[i%n];
- px[i-1].code = i;
- }//------------------------
- cout<<"There are "<<n<<" boys.nBoys leaved in order:n";
- pivot = &px[n-2];
- pCur = &px[n-1];
- countBoy(s-1);
- return px;
- }//------------------------------------
- void countBoy(int m){
- for(int i=0; i<m; ++i){
- pivot = pCur;
- pCur = pivot->next;
- }
- }//------------------------------------
- void process(){
- for(int i=1; i<n; ++i){
- countBoy(m);
- static int line=0;
- cout<<" "<<pCur->code;
- if(!(++line%10)) cout<<"n";
- pivot->next = pCur->next; //小孩脱链
- pCur = pivot;
- }
- }//====================================