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 Specification
The 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 Specification
For each line of the input, print one line containing the integer m fulfilling the requirement specified above.Sample Input
3
4
5
6
7
8
9
10
11
12
0
Sample Output
2
5
2
4
3
11
2
3
8
16
Source: University of Ulm Local Contest 1996
俺的:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
2 #include "targetver.h"
3
4 #include <stdio.h>
5 #include <tchar.h>
6
7
8
9 // TODO: 在此处引用程序需要的其他头文件
10 #include <vector>
11 #include <list>
12 #include <string>
13 #include <iostream>
14 #include <exception>
15 #include <stdexcept>
16
17 typedef struct{
18 int num;
19 bool dele;
20 }Elem;
21
22 // LabCPP.cpp : 定义控制台应用程序的入口点。
23 //
24
25 #include "stdafx.h"
26
27 using namespace std;
28
29 bool FTestM(vector<Elem> test,vector<Elem>::size_type m)
30 {
31 vector<Elem>::size_type total=test.size(); //记录还有多少元素未被测试
32 vector<Elem>::size_type count=0; //计数器
33 try
34 {
35 while(!(count==1 && test.at(1).dele==false && total==1)) //测试m是否为2且未被删除且只剩下一个元素
36 {
37 //if(test.at(count).dele==true) //如果该位置的元素已经被删除
38 //{
39 // count++; //计数器 +1
40 // continue; //循环继续
41 //}
42 if(count==1 && total != 1 && total !=test.size()) //如果m为2,但是还有其他的元素未被删除,则说明m值不合适
43 {
44 return false; //函数返回false
45 }
46 if(count==1 && total ==1 && total !=test.size()) //m合适
47 {
48 return true; //函数返回true
49 }
50
51 //处理元素
52 test.at(count).dele=true; //删除该元素
53 total--; //总数 -1
54 //count+=m; //计数器 +m
55 ////处理越界问题
56 //if(count>=test.size())
57 //{
58 // count=count%test.size();
59 //}
60
61 //使用迭代器处理越界和删除元素的情况
62 try
63 {
64 vector<Elem>::iterator itr=(test.begin()+count); //不知道指向的位置,假设为下表为count的位置
65 int tempcount=0;
66 while(tempcount!=m)
67 {
68 itr++; //迭代器自加操作
69 if(itr==test.end()) //超过界限,返回到第一位
70 itr=test.begin();
71 if((*itr).dele==false) //如果该元素没被删除
72 tempcount++; //计数器 +1
73 }
74 count=(*itr).num;
75 }
76 catch(exception ex4)
77 {
78 cout<<"ex4 error:"<<ex4.what()<<endl;
79 }
80 }
81 return true;
82 }
83 catch(exception ex3)
84 {
85 cout<<ex3.what()<<endl;
86 cout<<"ex3 error"<<endl;
87 }
88 //catch(runtime_error rterr)
89 //{
90 // cout<<rterr.what()<<endl;
91 //}
92 //catch(range_error rgerr)
93 //{
94 // cout<<rgerr.what()<<endl;
95 //}
96 }
97
98 int _tmain(int argc, _TCHAR* argv[])
99 {
100 int n; //n:有多少建筑
101 cin>>n;
102
103
104 Elem elem; //初始一个Elem值
105 elem.dele=false;
106 elem.num=0;
107
108 vector<Elem> test(n,elem); //创建一个名为test的vector容器,内含n个elem的副本
109 try
110 {
111 for (vector<Elem>::size_type i=0;i != n;i++) //初始化test容器内的元素
112 {
113 test.at(i).num=i;
114 }
115 }
116 catch(exception ex)
117 {
118 cout<<ex.what()<<endl;
119 cout<<"ex error";
120 }
121
122 vector<Elem>::size_type m;
123 try
124 {
125 for(m=2;m != 150;m++)
126 //穷举法测试出m的值
127 //150?不确定
128 {
129 if(FTestM(test,m)) //FTestM()测试m是否合适
130 break;
131 else
132 ;
133 }
134 }
135 catch(exception ex2)
136 {
137 cout<<ex2.what()<<endl;
138 cout<<"ex2 error";
139 }
140 //输出m的值
141 std::cout<<"The value of 'm' is :"<<endl;
142 std::cout<<m;
143
144 system("pause");
145 return 0;
146 }
147
148
牛人的:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
2 using namespace std;
3
4
5 int m;
6 int n;
7
8 int check(){//优胜者是CGB,返回1,其他返回0
9 int r=0;//每轮的胜利者
10 for(int i=2; i<n; i++)
11 r = (r+m)%i;
12 if(r==0)
13 return 1;
14 else return 0;
15 }
16 int main()
17 {
18
19 while(cin>>n && n!=0)
20 {
21 m=1;
22 while(!check())
23 m++;
24 cout<<m<<endl;
25
26 }
27 }
哭过之后继续努力,呵呵