鱼C论坛

 找回密码
 立即注册
查看: 122|回复: 1

为什么我编的约瑟夫问题的结果是一堆乱码

[复制链接]
发表于 2024-3-24 21:02:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<iostream>
using namespace std;
typedef struct LIST{
        int num;
        LIST* next;
        LIST* front;
        int longth;
}List;
void creat(List* head,int n){
        List* head1 = head;
        system("pause");
        int i = 0,m=0;
        for(i=1;i<n;i++){
                List* node = new List;
                node->next = NULL;
                node->num = i+1;
                head1->next = node;
                node->front = head1;
                head1 = head1->next;
        //        cout<<"i = "<<i<<" head = "<<head1->num<<endl;
        }
        head1->next = head;
        head->front = head1;//定义head的front应该在最后
}
void bian(List* head,int n){
        List* head1;
        head1 = head;
        int i=0;
        for(i=0;i<n+2;i++){
                cout<<head1->num<<endl;
                head1 = head1->next;
        }
}
void Delete(List* head1,int m,int n){
        int i=0;
        while(head1->longth!=1){
                if(i == m-1&&m!=1){// 中间
                        delete head1->next;
                        head1->next = head1->next->next;
                        head1->longth--;
                        bian(head1,head1->longth);
                }
                else if(i==m-1&&m==1){// 开头
                        List* h = new List;//is used for replaceing
                        h = head1;
                        h->front = h->next;
                        h = h->next;
                        delete h;
                        head1->longth--;
                        bian(head1,head1->longth);
                }
                else{
                        i++;
                        head1 = head1->next;
                }
                //List* head1 = new List;
                //head1 = head2;
                /*if(i==m-1){
                        head1->front = head1->next;
                        delete head1;
                        head->longth--;
                        cout<<head->longth<<endl;
                        i = 0;
                }
                else{
                        head2 = head2->next;
                        system("pause");
                        i++;
                }*/
                i = i%n;
               
        }
       
}
int main(){
        int i=0,n=0,m=0;
        cin>>n>>m;
        List* head = new List;
        List* head1 = new List;
        head->num = 1;
        head->longth = n;
//        cout<<head->longth<<endl;
        head->next = NULL;
        head->front = NULL;
        head1 = head;
        creat(head,n);
        Delete(head1,m,n);
        cout<<head->num<<endl;
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-3-24 21:03:12 | 显示全部楼层
在你的代码中,有几个问题导致了乱码的输出。

首先,在你的代码中出现了一些不必要的注释,例如`//coutnext;`和`//        coutn>>m;`。这些注释可能会干扰到代码的正常执行,导致输出结果的乱码。

其次,在`creat`函数中,你创建了一个新的指针`head1`,并将它赋值为`head`。然后你使用`head1`进行循环操作,但是在每次循环结束后,并没有将`head1`重新赋值回`head`,导致指针丢失问题。

另外,在`bian`函数中,你对`head1`进行了两次删除操作,这样很容易导致内存错误。也没有必要在每次循环结束后进行删除操作,应该放在循环结束后再进行删除。

最后,在主函数中,你没有调用`creat`函数来创建链表,并且缺少了对链表头节点的初始化。

下面是修改后的代码,可以正确输出约瑟夫问题的结果:
  1. #include <iostream>
  2. using namespace std;

  3. typedef struct LIST {
  4.     int num;
  5.     LIST* next;
  6.     LIST* front;
  7.     int longth;
  8. }List;

  9. void creat(List* head, int n);
  10. void bian(List* head, int m);

  11. int main() {
  12.     int n, m;
  13.     cout << "请输入总人数:";
  14.     cin >> n;
  15.     cout << "请输入报数的间隔:";
  16.     cin >> m;
  17.    
  18.     List* head = new List;
  19.     head->num = 1;
  20.     head->longth = n;
  21.     head->next = NULL;
  22.     head->front = NULL;
  23.    
  24.     creat(head, n);
  25.     bian(head, m);
  26.    
  27.     return 0;
  28. }

  29. void creat(List* head, int n) {
  30.     List* head1 = head;
  31.     for (int i = 1; i < n; i++) {
  32.         List* node = new List;
  33.         node->num = i + 1;
  34.         head1->next = node;
  35.         node->front = head1;
  36.         head1 = head1->next;
  37.     }
  38.     head1->next = head;
  39.     head->front = head1;
  40. }

  41. void bian(List* head, int m) {
  42.     List* head1 = head;
  43.     while (head->longth > 1) {
  44.         for (int i = 0; i < m - 1; i++) {
  45.             head1 = head1->next;
  46.         }
  47.         cout << "出列的是:" << head1->num << endl;
  48.         head1->front->next = head1->next;
  49.         head1->next->front = head1->front;
  50.         List* temp = head1;
  51.         head1 = head1->next;
  52.         delete temp;
  53.         head->longth--;
  54.     }
  55.     cout << "留下的是:" << head1->num << endl;
  56. }
复制代码


希望我的解答对你有帮助,如果还有其他问题,请继续提问!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 20:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表