当前位置:网站首页>牛客 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());
// }
}
}
边栏推荐
- Implementing DDD based on ABP
- Doing things software development - the importance of law and understanding of reasonable conclusions
- Kubernetes principle analysis and practical application manual, too complete
- org.apache.jasperException(could not initialize class org)
- Getting Started with TextBlock Control Basic Tools Usage, Get Started
- ansible学习笔记02
- Graham‘s Scan法求解凸包问题
- Delete table data or clear table
- The principle of hough transform detection of straight lines (opencv hough straight line detection)
- 二分查找的细节坑
猜你喜欢
[TypeScript] In-depth study of TypeScript type operations
i.MX6ULL驱动开发 | 33 - NXP原厂网络设备驱动浅读(LAN8720 PHY)
2022年整理LeetCode最新刷题攻略分享(附中文详细题解)
How Redis handles concurrent access
The use of border controls
研发过程中的文档管理与工具
Premiere Pro 2022 for (pr 2022)v22.5.0
Why don't you make a confession during the graduation season?
Grafana安装后web打开报错
Implementing distributed locks based on Redis (SETNX), case: Solving oversold orders under high concurrency
随机推荐
Delete table data or clear table
ansible学习笔记02
WPF项目--控件入门基础用法,必知必会XAML
mongo enters error
Graham's Scan method for solving convex hull problems
C language "the third is" upgrade (mode selection + AI chess)
研发过程中的文档管理与工具
LeetCode_733_图像渲染
The principle of hough transform detection of straight lines (opencv hough straight line detection)
tensorflow2.0 cnn(layerwise)
Deployment application life cycle and Pod health check
Single-cell sequencing workflow (single-cell RNA sequencing)
Codeforces Round #796 (Div. 2) (A-D)
Summary of the implementation method of string inversion "recommended collection"
更新数据表update
二分查找的细节坑
字符指针赋值[通俗易懂]
Internet banking stolen?This article tells you how to use online banking safely
Vb how to connect mysql_vb how to connect to the database collection "advice"
复杂高维医学数据挖掘与疾病风险分类研究