题意 :
思路 :
语法 :
#include
using namespace std;
const int N = 1e5 + 10;
int n;
int h1, h2, ne[N];
char e[N];
bool st[N];
int main()
{
scanf("%d%d%d", &h1, &h2, &n);
int address, next;
char data;
for (int i = 0; i < n; i ++ )
{
scanf("%d %c %d", &address, &data, &next);
e[address] = data, ne[address] = next;
}
for (int i = h1; ~i; i = ne[i])
st[i] = true;
for (int i = h2; ~i; i = ne[i])
if (st[i])
{
printf("%05d\n", i);
return 0;
}
puts("-1");
}
题意 :
思路 :
%05d
了,它这样用到的地址全部是输入给你的地址,不会由于int和string的转换失去前导零语法 :
#include
#include
#include
#include
using namespace std;
struct Node
{
string address;
int key;
string next;
bool operator< (const Node& t) const
{
return key < t.key;
}
};
int main()
{
int n;
char head[10];
scanf("%d %s", &n, head);
unordered_map<string, Node> map;
char address[10], next[10];
int key;
while (n -- )
{
scanf("%s %d %s", address, &key, next);
map[address] = {address, key, next};
}
vector<Node> nodes;
for (string i = head; i != "-1"; i = map[i].next) nodes.push_back(map[i]);
printf("%d ", (int)nodes.size());
if (nodes.empty()) puts("-1");
else
{
sort(nodes.begin(), nodes.end());
printf("%s\n", nodes[0].address.c_str());
for (int i = 0; i < nodes.size(); i ++ )
{
if (i + 1 == nodes.size())
printf("%s %d -1", nodes[i].address.c_str(), nodes[i].key);
else
printf("%s %d %s\n", nodes[i].address.c_str(), nodes[i].key, nodes[i + 1].address.c_str());
}
}
}
题意 :
思路 :
#include
#include
#include
using namespace std;
const int N = 1e5 + 10;
int n, head, m;
int e[N], ne[N];
int main()
{
scanf("%d%d%d", &head, &n, &m);
int address, data, next;
for (int i = 0; i < n; i ++ )
{
scanf("%d%d%d", &address, &data, &next);
e[address] = data, ne[address] = next;
}
vector<int> q;
for (int i = head; ~i; i = ne[i]) q.push_back(i);
for (int i = 0; i + m - 1 < q.size(); i += m )
{
reverse(q.begin() + i, q.begin() + i + m);
}
for (int i = 0; i < q.size(); i ++ )
{
printf("%05d %d ", q[i], e[q[i]]);
if (i != q.size() - 1) printf("%05d\n", q[i + 1]);
else printf("-1\n");
}
}
题意 :
#include
#include
#include
using namespace std;
const int N = 100010;
int n;
int h, e[N], ne[N];
bool st[N];
int main()
{
scanf("%d%d", &h, &n);
for (int i = 0; i < n; i ++ )
{
int address, key, next;
scanf("%d%d%d", &address, &key, &next);
e[address] = key, ne[address] = next;
}
vector<int> a, b;
for (int i = h; i != -1; i = ne[i])
{
int v = abs(e[i]);
if (st[v]) b.push_back(i);
else
{
st[v] = true;
a.push_back(i);
}
}
for (int i = 0; i < a.size(); i ++ )
{
printf("%05d %d ", a[i], e[a[i]]);
if (i + 1 == a.size()) puts("-1");
else printf("%05d\n", a[i + 1]);
}
for (int i = 0; i < b.size(); i ++ )
{
printf("%05d %d ", b[i], e[b[i]]);
if (i + 1 == b.size()) puts("-1");
else printf("%05d\n", b[i + 1]);
}
return 0;
}
题意 :
语法 :
a.insert(a.end(), b.begin(), b.end());
,在vector a容器的末尾加入vector b容器#include
#include
#include
using namespace std;
const int N = 100010;
int n, k;
int h, e[N], ne[N];
int main()
{
scanf("%d%d%d", &h, &n, &k);
for (int i = 0; i < n; i ++ )
{
int address, key, next;
scanf("%d%d%d", &address, &key, &next);
e[address] = key, ne[address] = next;
}
vector<int> a, b, c;
for (int i = h; i != -1; i = ne[i])
{
int v = e[i];
if (v < 0) a.push_back(i);
else if (v <= k) b.push_back(i);
else c.push_back(i);
}
a.insert(a.end(), b.begin(), b.end());
a.insert(a.end(), c.begin(), c.end());
for (int i = 0; i < a.size(); i ++ )
{
printf("%05d %d ", a[i], e[a[i]]);
if (i + 1 == a.size()) puts("-1");
else printf("%05d\n", a[i + 1]);
}
return 0;
}