第一次做zju的acm,受挫,好心人救我(1088)
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
------------------------------------------
我的程序如下:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct Node
{
int num;
struct Node *next;
}Node,*LinkList;
LinkList Create(int totalnum)
{
LinkList head,current;
head=(Node *)malloc(sizeof(Node));
head->num=totalnum;
head->next=head;
for(int i=totalnum-1;i>=1;i--){
current=(Node *)malloc(sizeof(Node));
current->num=i;
current->next=head;
head=current;
}
return head;
}
int goo(LinkList h,int m,int n)
{
LinkList temp=h;
LinkList p;
while(n>1){
for(int j=1;j<m;j++){
temp->next=temp;
}
p=temp->next;
temp->next=temp->next->next;
free(p);
n--;
}
return temp->num;
}
int play(LinkList first,int totalnum)
{
int m=1;
int goo(LinkList,int,int);
do{
if(goo(first,m,totalnum)==2){
printf("%d",m);
break;
}
m++;
}while(m<totalnum);
return m;
}
void main()
{
int ch,m;
LinkList p;
scanf("%d\n",ch);
if(ch>150||ch<0){
exit(0);
}
if(ch<3&&ch>0){
exit(0);
}
while(ch!=0){
p=Create(ch);
m=play(p,ch);
printf("%d\n",m);
scanf("%d\n",ch);
if(ch>150||ch<0){
exit(0);
}
if(ch<3&&ch>0){
exit(0);
}
}
getche();
}
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
------------------------------------------
我的程序如下:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct Node
{
int num;
struct Node *next;
}Node,*LinkList;
LinkList Create(int totalnum)
{
LinkList head,current;
head=(Node *)malloc(sizeof(Node));
head->num=totalnum;
head->next=head;
for(int i=totalnum-1;i>=1;i--){
current=(Node *)malloc(sizeof(Node));
current->num=i;
current->next=head;
head=current;
}
return head;
}
int goo(LinkList h,int m,int n)
{
LinkList temp=h;
LinkList p;
while(n>1){
for(int j=1;j<m;j++){
temp->next=temp;
}
p=temp->next;
temp->next=temp->next->next;
free(p);
n--;
}
return temp->num;
}
int play(LinkList first,int totalnum)
{
int m=1;
int goo(LinkList,int,int);
do{
if(goo(first,m,totalnum)==2){
printf("%d",m);
break;
}
m++;
}while(m<totalnum);
return m;
}
void main()
{
int ch,m;
LinkList p;
scanf("%d\n",ch);
if(ch>150||ch<0){
exit(0);
}
if(ch<3&&ch>0){
exit(0);
}
while(ch!=0){
p=Create(ch);
m=play(p,ch);
printf("%d\n",m);
scanf("%d\n",ch);
if(ch>150||ch<0){
exit(0);
}
if(ch<3&&ch>0){
exit(0);
}
}
getche();
}
...全文
相关推荐
acm新手必备 浙大acm解答 代码库 zoj zju
acm 新手必备 浙大 解答 代码库 zoj zju
zju1025.rar_ZJU_acm zju 10_pid_sticks_zju 1025
zju 1025 Wooden Sticks http://acm.zju.edu.cn/show_problem.php?pid=1025
acm zju 额度cn
acm zju 额度cnacm zju 额度cnacm zju 额度cnacm zju 额度cnacm zju 额度cn
ACM ZJU 1789 Suspects
用最简洁的语言实现了一下并查集,欢迎下载,如有意见或建议可以提出,谢谢大家的支持!:) #include<iostream> using namespace std; int n,m,k; struct TreeNode { int parent; int num; }T[100000]; void init() //并查集初始化
zju1048.rar_acm.zju.edu._pid_show_zju acm
zju 1048 Financial Managementhttp://acm.zju.edu.cn/show_problem.php?pid=1048


