欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > 每日OJ题_牛客_集合_排序_C++_Java

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

2024/10/24 17:10:59 来源:https://blog.csdn.net/GRrtx/article/details/143049310  浏览:    关键词:每日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 + " ");}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com