第一次做zju的acm,受挫,好心人救我(1088)

rkhw 2004-10-01 08:10:17
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();
}
...全文
145 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
galois_godel 2004-10-02
  • 打赏
  • 举报
回复
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int m,n;
int mm[1500];
int getans(int n)
{

if(n==1)return 0;
return (getans(n-1)+m)%n;

}


int main()
{
memset(mm,0,sizeof(mm));
while(scanf("%d",&n)==1){
if(n==0)break;
if(mm[n]==0){
m=1;
while(getans(n-1))m++;
mm[n]=m;
}
printf("%d\n",mm[n]);
}

return 0;
}

louisbadbad 2004-10-02
  • 打赏
  • 举报
回复
galois_godel的用递归实现的,我觉得也很不错。我向你们二位学习!
louisbadbad 2004-10-02
  • 打赏
  • 举报
回复
rkhw (我是菜鸟我怕怕) 我觉得的你的程序没有问题,只是可能比较费时。o(n^2)的时间复杂度
languagec 2004-10-02
  • 打赏
  • 举报
回复
rkhw 2004-10-02
  • 打赏
  • 举报
回复
to jp1984(毛咏洁) :
你说的很对,主要以前都写C++,没有这个习惯。
jp1984 2004-10-01
  • 打赏
  • 举报
回复
程序能通过编译???
scanf("%d\n",ch); /* & 都没加 */
还有 C 里面循环里好像不能定义局部变量 ..

33,006

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧