Keywords: 各种错误总结
Given a singly linked list, you are supposed to rearrange its elements so that all the negative values appear before all of the non-negatives, and all the values in [0, K] appear before all those greater than K. The order of the elements inside each class must not be changed. For example, given the list being 18→7→-4→0→5→-6→10→11→-2 and K being 10, you must output -4→-6→-2→7→0→5→10→18→11.
Input Specification:
Output Specification:
For each case, output in order (from beginning to the end of the list) the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:
00100 9 10
23333 10 27777
00000 0 99999
00100 18 12309
68237 -6 23333
33218 -4 00000
48652 -2 -1
99999 5 68237
27777 11 48652
12309 7 33218
Sample Output:
33218 -4 68237
68237 -6 48652
48652 -2 12309
12309 7 00000
00000 0 99999
99999 5 23333
23333 10 00100
00100 18 27777
27777 11 -1
Note1
- 题意,静态链表id排序,先按原序输出负值,再按原序输出比k小(或等于)的值,再输出剩余的值
- 思路 开三个vector记录三组内容,坑在与对这三个的链接,每个vector都可能不存在,因此输出有九种组合方式
- 一直卡在段错误: 原因q.size() 返回负值时由于unsigned,-1成了最大正数,坑了好久的段错误。
- 最后一个测试卡在一个条件的判断上,有一个分句不小心return了,测试方法:while(1)带入
最后的代码,改到面目全非。。。。。。。
#include<iostream>
#include<algorithm>
#include<vector>
#include<fstream>
#include<queue>
using namespace std;
const int maxn = 1e7;
struct node{
int id;
int address;
int next;
}a[maxn];
int visited[maxn] = {0};
vector<node> first;
vector<node> q;
vector<node> remain;
int main(){
#ifdef _DEBUG
ifstream cin("data2.txt");
#endif
int num, start, compare_k;
cin >> start >> num >> compare_k ;
fill(visited, visited + num , 0);
for(int i = 0; i < num; i++){
int aa, bb, cc;
cin >> aa >> bb >> cc;
a[aa].address = aa;
a[aa].id = bb;
a[aa].next = cc;
}
int temp = start, flag = 0;
while(temp != -1){
if(a[temp].id < 0 && visited[temp] == 0){
visited[temp] = 1;
first.push_back(a[temp]);
}
if(visited[temp] == 0 && a[temp].id <= compare_k){
visited[temp] = 1;
q.push_back(a[temp]);
}
else if(visited[temp] == 0 && a[temp].id > compare_k){
visited[temp] = 1;
remain.push_back(a[temp]);
}
temp = a[temp].next;
}
if(first.size() > 0){
temp =(int)(first.size() - 1);
for(int i = 0; i < temp; i++){
printf("%05d %d %05d\n", first[i].address, first[i].id, first[i + 1].address);
}
}
if(q.size() > 0){
if(first.size() > 0)
printf("%05d %d %05d\n", first[temp].address, first[temp].id, q[0].address);
temp =(int)(q.size() - 1);
for(int i = 0; i <temp; i++){
printf("%05d %d %05d\n", q[i].address, q[i].id, q[i + 1].address);
}
}
if(remain.size() > 0){
if(q.size() > 0)
printf("%05d %d %05d\n", q[temp].address, q[temp].id, remain[0].address);
else if(first.size() > 0){
//while(1);
printf("%05d %d %05d\n", first[temp].address, first[temp].id, remain[0].address);
//return 0;
}
temp = (int)(remain.size() - 1);
for(int i = 0; i < temp; i++){
printf("%05d %d %05d\n", remain[i].address, remain[i].id, remain[i + 1].address)
printf("%05d %d -1\n", remain[temp].address, remain[temp].id);
}
else{
if(q.size() > 0 && first.size() > 0) printf("%05d %d -1\n", first[temp].address, first[temp].id);
else if(q.size() > 0) printf("%05d %d -1\n", q[temp].address, q[temp].id);
else if(first.size() > 0) printf("%05d %d -1\n", first[temp].address, first[temp].id);
}
#ifdef _DEBUG
cin.close();
#endif
return 0;
}
Note2
其实用一个 vector v1.insert(v1.end(), v2.begin(), v2.end())就解决了 233
#include<iostream>
#include<algorithm>
#include<vector>
#include<fstream>
#include<queue>
using namespace std;
const int maxn = 1e7;
struct node{
int id;
int address;
int next;
}a[maxn];
int visited[maxn] = {0};
vector<node> first;
vector<node> q;
vector<node> remain;
int main(){
#ifdef _DEBUG
ifstream cin("data2.txt");
#endif
int num, start, compare_k;
cin >> start >> num >> compare_k ;
fill(visited, visited + num , 0);
for(int i = 0; i < num; i++){
int aa, bb, cc;
cin >> aa >> bb >> cc;
a[aa].address = aa;
a[aa].id = bb;
a[aa].next = cc;
}
int temp = start, flag = 0;
while(temp != -1){
if(a[temp].id < 0 && visited[temp] == 0){
visited[temp] = 1;
first.push_back(a[temp]);
}
if(visited[temp] == 0 && a[temp].id <= compare_k){
visited[temp] = 1;
q.push_back(a[temp]);
}
else if(visited[temp] == 0 && a[temp].id > compare_k){
visited[temp] = 1;
remain.push_back(a[temp]);
}
temp = a[temp].next;
}
first.insert(first.end(),q.begin(),q.end());
first.insert(first.end(),remain.begin(),remain.end());
temp = first.size() - 1;
if(temp == 0){
printf("%05d %d -1\n", first[temp].address, first[temp].id);
return 0;
}
for(int i = 0; i < temp; i++){
printf("%05d %d %05d\n", first[i].address, first[i].id, first[i + 1].address);
}
printf("%05d %d -1\n", first[temp].address, first[temp].id);
#ifdef _DEBUG
cin.close();
#endif
return 0;
}
错误总结
段错误
- 数组越界, 特别注意 XX.size() - 1
- 递归太深, 深度大小等待总结
段错误表示程序不能正常运行,然而在自己编译器跑过了样例说明主干逻辑没有问题,应考虑分支的逻辑以及大规模的数据
内存超限
一般不会,编译器的内存更容易超2333
浮点错误
主要是除以零和取余零
格式错误
换行,空格
详细介绍链接
https://wenku.baidu.com/view/21bef4f6ff00bed5b8f31d54.html