分享
 
 
 

acm.jlu.edu.cn-1097-System Overload

王朝system·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有