数据结构实验之数组二:稀疏矩阵

来源:互联网 发布:大数据涂子沛概括 编辑:程序博客网 时间:2024/06/05 20:22

数据结构实验之数组二:稀疏矩阵

Time Limit: 5MS Memory Limit: 1000KB
Submit Statistic

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

Hint

#include<bits/stdc++.h>using namespace std;struct node{    int a,b,c;}t[1010],T;int main(){    int n,m,p,key;    while(cin>>n>>m>>p)    {        for(int i=0;i<p;i++)        {            cin>>t[i].a>>t[i].b>>t[i].c;        }        for(int i=0;i<p-1;i++)        {            for(int j=0;j<p-i-1;j++)            {                if(t[j].b>=t[j+1].b)                {                    T=t[j];                    t[j]=t[j+1];                    t[j+1]=T;                }            }        }        cin>>key;        int flag=0;        for(int i=0;i<p;i++)        {            if(t[i].a==key||t[i].b==key||t[i].c==key)                flag=1;        }        if(flag)            cout<<"OK"<<endl;        else            cout<<"ERROR"<<endl;    }    return 0;}


0 0
原创粉丝点击