沃斯戈-莎比 发表于 2022-12-3 17:23:52

约瑟夫问题




#include <stdio.h>
#include <stdlib.h>

struct node{
        int date;
        node *next;
};
int main(){
        int n,m;
        scanf("%d %d",&n,&m);
        node *head,*p,*now,*prev;
        head = (node *)malloc(sizeof(node));
        head->date = 1;head->next = NULL;
        now = head;
        for(int i=2;i<=n;i++){
                p = (node *)malloc(sizeof(node));
                p->date=i;p->next = NULL;
                now->next = p;
                now = p;
        }
        now->next = head;
        now = head,prev = head;
        while((n--)>1){
                for(int i=1;i<m;i++){
                        prev = now;
                        now = now->next;
                }
                printf("%d ",now->date);
                prev->next = now->next;
                free(now);
                now = prev->date;
        }
        printf("%d",now->date);
        free(now);
        return 0;
}

为什么我把他换成c语言的他就出问题啦c语言还不太懂有谁知道吗{:5_104:}

zhangjinxuan 发表于 2022-12-3 17:31:58

C语言中结构体类型前要加 struct

zhangjinxuan 发表于 2022-12-3 17:36:53

【对管理员说的话:别删这个帖子,好吗{:10_254:}】

你把所有的 node 前面都加上 struct,就像这样:
#include <stdio.h>
#include <stdlib.h>

struct node{
      int date;
      struct node *next;
};
int main(){
      int n,m;
      scanf("%d %d",&n,&m);
      struct node *head,*p,*now,*prev;
      head = (struct node *)malloc(sizeof(struct node));
      head->date = 1;head->next = NULL;
      now = head;
      for(int i=2;i<=n;i++){
                p = (struct node *)malloc(sizeof(struct node));
                p->date=i;p->next = NULL;
                now->next = p;
                now = p;
      }
      now->next = head;
      now = head,prev = head;
      while((n--)>1){
                for(int i=1;i<m;i++){
                        prev = now;
                        now = now->next;
                }
                printf("%d ",now->date);
                prev->next = now->next;
                free(now);
                now = prev->next; //这里应该是 prev -> next,不应还是 prev -> date
      }
      printf("%d",now->date);
      free(now);
      return 0;
}
这是C语言的语法,没办法,但是你还可以这么做,就是用typedef关键字:
#include <stdio.h>
#include <stdlib.h>

typedef struct node{
      int date;
      struct node *next; //但是这里还要加 struct
} node;
int main(){
      int n,m;
      scanf("%d %d",&n,&m);
      node *head,*p,*now,*prev;
      head = (node *)malloc(sizeof(node));
      head->date = 1;head->next = NULL;
      now = head;
      for(int i=2;i<=n;i++){
                p = (node *)malloc(sizeof(node));
                p->date=i;p->next = NULL;
                now->next = p;
                now = p;
      }
      now->next = head;
      now = head,prev = head;
      while((n--)>1){
                for(int i=1;i<m;i++){
                        prev = now;
                        now = now->next;
                }
                printf("%d ",now->date);
                prev->next = now->next;
                free(now);
                now = prev->next;
      }
      printf("%d",now->date);
      free(now);
      return 0;
}

沃斯戈-莎比 发表于 2022-12-3 18:04:00

为什么我看不到回复啊{:5_100:}

傻眼貓咪 发表于 2022-12-3 18:10:08

#include <stdio.h>
#include <stdlib.h>

typedef struct node { // <------------------------------------------------ 注意这里
    int date;
    struct node* next; // <------------------------------------------------ 注意这里
}node; // <------------------------------------------------ 注意这里

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    node* head, * p, * now, * prev;
    head = (node*)malloc(sizeof(node));
    head->date = 1; head->next = NULL;
    now = head;
    for (int i = 2; i <= n; i++) {
      p = (node*)malloc(sizeof(node));
      p->date = i; p->next = NULL;
      now->next = p;
      now = p;
    }
    now->next = head;
    now = head, prev = head;
    while ((n--) > 1) {
      for (int i = 1; i < m; i++) {
            prev = now;
            now = now->next;
      }
      printf("%d ", now->date);
      prev->next = now->next;
      free(now);
      now = prev->date;
    }
    printf("%d", now->date);
    free(now);
    return 0;
}

咦一哈呦呦 发表于 2022-12-3 18:26:14

本帖最后由 咦一哈呦呦 于 2022-12-3 18:28 编辑

傻眼貓咪 发表于 2022-12-3 18:10


那个 是我用的DEVc++的问题吗还是和之前的问题一样,就一样的错误{:5_104:}   啊呀忘记换号了{:5_97:}

沃斯戈-莎比 发表于 2022-12-3 18:56:10

傻眼貓咪 发表于 2022-12-3 18:10


我会了我打错了一个东西我是莎比吗   now = prev->next 怎么会是date呢

柿子饼同学 发表于 2022-12-3 19:50:10

这啥书啊

沃斯戈-莎比 发表于 2022-12-3 20:44:41

柿子饼同学 发表于 2022-12-3 19:50
这啥书啊

算法竞赛罗勇军、郭卫斌

柿子饼同学 发表于 2022-12-3 20:46:15

沃斯戈-莎比 发表于 2022-12-3 20:44
算法竞赛罗勇军、郭卫斌

楼主也是搞竞赛嘛{:10_254:}

沃斯戈-莎比 发表于 2022-12-3 22:14:23

柿子饼同学 发表于 2022-12-3 20:46
楼主也是搞竞赛嘛

{:5_107:}

zhangjinxuan 发表于 2022-12-4 11:00:35

沃斯戈-莎比 发表于 2022-12-3 18:04
为什么我看不到回复啊

现在好了,再看看?

zhangjinxuan 发表于 2022-12-4 11:01:13

沃斯戈-莎比 发表于 2022-12-3 22:14


你也打竞赛{:10_254:},加个好友呗{:10_254:}

zhangjinxuan 发表于 2022-12-4 12:45:32

沃斯戈-莎比 发表于 2022-12-3 18:04
为什么我看不到回复啊

可以设置最佳了吗{:10_254:}
页: [1]
查看完整版本: 约瑟夫问题