there is voice recorder which store student voice as per roll number on which it was heard earlier. When the attendance process is
complete, it will provide a list which would consist of the number of distinct voices.
teacher presents the list to you and asks for the roll numbers of students who were not present in class.
i am trying to find out roll number of absent students in increasing order.
我写了这个案例,但是一些测试案例失败了。我不确定老师会在清单中提供什么值。
there are only two inputs :
1. no of student
2. result from voice recorder
任何人都可以告诉我们这里缺少什么
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
List<Integer> ll = new ArrayList<>();
List<Integer> input = new ArrayList<>();
for (int i = 1; i <= n; i++) {
ll.add(i);
}
String lines = br.readLine();
String[] strs = lines.trim().split("\\s+");
for (int i = 0; i < strs.length; i++) {
input.add(Integer.parseInt(strs[i]));
}
for (int i = 0; i < ll.size(); i++) {
if (input.contains(ll.get(i))) {
continue;
}
else {
System.out.print(ll.get(i));
}
if (i != ll.size() - 1) {
System.out.print(" ");
}
}
}
答案 0 :(得分:1)
这很好用,每个测试用例都通过了。
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int stdCount = Integer.parseInt(br.readLine());
String rollNumbers = br.readLine();
TreeSet<Integer> presentStudents = new TreeSet<Integer>();
String[] rollNoArr = rollNumbers.split(" ");
for(String s : rollNoArr) {
presentStudents.add(Integer.valueOf(s.trim()));
}
for(int i = 1; i <= stdCount; i++) {
if(!presentStudents.contains(i)) {
System.out.print(i);
if(i < stdCount) System.out.print(" ");
}
}
}
答案 1 :(得分:0)
假设我正确地阅读了问题,则首先输入班上的学生总数。每个学生都有一个分配的编号,然后您提供一个由空格分隔的编号列表(对应于班级中的学生)。您的代码缺少一些方括号,这使事情搞砸了。
此外,对于这种特定情况,您的逻辑:
if (true) {
continue;
}
else{
// Do something
}
可以通过以下操作使之简单得多
if(!true){
// Do something
}
这是我修改过的最终代码:
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
List<Integer> ll = new ArrayList<>();
List<Integer> input = new ArrayList<>();
for (int i = 1; i <= n; i++) {
ll.add(i);
}
String lines = br.readLine();
String[] strs = lines.trim().split("\\s+");
for (int i = 0; i < strs.length; i++) {
input.add(Integer.parseInt(strs[i]));
}
for (int i = 0; i < ll.size(); i++) {
if (!input.contains(ll.get(i))) {
System.out.print(ll.get(i));
if (i != ll.size() - 1) {
System.out.print(" ");
}
}
}
}
输入:
6
1 3 4
结果为:
2 5 6
答案 2 :(得分:0)
////使用Java8的工作代码
{
public static void main(String[] args) throws IOException {
List<Integer> totalRolls;
String[] inputRollsWithProxyStudentsRoll;
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
totalRolls = IntStream
.rangeClosed(1, Integer.parseInt(br.readLine()))
.boxed()
.collect(Collectors.toList());
inputRollsWithProxyStudentsRoll = br.readLine().trim().split("\\s+");
}
List<Integer> rollsWithProxyStudentsRoll = Arrays
.stream(inputRollsWithProxyStudentsRoll).map(Integer::parseInt)
.collect(Collectors.toList());
IntStream
.range(0, totalRolls.size())
.filter(i -> !rollsWithProxyStudentsRoll.contains(totalRolls.get(i)))
.forEach(i -> {
System.out.print(totalRolls.get(i));
if (i != totalRolls.size() - 1) System.out.print(" ");
});
}
}
答案 3 :(得分:-2)
import java.io.*
import java.util.*;
class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 0; j < n; j++) {
if (i == array[j]) {
break;
}
if (j == n - 1 && i != array[j]) {
System.out.println(i + " ");
}
}
}
}
}