PTA链表操作

一、题目描述

  

 

 二、解题思路

  用mp记录前面出现过的数字,如果后面的数字的因子出现过,则这个数不加入答案。最后把答案正着打一遍,倒着打一遍就行了。

三、代码实现

 1 #include "bits/stdc++.h"
 2 #define PII pair<int,int>
 3 #define rep(i,z,n) for(int i = z;i <= n; i++)
 4 #define per(i,n,z) for(int i = n;i >= z; i--)
 5 #define ll long long
 6 #define db double
 7 #define vi vector<int>
 8 #define debug(x) cerr << "!!!" << x << endl;
 9 using namespace std;
10 //从某个串中把某个子串替换成另一个子串
11 string& replace_all(string& src, const string& old_value, const string& new_value) {
12     // 每次重新定位起始位置,防止上轮替换后的字符串形成新的old_value
13     for (string::size_type pos(0); pos != string::npos; pos += new_value.length()) {
14         if ((pos = src.find(old_value, pos)) != string::npos) {
15             src.replace(pos, old_value.length(), new_value);
16         }
17         else break;
18     }
19     return src;
20 }
21 inline ll read()
22 {
23     ll s,r;
24     r = 1;
25     s = 0;
26     char ch = getchar();
27     while(ch < '0' || ch > '9'){
28         if(ch == '-')
29             r = -1;
30         ch = getchar();
31     }
32     while(ch >= '0' && ch <= '9'){
33         s = (s << 1) + (s << 3) + (ch ^ 48);
34         ch = getchar();
35     }
36     return s * r;
37 }
38 inline void write(ll x)
39 {
40     if(x < 0) putchar('-'),x = -x;
41     if(x > 9) write(x / 10);
42     putchar(x % 10 + '0');
43 }
44 int a[100010];
45 int main()
46 {
47     int t;
48     cin >> t;
49     while(t--){
50         int n;
51         n = read();
52         map <int,bool> mp;
53         vector <int> ans;
54         int cnt = 0;
55         rep(i,1,n){
56             int d;
57             d = read();
58             bool flag = false;
59             rep(j,1,d / 2){
60                 if(d % j == 0 && (mp[j] || mp[d / j])){
61                     flag = true;
62                     break;
63                 }
64             }
65             if(flag)
66                 continue;
67             else{
68                 mp[d] = true;
69                 ans.push_back(d);
70                 cnt++;
71             }
72         }
73         cout << ans.size() << endl;
74         rep(i,0,cnt - 1){
75             if(i != cnt - 1)
76                 cout << ans[i] << ' ';
77             else
78                 cout << ans[i];
79         }
80         cout << endl;
81         cnt = cnt - 1;
82         while(cnt >= 0){
83             if(cnt > 0)
84                 cout << ans[cnt] << ' ';
85             else
86                 cout << ans[cnt];
87             cnt--;
88         }
89         cout << endl;
90     }
91     return 0;
92 }
posted @ 2022-03-15 15:36  scannerkk  阅读(70)  评论(0)    收藏  举报