| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 521 人关注过本帖
标题:请帅哥美女们进来帮小弟一个忙。谢了!
只看楼主 加入收藏
以中
Rank: 3Rank: 3
来 自:长沙
等 级:论坛游侠
帖 子:108
专家分:129
注 册:2010-4-13
结帖率:80%
收藏
已结贴  问题点数:20 回复次数:1 
请帅哥美女们进来帮小弟一个忙。谢了!
n个人围成一圈,从第一个人起,1,2,3的循环,拿到三号的退出,问最后剩下的是原来的几号?

/* Note:Your choice is C IDE */
#include "stdio.h"
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
    int num;
    struct student *next;
};
int n;
struct student *creat(void)
{
    struct student *head;
    struct student *p1,*p2;
    int j;
    for(j=1;j<=n;j++)
    {p1=p2=(struct student *)malloc(LEN);
    &p1->num=j;
    head=NULL;
    while(p1->num!=0)
    {
        n=n+1;
        if(n==1)head=p1;
        else p2->next=p1;
        p2=p1;
        /*p1=(struct student*)malloc(LEN);
        scanf("%ld",&p1->num);
        */
    }
    }
p2->next=NULL;
return(head);
}
struct student *del(struct student *head)
{
    struct student *p1,*p2;
    int i;
    if(head==NULL){printf("\nlist null!\n");return(head);}
    p1=head;
    for(i=0;;i++)
 {
        
    /*loop:*/ p2=p1;p1=p1->next;
            
    if(i%3==0)
        {/*if(p1==head)head=p1->next;*/
        /*else*/ p2->next=p1->next;
        n=n-1;
        }
    if(p1->next==NULL)
    p1->next=head;
/*    goto loop;*/
    if(n==1)
    return(head);
 /*   break;*/
 }
}
void print(struct student *head)
{
    struct student *p;
    printf("\nNow,These %d records are:\n",n);
    p=head;
    if(head!=NULL)
    do
    {printf("%ld",p->num);
        p=p->next;
        }while(p!=NULL);
    }
void main()
{
    int n;
 struct student *head;
 printf("input  n:\n");
 scanf("%ld\n",&n);
 head =creat();
 print(head);
 printf("\nnow begin to delete number three.");
 head=del(head);
  printf("\nnow begin to show the last number.");  
  print(head);
}帮忙修正一下,谢谢了。
搜索更多相关主题的帖子: 美女 帅哥 
2010-08-06 12:22
erikyo
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:270
专家分:1154
注 册:2010-6-10
收藏
得分:20 
这是一个典型的约瑟夫环的问题,感觉楼主写得有点混乱,按照你的意图大致写了一个,供楼主参考一下:
#include <stdio.h>
#include <stdlib.h>

struct student
{
    int num;
    struct student *next;
};

#define LEN sizeof(struct student)

//int n;

struct student *creat(int n)        //创建循环链表
{
    struct student *head = NULL;
    struct student *p1,*p2;
    int j;
    for(j=0;j<n;j++)
    {
            p1=(struct student *)malloc(LEN);
            p1->num=j;

         if(NULL == head)
            head = p1;
        else
            p2->next = p1;
        p2 = p1;
    }
   
    p1->next = head;
    return(head);
}

struct student *del(struct student *head, int n)        //删除满足要求的结点
{
    struct student *p1,*p2,*pfree;
    int i = 0;
   
    if(head==NULL)
    {
        printf("\nlist null!\n");
        return(head);
    }
   
    p1=head;

    while(p1->next != p1)            
    {
        ++i;
        p2 = p1;
        p1 = p1->next;
        if(i % 3 == 0)
        {
            pfree = p1;
            printf("%d\n",p1->num);
            p2->next = p1->next;
            free(pfree);
            p1 = p2;
        }
        
    }
    return p1;
}

void print(struct student *head, int n)        //因为是循环链表所以只能借助i来控制
{
    struct student *p;
    int i = 0;
    printf("\nNow,These %d records are:\n",n);
    p=head;
    if(head!=NULL)
    for(i = 0; i < n; i++ )
    {
        printf("%d",p->num);
       p=p->next;
     }
}

int main()
{
    int n;
    struct student *head;
   
    printf("input  n:\n");
    scanf("%d",&n);
   
    head =creat(n);
    print(head, n);
    printf("\nnow begin to delete number three.\n");
    head=del(head,n);
      printf("\nnow begin to show the last number.\n");  
      printf("The last num is %d!\n",head->num);
    free(head);
    return 0;
}
收到的鲜花
  • 以中2010-08-07 09:28 送鲜花  3朵   附言:我很赞同
2010-08-06 16:01
快速回复:请帅哥美女们进来帮小弟一个忙。谢了!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040921 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved