这题题目没怎么弄清,不知道循环旋转的时候从哪开始,也就不想了,写个简答的约瑟夫循环
#include<iostream> using namespace std ; struct LinkNode { LinkNode *link ; int data ; } ;//dingyi void CreatList (LinkNode *first,int number) { // LinkNode *first ;*first ; LinkNode *current = first ; // int number ; // int m1 ; // int m2 ; // cin >> number ; // cin >> m1 ; // cin >> m2 ; for(int i =1 ;i < number ;i++ ){ //create current->data = i ; LinkNode *newNode =new LinkNode ; current->link = newNode ; current = newNode ; } current->data = number ; current->link = first ; } void print(LinkNode *first ) { //int i= 0; while (first != NULL) { cout << first ->data << endl; //i++; first = first->link ; } //cout <<i; } void Josephus (LinkNode *first,int number,int m1 ) { LinkNode *pre =NULL ; int i ; int j ; int arr[number] ; for(int k=1 ;k<=8;k++) { arr[k] =k ; } for(i=1 ;i<number ;i++) { for(j=1;j<m1;j++) { pre = first ; first = first->link ; } cout <<"the out number is " <<first->data <<endl; for(int g=1;g<=8 ;g++) { if(first->data == arr[g] ) { arr[g] =0 ; } } pre->link =first->link ; delete first ; first =pre ->link ; } //cout the answer ; for(int a =1 ;a<=8;a++ ) { if(arr[a] !=0) { cout << "the final is " ; cout <<arr[a] <<endl; break ; } } } int main () { LinkNode *first = new LinkNode ; //first->data = 100 ; //first->link = NULL ; int number ,m1 ,m2 ; cin >> number ; cin >> m1 ; CreatList (first , number ) ; Josephus (first ,number , m1) ; cout <<endl ; // cin >> m2 ; // return 0 ; }
时间: 2024-10-03 15:13:28