标题:数据结构中数组转置的问题
取消只看楼主
x1471898145
Rank: 1
等 级:新手上路
帖 子:5
专家分:6
注 册:2018-5-27
结帖率:0
已结贴  问题点数:20 回复次数:0 
数据结构中数组转置的问题
编译可以通过但没有输出,不知道错在哪,好难受。。。。
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int maxn = 5;

typedef struct {
    int i, j;
    int val;
}Triple;

typedef struct {
    Triple data[maxn + 1];
    int row, col, sum;
}TSMatrix;

void sort_triple(TSMatrix M, TSMatrix &T)
{
    T.row = M.col;  
    T.col = M.row;
    T.sum = M.sum;
    int num[maxn + 1];
    int cpot[maxn + 1];
    if (T.sum)
    {
        for (int col = 1; col <= M.col; col++)    num[col] = 0;
        for (int i = 1; i <= M.sum; i++)   num[M.data[i].j]++;
        cpot[1] = 1;
        for (int i = 2; i <= M.col; i++)   cpot[i] = cpot[i - 1] + num[i - 1];
        for (int i = 1; i <= M.sum; i++)
        {
            int col = M.data[i].j;
            int tt = cpot[col];
            T.data[tt].i = M.data[i].j;
            T.data[tt].j = M.data[i].i;
            T.data[tt].val = M.data[i].val;
            cpot[col]++;
        }
    }
}

int main()
{
    TSMatrix a;
    int k = 0;
    int aa[maxn][maxn] = {
    {0,0,0,4,0},
    {0,0,0,0,0},
    {1,0,0,0,0},
    {0,3,0,0,0},
    {0,0,2,0,0}
    };
    for (int i = 0; i < maxn; i++)
    {
        for (int j = 0; j < maxn; j++)
        {
            if (aa[maxn][maxn])
            {
                k++;
                a.data[k].i = i+1;
                a.data[k].j = j+1;
                a.data[k].val = aa[i][j];
            }
        }
    }
    a.row = maxn;
    a.col = maxn;
    a.sum = k;
    TSMatrix b;
    sort_triple(a, b);
    int bb[maxn][maxn] = { 0 };
    for (int p = 1; p <= b.sum; p++)
    {
        int x = b.data[p].i-1;
        int y = b.data[p].j-1;
        int value= b.data[p].val;
        bb[x][y] = value;
    }
    puts("转置前:");
    for (int i = 0; i < maxn; i++)
    {
        for (int j = 0; j < maxn; j++)
        {
            printf("%d ", aa[i][j]);
        }
        puts("");
    }
    puts("转置后:");
    for (int i = 0; i < maxn; i++)
    {
        for (int j = 0; j < maxn; j++)
        {
            printf("%d ", bb[i][j]);
        }
        puts("");
    }
    system("pause");
    return 0;


}
搜索更多相关主题的帖子: int data sum for i++ 
2018-09-18 17:29



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-489868-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.488914 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved