Description
Little Susie, thanks to her older brother, likes to play with cars. Today she decided to set up a tournament between them. The process of a tournament is described in the next paragraph.
There are n toy cars. Each pair collides. The result of a collision can be one of the following: no car turned over, one car turned over, both cars turned over. A car is good if it turned over in no collision. The results of the collisions are determined by an n × n matrix А: there is a number on the intersection of the і-th row and j-th column that describes the result of the collision of the і-th and the j-th car:
- 1: if this pair of cars never collided. - 1 occurs only on the main diagonal of the matrix.
0: if no car turned over during the collision.
1: if only the i-th car turned over during the collision.
2: if only the j-th car turned over during the collision.
3: if both cars turned over during the collision.
Susie wants to find all the good cars. She quickly determined which cars are good. Can you cope with the task?
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of cars.
Each of the next n lines contains n space-separated integers that determine matrix A.
It is guaranteed that on the main diagonal there are - 1, and - 1 doesn't appear anywhere else in the matrix.
It is guaranteed that the input is correct, that is,
if Aij = 1, then Aji = 2,
if Aij = 3, then Aji = 3,
and if Aij = 0, then Aji = 0.
Output
Print the number of good cars and in the next line print their space-separated indices in the increasing order.
Sample Input
> Input
3
-1 0 0
0 -1 1
0 2 -1
> Output
2
1 3
> Input
4
-1 3 3 3
3 -1 3 3
3 3 -1 3
3 3 3 -1
> Output
0
java解答
package EveryDayPracticeB;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class BTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[][] array = new int[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
array[i][j] = scanner.nextInt();
}
}
List<Integer> list = getRes(array, n);
//去重
List<Integer> res = list.stream().distinct().collect(Collectors.toList());
int num = res.size();
System.out.println(num);
for(int i = 0; i < num; i++){
System.out.print(res.get(i) + " ");
}
}
/**
* 设置两个list,分别是“好车组”和“坏车组”
* “好车组”:在这一次的碰撞中未被撞倒 --> win
* “坏车组”:在这一次的碰撞中被撞倒 --> lose
* win 对 lose 取差集就是从来没有被撞倒的车 --> 合格车辆!!
* 【注意】:此时的合格车辆是带有重复的,需要在用到的时候进行去重
* @param array
* @param n
* @return
*/
public static List<Integer> getRes(int[][] array, int n){
ArrayList<Integer> win = new ArrayList<>();
ArrayList<Integer> lose = new ArrayList<>();
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(array[i][j] == 3){
lose.add(i + 1);
lose.add(j + 1);
}else if(array[i][j] == 2){
win.add(i + 1);
lose.add(j + 1);
}else if(array[i][j] == 1){
win.add(j + 1);
lose.add(i + 1);
}else if(array[i][j] == 0){
win.add(j + 1);
win.add(i + 1);
}
}
}
win.removeAll(lose);
return win;
}
}