6 条题解

  • 4
    @ 2026-2-10 15:30:46
    #include<iostream>
    #include<set>
    using namespace std;
    int main() {
        int n;
        cin>>n;
        set<int> s;
        for(int i=0;i<n;i++) {
            int tmp;
            cin>>tmp;
            s.insert(tmp);
        }
        cout<<s.size()<<endl;
        for(int n : s) {
            cout<<n<<" ";
        }
        return 0;
    }
    
    • 2
      @ 2026-2-10 15:27:02
      #include<bits/stdc++.h>
      using namespace std;
      int main(){
      	ios::sync_with_stdio(false);
      	cin.tie(0);
      	int n;
      	vector<int> a;
      	cin >> n;
      	for(int i = 0; i < n; i++){
      		int m;
      		cin >> m;
      		a.push_back(m);
      	}
      	sort(a.begin(), a.end());
      	a.erase(unique(a.begin(), a.end()), a.end());
      	cout << a.size() << endl;
      	for(int i = 0; i < a.size(); i++){
      		cout << a[i] << " ";
      	}
      	return 0;
      }
      
      • 1
        @ 2026-6-12 22:20:09
        1. 下面的麻烦得要死,看看我的: #include<bits/stdc++.h> using namespace std; int main(){ int N; cin>>N; int pc[105]; for(int i=0;i<N;i++){ cin>>pc[i]; } sort(pc,pc+N); int M=0; int unique_pc[105]; for(int i=0;i<N;i++){ if(i==0||pc[i]!=pc[i-1]){ unique_pc[M]=pc[i]; M++; } } cout<<M<<endl; for(int i=0;i<M;i++){ cout<<unique_pc[i]; if(i<M-1){ cout<<" "; } } return 0; }
        • @ 2026-7-10 10:57:51

          请把代码放代码块,谢谢。。。

      • 0
        @ 2026-2-13 10:11:47
        #include <bits/stdc++.h>
        using namespace std;
        int main() {
            int N;
            cin >> N;
            int a[105];
            for (int i = 0; i < N; i++) {
                cin >> a[i];
            }
         // 1. 去重
            int b[105];
            int m = 0;  // 去重后的长度
            for (int i = 0; i < N; i++) {
                bool repeat = false;
                for (int j = 0; j < m; j++) {
                    if (a[i] == b[j]) {
                        repeat = true;
                        break;
                    }
                }
                if (!repeat) {
                    b[m++] = a[i];
                }
            }
        // 2. 排序(冒泡排序)
            for (int i = 0; i < m - 1; i++) {
                for (int j = 0; j < m - i - 1; j++) {
                    if (b[j] > b[j + 1]) {
                        swap(b[j], b[j + 1]);
                    }
                }
            }
            // 3. 输出
            cout << m << "\n";
            for (int i = 0; i < m; i++) {
                cout << b[i] << " ";
            }
            return 0;
        }
        
        
        
        • 0
          @ 2026-2-13 10:11:18
          #include <bits/stdc++.h>
          using namespace std;
          int main() {
              int N;
              cin >> N;
              vector<int> v(N);
              for (int i = 0; i < N; i++) {
                  cin >> v[i];
              }
              // 1. 排序
              sort(v.begin(), v.end());
              // 2. 去重
              auto it = unique(v.begin(), v.end());
              // 3. 输出结果
              int M = it - v.begin();
              cout << M << "\n";
              for (int i = 0; i < M; i++) {
                  cout << v[i] << " ";
              }
              return 0;
          }
          
          
          
          • -2
            @ 2026-2-10 15:46:45

            // 包含所有标准库的头文件,简化代码 #include<bits/stdc++.h> using namespace std;

            int main(){ // 关闭输入输出同步,提高cin/cout速度 ios::sync_with_stdio(false); // 解除cin与cout的绑定,进一步提高速度 cin.tie(0);

            // 定义变量n表示随机数的个数
            int n;
            // 定义动态数组a存储随机数
            vector<int> a;
            // 读入随机数的个数n
            cin >> n;
            
            // 循环n次,读入每个随机数
            for(int i = 0; i < n; i++){
                // 定义临时变量m存储当前读入的随机数
                int m;
                // 读入一个随机数
                cin >> m;
                // 将随机数添加到数组末尾
                a.push_back(m);
            }
            
            // 对数组进行升序排序,使相同数字相邻
            sort(a.begin(), a.end());
            // 去重操作:unique将重复元素移到末尾,返回去重后的新结尾
            // erase删除从新结尾到原结尾的所有重复元素
            a.erase(unique(a.begin(), a.end()), a.end());
            
            // 输出不重复随机数的个数
            cout << a.size() << endl;
            
            // 循环输出所有不重复且排序后的随机数
            for(int i = 0; i < a.size(); i++){
                // 逐个输出数组元素,用空格分隔
                cout << a[i] << " ";
            }
            
            // 程序正常结束
            return 0;
            

            }

            • 1

            信息

            ID
            75
            时间
            1000ms
            内存
            256MiB
            难度
            2
            标签
            递交数
            36
            已通过
            25
            上传者