Circulation delete elements, returns the last element to be removed subscript

Enter an integer n (n is two or more, 1,000 or less), i = 2 from two elements of the array removed arr [n] of the element in the array may be circulated, a return to the last element to be removed every start subscript:

#include<iostream>
#include<vector>
using namespace std;
int search(int n){
    vector<int>visited(n,0);
    if(n==2){
        return 1;
    }
    int count=0;
    int i=2;
    while(true){ 
         visited[i]=1;
         count++;
        if(count==n){
           return i;
        }
       int count2=0;
       while(visited[i]==1||count2<2)
       {
           if(visited[i]==0){
            count2++;
            }
            i++;
            i=i%n;
       } 
    }
}
int main(){
    int n;
    while(cin>>n){
        cout<<search(n)<<endl;
    }
}

Guess you like

Origin www.cnblogs.com/qiuhaifeng/p/11515375.html