当前位置:网站首页>牛客 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());
// }
}
}
边栏推荐
猜你喜欢
随机推荐
2.索引及调优篇【mysql高级】
ASP.NET Core generates continuous Guid
npm安装时卡在sill idealTree buildDeps,npm安装速度慢,npm安装卡在一个地方不动
Internet banking stolen?This article tells you how to use online banking safely
做事软件开发-法的重要性所在以及合理结论的认识
How Redis handles concurrent access
Unity中实现点选RenderTexture中的3D模型
长得很怪的箱图
MySQL multi-table union query
11 pinia使用
【TypeScript】深入学习TypeScript类型操作
Replication Latency Case (1) - Eventual Consistency
[MySQL] Mysql paradigm and the role of foreign keys
The use of border controls
MySQL常用语句整理
Visualize GraphQL schemas with GraphiQL
贪吃蛇项目(简单)
Applicable Scenarios of Multi-Master Replication (1) - Multi-IDC
Tencent Cloud Deployment----DevOps
Insert into data table to insert data
![[TypeScript] In-depth study of TypeScript type operations](/img/d9/ee240ccba72e8d3114ee5c52ed0c8f.png)






