数据结构实验之数组二:稀疏矩阵
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>
using namespace std;
struct node
{
int row;
int col;
int data;
}mp[1005];
void func(int n,int key)
{
int i,flag=0;
for(i=0;i<n;i++)
{
if(mp[i].data==key)
flag=1;
}
if(flag)
cout<<"OK"<<endl;
else
cout<<"ERROR"<<endl;
}
int main()
{
int mu,nu,tu;
int i,k;
while(cin>>mu>>nu>>tu)
{
for(i=0;i<tu;i++)
cin>>mp[i].row>>mp[i].col>>mp[i].data;
cin>>k;
func(tu,k);
}
return 0;
}