1 solutions

  • 0
    @ 2025-11-5 15:43:29

    C :

    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXSIZE 12500//矩阵的最大值
    #define MAXRC 100//行或列的最大值
    
    //定义三元组矩阵存储元素类型
    typedef struct
    {
    	int x, y;//x为行号,y为列号
    	int e;//e为数值
    }Triple;
    
    void zhuanzhi()
    {
    	Triple san[100];
    	Triple san1[100];
    	int tu, nu, mu;//tu代表行数,nu代表列数,mu代表非零元个数
    
    	scanf("%d%d%d", &tu, &nu, &mu);
    
    	for (int i = 0; i < mu; i++)
    		scanf("%d%d%d", &san[i].x, &san[i].y, &san[i].e);
    
    	int k = 0;
    	for (int i = 1; i <= nu; i++)
    	{
    		for (int j = 0; j < mu; j++)
    		{
    			if (san[j].y == i)
    			{
    				san1[k].x = san[j].y;
    				san1[k].y = san[j].x;
    				san1[k].e = san[j].e;
    				k++;
    			}
    		}
    		
    	}
    	for (int k = 0; k < mu; k++)
    	{
    		printf("%d %d %d\n", san1[k].x, san1[k].y, san1[k].e);
    	}
    }
    
    int main()
    {
    	int n;
    	scanf("%d", &n);
    	for (int i = 0; i < n;i++)
    	{
    		zhuanzhi();
    		if (i < n - 1)
    			printf("\n");
    	}
    	return 0;
    }
    

    C++ :

    #include<stdio.h>
    #include <algorithm>
    using namespace std; 
    typedef struct node
    {
    	int x,y,wei;
    };
    int cmp(const node& a,const node & b)
    {
    	return (a.x<b.x||(a.x==b.x&&a.y<b.y));
    }
    int main()
    {
    	int t;scanf("%d",&t);
    	int dd=0;
    	while(t--)
    	{
    		if (dd==1) printf("\n");
    		else dd=1;
    		node k[20];
    		int a,b,c;scanf("%d%d%d",&a,&b,&c);
    		for (int i=0;i<c;i++)
    			scanf("%d%d%d",&k[i].y,&k[i].x,&k[i].wei);
    		sort(k,k+c,cmp);
    		for (int i=0;i<c;i++)
    			printf("%d %d %d\n",k[i].x,k[i].y,k[i].wei);
    	}
    	return 0;
    }
    
    • 1

    Information

    ID
    17092
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By