数据结构实验之数组二:稀疏矩阵
Time Limit: 5MS Memory Limit: 1000KB
Problem Description
对于一个n*n的稀疏矩阵M(1 <= n <= 1000),采用三元组顺序表存储表示,查找从键盘输入的某个非零数据是否在稀疏矩阵中,如果存在则输出OK,不存在则输出ERROR。稀疏矩阵示例图如下:
Input
连续输入多组数据,每组数据的第一行是三个整数mu, nu, tu(tu<=50),分别表示稀疏矩阵的行数、列数和矩阵中非零元素的个数,数据之间用空格间隔,随后tu行输入稀疏矩阵的非零元素所在的行、列值和非零元素的值,每组数据的最后一行输入要查询的数据k。
Output
输出查询结果,查找成功输出
OK
,找不到输出
ERROR。
Example Input
3 5 51 2 141 5 -52 2 -73 1 363 4 2836
Example Output
OK
参考代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
struct node
{
int a,b,c;
} str[1010],t;
int main()
{
int n,m,q,i,a;
while(~scanf("%d%d%d",&n,&m,&q))
{
int flag=0;
for( i=0; i<q; i++)
{
cin>>str[i].a>>str[i].b>>str[i].c;
}
cin>>a;
for (i=0;i<q;i++)
{
if (str[i].c==a)
{
flag=1;
cout<<"OK"<<endl;
break;
}
}
if (flag==0) cout<<"ERROR"<<endl;
}
return 0;
}