PAT甲级1074 【Reversing Linked List (25)】

在王道书上看到了逆转的好方法,要注意Reverse函数数组下标的确定。本菜因为

混用cout和printf

导致一直WA。。。留下了没有技术的泪水,虽然代码写得非常搓,还是记录下自己的错误。

#include
#include
using namespace std;
const int maxn = 1e6+10;
struct Node{
    int addr;
    int data;
    int next; 
}node[maxn],mynode[maxn];
void Reverse(int st, int ed, int k){
    int mid = (st+ed)/2 - st;
    for(int i = 0; i<=mid; i++){
        Node tmp;
        tmp.data = mynode[i+st].data;
        tmp.addr = mynode[i+st].addr;
        mynode[st+i].data = mynode[ed-i].data;
        mynode[st+i].addr = mynode[ed-i].addr;
        mynode[ed-i].data = tmp.data;
        mynode[ed-i].addr = tmp.addr;
    }

}
int main(){
//  freopen("in.txt", "r", stdin);
    int st, n, k, addr, data, next, p, cnt = 0;
    cin >> st >> n >> k;
    for(int i = 0; i> addr >> data >> next;
        node[addr].data = data;
        node[addr].next = next;
    }
    p = st;
    while(p != -1){
        mynode[cnt].data = node[p].data;
        mynode[cnt].addr = p;
        p = node[p].next;
        cnt++;
    } 
    for(int i = 0; iif(i+k-1 >= cnt) break;
        Reverse(i, i+k-1, k);
    }
    for(int i = 0; iprintf("%05d %d ", mynode[i].addr, mynode[i].data);
        if(i != cnt-1) printf("%05d\n", mynode[i+1].addr);
        else printf("-1\n");
    }
    return 0;
}

你可能感兴趣的:(错误提醒,PAT,链表,反转,C++)