当前位置:网站首页>牛客 HJ3 明明的随机数
牛客 HJ3 明明的随机数
2022-07-31 15:58:00 【顧棟】
描述
明明生成了NN个1到500之间的随机整数。请你删去其中重复的数字,即相同的数字只保留一个,把其余相同的数去掉,然后再把这些数从小到大排序,按照排好的顺序输出。
数据范围: 1 ≤ n ≤ 1000 {1 \le n \le 1000} 1≤n≤1000 ,输入的数字大小满足 1 ≤ v a l ≤ 500 {1 \le val \le 500} 1≤val≤500
输入描述:
第一行先输入随机整数的个数 N 。 接下来的 N 行每行输入一个整数,代表明明生成的随机数。 具体格式可以参考下面的"示例"。
输出描述:
输出多行,表示输入数据处理后的结果
示例1
输入:
3
2
2
1
输出:
1
2
说明:
输入解释:
第一个数字是3,也即这个小样例的N=3,说明用计算机生成了3个1到500之间的随机整数,接下来每行一个随机数字,共3行,也即这3个随机数字为:
2
2
1
所以样例的输出为:
1
2
java实现
第一种解法:直接通过TreeSet来实现排序和去重
第二种解法:借助通过数组来实现排序和去重
package nowcoder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.TreeSet;
public class HJ003 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 0-1000是1001个数
boolean[] stu = new boolean[1001];
String nLine = br.readLine();
StringBuilder sb = new StringBuilder();
if (null != nLine) {
int n = Integer.parseInt(nLine);
for (int i = 0; i < n; i++) {
stu[Integer.parseInt(br.readLine())] = true;
}
for (int i = 0; i < 1001; i++) {
if (stu[i]) {
sb.append(i).append("\n");
}
}
sb.deleteCharAt(sb.length() - 1);
System.out.println(sb);
}
// TreeSet set = new TreeSet();
// String s = br.readLine();
// while (null != (s = br.readLine())) {
// set.add(Integer.parseInt(s));
// }
// Iterator t = set.iterator();
// while (t.hasNext()) {
// System.out.println(t.next());
// }
}
}
边栏推荐
- Handling write conflicts under multi-master replication (4) - multi-master replication topology
- Deployment应用生命周期与Pod健康检查
- Graham's Scan method for solving convex hull problems
- 腾讯云部署----DevOps
- Why don't you make a confession during the graduation season?
- 7. Summary of common interview questions
- After Grafana is installed, the web opens and reports an error
- 11 pinia use
- .NET 20th Anniversary Interview - Zhang Shanyou: How .NET technology empowers and changes the world
- Implement anti-shake and throttling functions
猜你喜欢

Getting Started with TextBlock Control Basic Tools Usage, Get Started

Implementing DDD based on ABP

What is the difference between BI software in the domestic market?

radiobutton的使用

【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】

Kubernetes common commands

The use of border controls

WPF项目--控件入门基础用法,必知必会XAML

Tencent Cloud Deployment----DevOps

Implementing distributed locks based on Redis (SETNX), case: Solving oversold orders under high concurrency
随机推荐
Matlab matrix basic operations (definition, operation)
After the form is submitted, the page does not jump [easy to understand]
苹果官网样式调整 结账时产品图片“巨大化”
2020微信小程序反编译教程(小程序反编译源码能用吗)
入职一个月反思
MySQL multi-table union query
C language - function
字符指针赋值[通俗易懂]
长得很怪的箱图
Applicable scenario of multi-master replication (2) - client and collaborative editing that require offline operation
多主复制的适用场景(1)-多IDC
ansible study notes 02
MySQL数据库操作
6-22漏洞利用-postgresql数据库密码破解
Snake Project (Simple)
Unity中实现点选RenderTexture中的3D模型
全新宝马3系上市,安全、舒适一个不落
TextBlock控件入门基础工具使用用法,取上法入门
leetcode303 Weekly Match Replay
Doing things software development - the importance of law and understanding of reasonable conclusions