注册 登录
编程论坛 C++教室

麻烦大几帮忙看一下

徐宗保 发布于 2013-09-26 22:49, 563 次点击
编写一个函数,使给定的一个N维数组(N×N)行列互换(N>3)。


#include<iostream>
using namespace std;
int show(int a[5][5])
{
    int i,j;
       for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
           cout<<a[i][j]<<"  ";
        }
        cout<<endl;
    }
    return 0;
}
int exchange(int a[5][5])
{
    int i,j,temp;
       for(i=0;i<5;i++)
        for(j=0;j<=i;j++)
        {
            temp=a[i][j];
            a[i][j]=a[j][i];
            a[j][i]=temp;
        }
    return 0;
}
void main()
{
    int a[5][5]={{1,1,1,1,1},{2,2,2,2,2},{3,3,3,3,3},{4,4,4,4,4},{5,5,5,5,5}};
    int play(int),exchenge(int);
    play(a[5][5]);
    exchange(a[5][5]);
    play(a[5][5]);
}
7 回复
#2
pauljames2013-09-27 07:04
就是矩阵转置了,用双重循环,记住关系A[i][j]=A[j][i]
#3
rjsp2013-09-27 08:34
你的代码是抄来的吧?否则不会能写出这样的代码,却在main中连最基本的错误都看不出。

程序代码:
#include <iostream>
#include <algorithm>

template<typename T, size_t N>
void PrintSquare( std::ostream& os, const T (&a)[N][N] )
{
    for( size_t i=0; i!=N*N; ++i )
        os << a[i/N][i%N] << ( (i+1)%N==0 ? "\n" : " " );
}

template<typename T, size_t N>
void ExchgSquare( T (&a)[N][N] )
{
    for( size_t i=0; i!=N*N; ++i )
        if( i/N > i%N )
            std::swap( a[i/N][i%N], a[i%N][i/N] );
}

using namespace std;

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

    PrintSquare( cout, a );
    ExchgSquare( a );
    PrintSquare( cout, a );

    return 0;
}



[ 本帖最后由 rjsp 于 2013-9-27 11:17 编辑 ]
#4
默默潜水Y2013-09-27 10:48
我是新手,楼上的太高端了,楼主看得懂不- -。
程序代码:
#include<iostream>

using namespace std;

void show(int a[][5])
{
    int i,j;
       for(i=0;i<5;i++){
           for(j=0;j<5;j++){
                cout << a[i][j] << "  ";
           }
            cout << endl;
       }
       cout << endl;
}

void exchange(int a[][5])
{
    int i,j,temp;

    for(i=0;i<5;i++){
        for(j=i;j<5;j++){
            temp = a[i][j];
            a[i][j] = a[j][i];
            a[j][i] = temp;
        }
    }
    cout << endl;
    show(a);
}

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

    show(a);

    exchange(a);

    system("pause");

    return 0;
}
这样可以不?
#5
3037709572013-09-27 14:10
以下是引用rjsp在2013-9-27 08:34:55的发言:

你的代码是抄来的吧?否则不会能写出这样的代码,却在main中连最基本的错误都看不出。

#include  
#include  
 
template
void PrintSquare( std::ostream& os, const T (&a)[N][N] )
{
    for( size_t i=0; i!=N*N; ++i )
        os << a << ( (i+1)%N==0 ? "\n" : " " );
}
 
template
void ExchgSquare( T (&a)[N][N] )
{
    for( size_t i=0; i!=N*N; ++i )
        if( i/N > i%N )
            std::swap( a, a );
}
 
using namespace std;
 
int main()
{
    int a[5][5] = { {1,1,1,1,1}, {2,2,2,2,2}, {3,3,3,3,3}, {4,4,4,4,4}, {5,5,5,5,5} };
 
    PrintSquare( cout, a );
    ExchgSquare( a );
    PrintSquare( cout, a );
 
    return 0;
}
这个代码让你越改越乱了,就这样就行了:
#include <iostream>
#include <algorithm>
template<typename T, size_t N>
void PrintSquare( std::ostream& os, const T (&a)[N][N] )
{
    for( size_t i=0; i!=N*N; ++i )
        os << a[i/N][i%N] << ( (i+1)%N==0 ? "\n" : " " );
}
template<typename T, size_t N>
void ExchgSquare( T (&a)[N][N] )
{
    for( size_t i=0; i!=N*N; ++i )
        if( i/N > i%N )
            std::swap( a[i/N][i%N], a[i%N][i/N] );
}
using namespace std;
int main()
{
    int a[5][5] = {
        {1,2,3,4,5},
       {6,7,8,9,10},
       {11,12,13,14,15},
       {16,17,18,19,20},
       {21,22,23,24,25}
    };
    PrintSquare( cout,a);
    ExchgSquare( a );
    PrintSquare( cout,a);
    return 0;
}

#6
C_printf2013-09-27 15:04
2个版主为何要整模板? 高端大气上档次? 别人是新手,按别人代码解答就行了。
#7
徐宗保2013-09-27 22:07
回复 4楼 默默潜水Y
看不懂
#8
yushuibin22013-09-27 22:32
希望在后面加注释
便于理解
1