System Overload
Recently you must have experienced that when too many people use the BBS simultaneously, the net becomes very, very slow.
To put an end to this problem, the Sysop has developed a contingency scheme for times of peak load to cut off net access for some buildings of the university in a systematic, totally fair manner. Our university buildings were enumerated randomly from 1 to n. XWB is number 1, CaoGuangBiao (CGB) Building is number 2, and so on in a purely random order.
Then a number m would be picked at random, and BBS access would first be cut off in building 1 (clearly the fairest starting point) and then in every mth building after that, wrapping around to 1 after n, and ignoring buildings already cut off. For example, if n=17 and m=5, net access would be cut off to the buildings in the order [1,6,11,16,5,12,2,9,17,10,4,15,14,3,8,13,7]. The problem is that it is clearly fairest to cut off CGB Building last (after all, this is where the best programmers come from), so for a given n, the random number m needs to be carefully chosen so that building 2 is the last building selected.
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
编译环境windows xp sp2 VC++ 2005 Express Edition Beta
以下源码仅参考切勿直接提交使用
#include<iostream>
using namespace std;
const int N=150;
void main()
{
int best[N];//best[n]保存总数是n+1的最小步长,程序中实际考虑支持第一个节点的情形
best[2]=2;
for(int i=3;i<N;i++)
{
int b=2;//当前正在测试的步长
int p=i;//p保存经历一轮挑选后剩余的building总数
int c=0;//经过一轮挑选后还剩余的没有数过的building数
while(1)
{
if(i==b)//步长与总数相等必然失败
{
b++;
continue;
}
if((p+c)>b)//p+c是下一轮挑选的building总数
{
int t=c;
c=(p+c-(p+c)/b*b);
p-=(p+t)/b;
if(c==0&&b==best[p])//如果与已经计算出的某个情形相同则记录
{
best[i]=b;
break;
}
if((c==0&&b<best[p])||(b-c==1&&p!=1))//如果与已经计算出的某个情形相同但步长要小则中止,如果数到第一栋建筑则中止
{
b++;
p=i;
c=0;
continue;
}
}
else
{
c=p-(b-c)%p;
if(c==p)
c=0;
p--;
if(p==1&&c==0)//标志着数到最后一个是第一栋建筑
{
best[i]=b;
break;
}
if(p==c)//标志着数到了第一栋建筑
{
b++;
p=i;
c=0;
continue;
}
}
}
}
int n;
while(cin>>n,n>0)
cout<<best[n-1]<<endl;
}
“改进”后的版本
#include<iostream>
using namespace std;
const int N=151;
void main()
{
int best[N]={0,0,2,5,2,4,3,11,2,3,8,16,4,21,6,5,2,
11,20,34,8,15,10,7,13,11,13,45,18,23,8,3,2,25,
75,42,13,5,23,13,50,16,18,89,38,8,39,30,29,38,
7,45,23,137,46,63,17,48,5,46,34,140,33,39,2,28,
29,79,33,48,3,10,46,120,6,37,17,8,44,15,160,20,
35,144,104,179,153,24,8,265,19,9,62,7,139,19,44,
93,182,27,158,185,193,17,82,3,11,43,55,21,41,
146,29,80,59,8,29,66,19,160,59,28,129,127,120,
72,45,157,2,63,127,81,318,513,98,28,32,231,236,
411,26,45,5,303,228,66,9,205,65,39,16};
int n;
while(cin>>n,n>0)
cout<<best[n-1]<<endl;
}