Description
对于一个n*n的稀疏矩阵M(1 <= n <= 1000),采用三元组顺序表存储表示,查找从键盘输入的某个非零数据是否在稀疏矩阵中,如果存在则输出OK,不存在则输出ERROR。稀疏矩阵示例图如下:
Input
连续输入多组数据,每组数据的第一行是三个整数mu, nu, tu(tu<=50),分别表示稀疏矩阵的行数、列数和矩阵中非零元素的个数,数据之间用空格间隔,随后tu行输入稀疏矩阵的非零元素所在的行、列值和非零元素的值,每组数据的最后一行输入要查询的数据k。
Output
输出查询结果,查找成功输出OK,找不到输出ERROR。
Sample
Input
3 5 5
1 2 14
1 5 -5
2 2 -7
3 1 36
3 4 28
36
Output
OK
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stack>
#include<queue>
using namespace std;
int main()
{
int n,m,x;
int a,b,c,k;
int ans[10010],cnt;
while(~scanf("%d %d %d",&n,&m,&x))
{
cnt = 0;
for(int i=0; i<x; i++)
{
scanf("%d %d %d",&a,&b,&c);
ans[cnt++]=c;
}
scanf("%d",&k);
int flag = 0;
for(int i=0; i<cnt; i++)
{
if(k == ans[i])
{
printf("OK\n");
flag=1;
break;
}
}
if(flag == 0)
printf("ERROR\n");
}
return 0;
}