有道ACM的题和你的题一样.我把这ACM的题的答案给你!
#include<stdio.h>
int main()
{
int n,m,answer
while(scanf("%d %d",&n,&m)==2)
{
answer=m-(n-1)/2
if(answer>0)
printf("%d\n",answer)
else
printf("0\n")
}
return 0
}
我自己的编程思路是:定义一个数组,里面填充1-30这些数字,然后循环,到要求的数字时将对应位置的元素填充为一个特别数字,标识这个数字失效没有作用,循环的条件是数组里面的元素是不是还剩下六个,最后,输出数组元素的对应位置就是原来的第几号海盗,
约瑟夫环typedef struct node
{
int num,code
struct node *next
}lnode
void main()
{
int i,j,key,n/*i,j为记数器,key为输入的密码,n为人的总个数*/
lnode *p,*s,*head
head=(lnode *)malloc(sizeof(lnode))/*为头结点分配空间*/
p=head
printf("Please enter the num of the person:")/*输入人的总个数*/
scanf("%d",&n)
for(i=1i<=ni++)
{
printf("Person %d",i)
printf(" code: ")
scanf("%d",&key)/*输入各个人的密码*/
s=p
p=(lnode *)malloc(sizeof(lnode))/*创建新的结点*/
s->next=p
p->num=i
p->code=key
}
p->next=head->next
p=head
head=head->next
free(p)
p=head
do
{
printf("\nPerson%d Code:%d",p->num,p->code)/*输出链表*/
p=p->next
}while(p!=head)
printf("\nPlease enter your first key:")/*输入第一个数*/
scanf("%d",&key)
do
{
j=1/*j为记数数*/
p=head
while(j<key)
{
s=p
p=p->next
j++
}
i=p->num
key=p->code
printf("\nThe out of the num:")
printf("Person%d",i)
s->next=p->next
head=p->next/*重新定义head,下次循环的开始结点*/
free(p)
n--/*每循环一次人是减1*/
}while(n>0)
getch()
}