Recently you must have experienced that when too many people use the BBS simultaneously, the net becomes very, very slow. Your job is to write a program that will read in a number of buildings n and then determine the smallest integer m that will ensure that our CGB Building can surf the net while the rest of the university is cut off. Input SpecificationThe input file will contain one or more lines, each line containing one integer n with 3 <= n < 150, representing the number of buildings in the university.Input is terminated by a value of zero (0) for n. Output SpecificationFor each line of the input, print one line containing the integer m fulfilling the requirement specified above.Sample Input3 4 5 6 7 8 9 10 11 12 0 Sample Output2 5 2 4 3 11 2 3 8 16 |
直接模拟即可
#include <bits/stdc++.h>
using namespace std;
int a[160];
int findd(int n,int s){
a[1]=1;
int w=1;
int d=1;
while(1){
if(w==n-1)
break;
for(int i=1;i<=s;i++){
d++;
if(d>n)
d=1;
while(a[d]!=0){
d++;
if(d>n)
d=1;
}
}
a[d]=w+1;
w++;
}
if(a[2]==0)
return 1;
else
return 0;
}
int main(){
int n;
while(scanf("%d",&n)!=EOF && n){
for(int i=2;;i++){
memset(a,0,sizeof(a));
if(findd(n,i)){
printf("%d\n",i);
break;
}
}
}
}