当前位置: 首页 > article >正文

每日OJ题_牛客_集合_排序_C++_Java

目录

牛客_集合_排序

题目解析

C++代码

Java代码


牛客_集合_排序

集合_牛客题霸_牛客网 (nowcoder.com)


题目解析

笔试题可直接用set排序,面试可询问是否要手写排序函数,如果要手写排序,推荐写快排。

C++代码

#include <iostream>
#include <set>
using namespace std;

int main()
{
    int n = 0, m = 0;
    cin >> n >> m;
    set<int> s;
    int tmp = 0;
    while(n--)
    {
        cin >> tmp;
        s.insert(tmp);
    }
    while(m--)
    {
        cin >> tmp;
        s.insert(tmp);
    }
    auto it = s.begin();
    while(it != s.end())
    {
        cout << *it << " ";
        ++it;
    }
    return 0;
}

Java代码

import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main
{
    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt();
        TreeSet<Integer> set = new TreeSet<>();
        int x;
        while(n-- != 0)
        {
            x = in.nextInt();
            set.add(x);
        }
        while(m-- != 0)
        {
            x = in.nextInt();
            set.add(x);
        }
        for(int a : set)
        {
            System.out.print(a + " ");
        }
    }
}

http://www.kler.cn/news/360129.html

相关文章:

  • stable diffusion WEBUI Brief summary
  • 家政小程序搭建,数字化市场发展下的意义
  • RK3588开发笔记-麦克风阵列多pdm通道合并成一个声卡
  • 智能新势力:防爆挂轨巡检机器人助力化工安全
  • 外包干了2个月,技术明显退步
  • Java八股整合(Kafka+RocketMQ+K8S)
  • Redis入门:在Java程序中高效使用Redis
  • 4.流程控制及函数
  • 代码随想录算法训练营第二十二天 | 回溯理论基础 77.组合 216.组合总和Ⅲ 17.电话号码的字母组合
  • nginx精讲
  • CORS预检请求配置流程图 srpingboot和uniapp
  • 如何获得淘宝/天猫淘宝商品详情 API接口
  • jmeter响应断言放进csv文件遇到的问题
  • MySQL索引、事物与存储引擎
  • SpringCloud学习记录|day6
  • 【进阶OpenCV】 (14)-- 人脸识别 -- LBPH 算法
  • 稀土阻燃协效剂的应用
  • 如何新建一个React Native的项目
  • lua的使用
  • git配置以及如何删除git