| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 871 人关注过本帖
标题:数据结构中数组转置的问题
只看楼主 加入收藏
x1471898145
Rank: 1
等 级:新手上路
帖 子:5
专家分:6
注 册:2018-5-27
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
数据结构中数组转置的问题
编译可以通过但没有输出,不知道错在哪,好难受。。。。
#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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:20 
if (aa[maxn][maxn]) 应该是 if (aa[i][j]) 吧?

整个代码……
sort_triple完全看不懂
下标竟然从1开始,这个没办法忍了
2018-09-19 08:38
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:0 
程序代码:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

template<typename T> class  SparseMaxtrix
{
public:
    SparseMaxtrix() : rownum_(), colnum_(), nodes_()
    {
    }
    template<size_t ROW, size_t COL> SparseMaxtrix( const T (&arr)[ROW][COL] ) : rownum_(ROW), colnum_(COL), nodes_()
    {
        for( size_t r=0; r!=ROW; ++r )
            for( size_t c=0; c!=COL; ++c )
                if( arr[r][c] != T() )
                    nodes_.push_back( Node_{r,c,arr[r][c]} );
    }
    SparseMaxtrix( const SparseMaxtrix& m ) = default;
    SparseMaxtrix( SparseMaxtrix&& m ) : rownum_(), colnum_(), nodes_()
    {
        std::swap( rownum_, m.rownum_ );
        std::swap( colnum_, m.colnum_ );
        nodes_.swap( m.nodes_ );
    }
    SparseMaxtrix& operator=( const SparseMaxtrix& m ) = default;
    SparseMaxtrix& operator=( SparseMaxtrix&& m )
    {
        std::swap( rownum_, m.rownum_ );
        std::swap( colnum_, m.colnum_ );
        nodes_.swap( m.nodes_ );
        return *this;
    }
    SparseMaxtrix Transpose() const
    {
        SparseMaxtrix result;
        result.rownum_ = colnum_;
        result.colnum_ = rownum_;
        result.nodes_.reserve( nodes_.size() );
        for( typename std::vector<Node_>::const_iterator itor=nodes_.begin(); itor!=nodes_.end(); ++itor )
            result.nodes_.push_back( Node_{itor->col,itor->row,itor->value} );
        std::sort( result.nodes_.begin(), result.nodes_.end(), [](const Node_& a, const Node_& b){return a.row!=b.row ? a.row<b.row : a.col<b.col;} );
        return result;
    }

private:
    size_t rownum_, colnum_;
    struct Node_ {
        size_t row, col;
        T value;
    };
    std::vector<Node_> nodes_;

    friend std::ostream& operator<<( std::ostream& os, const SparseMaxtrix& m )
    {
        typename std::vector<Node_>::const_iterator itor = m.nodes_.begin();
        for( size_t r=0; r!=m.rownum_; ++r )
        {
            for( size_t c=0; c!=m.colnum_; ++c )
            {
                if( itor!=m.nodes_.end() && r==itor->row && c==itor->col )
                {
                    os << itor->value;
                    ++itor;
                }
                else
                    os << T();

                os << (c+1!=m.colnum_?' ':'\n');
            }
        }
        return os;
    }
};

int main( void )
{
    int arr[5][4] = { 0, 0, 0, 4
                    , 0, 0, 0, 0
                    , 1, 0, 0, 5
                    , 0, 3, 0, 0
                    , 0, 0, 2, 0 };

    SparseMaxtrix<int> a = arr;
    SparseMaxtrix<int> b = a.Transpose();

    cout << "转置前:\n" << a << '\n';
    cout << "转置后:\n" << b << '\n';
}

输出
转置前:
0 0 0 4
0 0 0 0
1 0 0 5
0 3 0 0
0 0 2 0

转置后:
0 0 1 0 0
0 0 0 3 0
0 0 0 0 2
4 0 5 0 0

2018-09-19 14:06
快速回复:数据结构中数组转置的问题
数据加载中...
 
   



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

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