sdut原題鏈接
數據結構實驗之數組二:稀疏矩陣 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 5 1 2 14 1 5 -5 2 2 -7 3 1 36 3 4 28 36
Example Output OK
Hint
Author xam
以下為accepted代碼
#include <stdio.h>#include <stdlib.h>#define MAXN 1004struct node{ int r; int l; int date;};struct node1{ int mu; int nu; int tu; struct node ans[MAXN];};int main(){ int i, x, flag; struct node1 *p; p = (struct node1 *)malloc(sizeof(struct node1)); while(scanf("%d %d %d", &p->mu, &p->nu, &p->tu) != EOF) { flag = 0; for(i = 0; i < p->tu; i++) { scanf("%d %d %d", &p->ans[i].r, &p->ans[i].l, &p->ans[i].date); } scanf("%d", &x); for(i = 0; i < p->tu; i++) { if(p->ans[i].date == x) { flag = 1; break; } } if(flag) printf("OK/n"); else printf("ERROR/n"); } return 0;}/***************************************************User name: jk160630Result: AcceptedTake time: 0msTake Memory: 108KBSubmit time: 2017-02-03 19:53:01****************************************************/新聞熱點
疑難解答