链接:https://ac.nowcoder.com/acm/contest/59935/B 来源:牛客网 It's New Year's Day, and it was a tradition for ancient Chinese to write couplets. Sometimes one person would give the first line, and another person would need to come up with the second line based on the sentence structure and meaning, which requires considerable literary skills. Now given a first line of length nn, with each character represented by a number for convenience, you need to write a second line that meets the following requirements: 1. Each number in the second line must be a positive integer. 2. Each number in the second line must not appear in the first line. 3. The ii-th number in the first line corresponds to the ii-th number in the second line. You can specify the correspondence relationship arbitrarily, but the relationship must be consistent and unique. For example, if you specify that the number 3 in the first line corresponds to the number 1 in the second line, then another non-3 number in the first line cannot correspond to the number 1, and all 3s in the first line must correspond to 1.用c++给我把这道题做出来
时间: 2024-01-04 14:02:18 浏览: 141
好的,这道题目可以使用一个桶来记录第一行中出现的数字,然后从1到n遍历,如果该数字没有在桶中出现过,则将其加入到第二行中,并在桶中标记该数字已经出现过。具体实现可以参考以下代码:
```c++
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> first(n);
vector<bool> used(n + 1, false);
for (int i = 0; i < n; i++) {
cin >> first[i];
used[first[i]] = true; // 记录第一行中出现的数字
}
vector<int> second(n);
for (int i = 1; i <= n; i++) {
if (!used[i]) {
second[first[i - 1] - 1] = i; // 根据题目要求,第i个数字对应第i个位置
}
}
for (int i = 0; i < n; i++) {
cout << second[i] << " ";
}
cout << endl;
return 0;
}
```
希望能帮到你!
相关问题
https://ac.nowcoder.com/acm/contest/67743/L
很抱歉,我无法访问互联网上的链接。但是,我可以为您提供一些关于Nowcoder的一般信息。
Nowcoder是一个面向程序员的在线学习和竞赛平台,提供各种编程题目和算法竞赛。链接中的https://ac.nowcoder.com/acm/contest/67743/L可能是一个Nowcoder的竞赛链接,其中的"/acm/contest/67743/L"可能是竞赛的具体页面或题目链接。
如果您对Nowcoder平台或者竞赛有任何具体问题,我会尽力回答。请提出您的问题。
阅读全文