这是我的解决方案:
#include <iostream>
#include <iomanip>
#include <vector>
#include <tuple>
int getDigitCount( int num )
{
num = abs( num );
return ( num < 10 ? 1 :
( num < 100 ? 2 :
( num < 1000 ? 3 :
( num < 10'000 ? 4 :
( num < 100'000 ? 5 :
( num < 1'000'000 ? 6 :
( num < 10'000'000 ? 7 :
( num < 100'000'000 ? 8 :
( num < 1'000'000'000 ? 9 :
10 )))))))));
}
void printMatrix( const std::vector<int>& array,
const std::tuple< const size_t, const size_t >& dimensions, const int maxDigitCount )
{
const auto& [ rowCount, colCount ] { dimensions };
std::cout << '\n' << " \\ Column ";
for ( size_t colNumber = 0; colNumber < colCount; ++colNumber )
{
std::cout << std::left << std::setw( maxDigitCount + 2 ) << std::setfill(' ') << colNumber;
}
std::cout << '\n' << "Row \\" << '\n' << '\n';
for ( size_t idx = 0; idx < rowCount * colCount; ++idx )
{
if ( ( idx ) % ( colCount ) == 0 )
{
std::cout << " " << ( idx ) / ( colCount ) << " ";
}
std::cout << std::left << std::setw( maxDigitCount + 2 ) << std::setfill(' ') << array[ idx ];
if ( ( idx + 1 ) % ( colCount ) == 0 )
{
std::cout << '\n' << '\n';
}
}
}
// get row count and column count from the user
auto getDimensions( )
{
constexpr int MIN_ALLOWED_ROW_COUNT { 1 };
constexpr int MIN_ALLOWED_COL_COUNT { 1 };
constexpr int MAX_ALLOWED_ROW_COUNT { 50 };
constexpr int MAX_ALLOWED_COL_COUNT { 50 };
int inputRowCount { };
int inputColCount { };
do
{
std::cout << "Enter row count: ";
std::cin >> inputRowCount;
std::cout << "Enter column count: ";
std::cin >> inputColCount;
} while ( inputRowCount < MIN_ALLOWED_ROW_COUNT || inputColCount < MIN_ALLOWED_COL_COUNT ||
inputRowCount > MAX_ALLOWED_ROW_COUNT || inputColCount > MAX_ALLOWED_COL_COUNT );
return std::make_tuple<int, int>( std::move( inputRowCount ), std::move( inputColCount ) );
}
// get user input ( user's matrix ) and populate the array
void getArrayElements( std::vector<int>& array, int& maxDigitCount )
{
for ( std::vector<int>::iterator it = array.begin( ); it != array.end( ); ++it )
{
std::cin >> *it;
if ( getDigitCount( *it ) > maxDigitCount )
{
maxDigitCount = getDigitCount( *it );
}
}
}
// bubble sort the array
void bubbleSortArray( std::vector<int>& array, const size_t& elementCount )
{
for ( size_t iterCount = 0; iterCount < elementCount - 1; ++iterCount )
{
for ( size_t idx = 0; idx < ( elementCount - 1 - iterCount ); ++idx )
{
if ( array[ idx ] > array[ idx + 1 ] )
{
int temp = array[ idx ];
array[ idx ] = array[ idx + 1 ];
array[ idx + 1 ] = temp;
}
}
}
}
// transpose 1D array and store in 1D transposedArray
void transposeMatrix( std::vector<int>& array, std::vector<int>& transposedArray,
const std::tuple< const size_t, const size_t >& dimensions )
{
size_t row { };
size_t col { };
const auto& [ rowCount, colCount ] { dimensions };
for ( size_t idx = 0; idx < rowCount * colCount; ++idx )
{
if ( col == colCount )
{
++row;
col = 0;
}
size_t newIdx { row + ( col * rowCount ) };
transposedArray[ newIdx ] = array[ idx ];
++col;
}
}
void launch( )
{
const auto [ inputRowCount, inputColCount ] = getDimensions( );
const size_t rowCount { static_cast<unsigned int>( inputRowCount ) };
const size_t colCount { static_cast<unsigned int>( inputColCount ) };
const std::tuple< const size_t, const size_t > dimensions( std::move( rowCount ),
std::move( colCount ) );
const size_t elementCount { rowCount * colCount };
std::vector<int> array( elementCount ); // the 1D array for storing the user's matrix
int maxDigitCount { };
getArrayElements( array, maxDigitCount );
std::cout << "\nOriginal 2D array:\n"; // print out the array
printMatrix( array, dimensions, maxDigitCount );
bubbleSortArray( array, elementCount );
std::cout << "\nSorted 2D array:\n"; // print out the sorted array
printMatrix( array, dimensions, maxDigitCount );
std::vector<int> transposedArray( elementCount );
transposeMatrix( array, transposedArray, dimensions );
std::cout << "\nTransposed sorted 2D array:\n"; // print out the transposed sorted array
printMatrix( transposedArray, std::make_tuple< const size_t, const size_t >
( std::move( colCount ), std::move( rowCount ) ), maxDigitCount );
}
int main()
{
launch( );
return 0;
}
还有一个示例输入/输出:
Enter row count: 5
Enter column count: 4
1 6 11 16
2 7 12 17
3 8 13 18
4 9 14 19
5 10 15 20
Original 2D array:
\ Column 0 1 2 3
Row \
0 1 6 11 16
1 2 7 12 17
2 3 8 13 18
3 4 9 14 19
4 5 10 15 20
Sorted 2D array:
\ Column 0 1 2 3
Row \
0 1 2 3 4
1 5 6 7 8
2 9 10 11 12
3 13 14 15 16
4 17 18 19 20
Transposed sorted 2D array:
\ Column 0 1 2 3 4
Row \
0 1 5 9 13 17
1 2 6 10 14 18
2 3 7 11 15 19
3 4 8 12 16 20
转置的排序二维数组是您想要获得的输出(基于我对您问题的理解)。
此外,顶部还有一个函数getDigitCount,用于计算一个数字在所有用户提供的数字之间的最大位数以进行字符串格式化(使用std::setw)。