Cons. Marx 2021-08-25 13:54 采纳率: 92.3%
浏览 41
已结题

字符串为什么循环时出错了,?

img
简单调试了一下,发现问题出现在67-70行的那个循环里,请大家看看哪里出问题了


 
#include <iostream>
using namespace std;
void swap(struct List &a,struct  List &b);
#include <string>
#include<cstring> 
//            00100 6 4        
//00100 1 12309    00000 4 33218
//12309 2 33218    33218 3 12309
//33218 3 00000    12309 2 00100
//00000 4 99999    00100 1 99999
//99999 5 68237    99999 5 68237
//68237 6 -1    68237 6 -1
struct List{
    string add;
    int value;
    string next;
};
int main()
{
    string headadd;
    int n,node;
    cin>>headadd>>n>>node;
    List a[n];
    for(int i=0;i<n;i++)
    {
        cin>>a[i].add>>a[i].value>>a[i].next;
    }
    for(int j=1;j<n;j++)
    {
        for(int k=j;k<n;k++)
        {
            if(a[k].add == headadd)
            {
                swap(a[k],a[0]);
            }
        }
    }
    for(int j=0;j<n;j++)
    {
        for(int k=j+1;k<n;k++)
        {
            if(a[k].add == a[j].next)
            {
                swap(a[k],a[j+1]);
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        cout<<a[i].value<<endl;
    }
    cout<<"here:"<<endl;
    //node=4,n=6
    for(int cnt=0;cnt<n/node;)//控制有几次循环 
    {
        for(int jud=cnt;jud<node/2;jud++)//控制每次循环的开始位置以及循环次数 
        {
            swap(a[jud],a[node-jud-1]);
        }
        cnt += node;
    }
    for(int i=0;i<n;i++)//遍历链表并将链表的下一个地址改变 
    {
        a[i].next = a[i+1].add;
    }
    a[n-1].next = '-1';    
    for(int i=0;i<n;i++)
    {
        cout<<a[i].add<<" "<<a[i].value<<" "<<a[i].next<<endl;
    }
}
void swap(struct List &a,struct  List &b)
{
    struct List L;
    L.add = a.add;
    L.value = a.value;
    L.next = a.next;
    a.add = b.add;
    a.value = b.value;
    a.next =b.next;
    b.add = L.add;
    b.value = L.value; 
    b.next = L.next;    
}
  • 写回答

2条回答 默认 最新

  • qzjhjxj 2021-08-25 14:05
    关注

    第67--70行改为:

    for (int i = 0; i < n-1; i++)//遍历链表并将链表的下一个地址改变 
        {
            a[i].next = a[i + 1].add;
        }
        
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月2日
  • 已采纳回答 8月25日
  • 创建了问题 8月25日

悬赏问题

  • ¥30 matlab解优化问题代码
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline