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
思路:输入一个整数n,下面有n行n列,a[i][j]表示第i个车和第j个车进行碰撞时哪一个会被撞翻,0表示都不会被撞翻,1表示第i个车会被撞翻,i车是坏的;2表示第j个车会被撞翻,j车是坏的;3表示两个都会撞翻,表示两个都是坏的。用for循环看看输入的数是对应的哪一种情况。
#include<stdio.h> #include<string.h> #define N 1010 int main() { int n,i,j; int a[N][N],b[N]; while(~scanf("%d",&n)) { int t=n; for(i=0; i<=n-1; i++) { b[i]=i+1; } for(i=0; i<=n-1; i++) { for(j=0; j<=n-1; j++) { scanf("%d",&a[i][j]); } } for(i=0; i<=n-1; i++) { for(j=0; j<=n-1; j++) { if(a[i][j]==1) { if(b[i]!=0) { b[i]=0; t--; continue; } else { continue; } } else if(a[i][j]==2 ) { if(b[j]!=0) { b[j]=0; t--; continue; } else { continue; } } else if(a[i][j]==3) { if(b[i]==0&&b[j]!=0 ) { b[j]=0; t=t-1; continue; } else if(b[i]!=0 && b[j]!=0) { b[i]=0; b[j]=0; t=t-2; continue; } else { continue; } } } } if(t<=0) { printf("0\n"); } else { printf("%d\n",t); for(i=0; i<=n-1; i++) { if(b[i]!=0 && i==n-1) { printf("%d\n",b[i]); } else if(b[i]!=0 && i!=n-1) { if(t==1) { printf("%d\n",b[i]); } else printf("%d ",b[i]); } } } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。