要求输入一组数据,如B a & 2,输出有这一行数据组成的对称矩阵
B a & 2
a B 2 &
& 2 B a
2 & a B
,该问题也可用于求循环赛事的赛程表:
循环赛事问题可以参见这篇文章:http://blog.youkuaiyun.com/liufeng_king/article/details/8488421
代码如下:n表示元素的个数
#include<iostream>
#include<math.h>
using namespace std;
//#define DEBUG
const int MAXSize = 130;
char Table[MAXSize][MAXSize];
int main()
{
#ifdef DEBUG
freopen("Input.txt", "r", stdin);
setbuf(stdout, NULL);
#endif
int tc, T;
int n, k;
cin>>T;
for (tc = 1; tc <= T; tc++)
{
cin >> n;
k = (int)sqrt(n);
for (int i = 1; i <= n; i++)
cin >> Table[1][i]; //Input the first row of symmetric Matrix
int m = 1;
int tag = n;
for (int s = 1; s <= k; s++)
{
tag /= 2;
for (int t = 1; t <= tag; t++)
{
for (int i = m + 1; i <= 2 * m; i++)
{
for (int j = m + 1; j <= 2 * m; j++)
{
Table[i][j + (t - 1)*m * 2] = Table[i - m][j + (t - 1)*m * 2 - m];//右下角等于左上角的值
Table[i][j + (t - 1)*m * 2 - m] = Table[i - m][j + (t - 1)*m * 2];//左下角等于右上角的值
}
}
}
m *= 2;
}
cout << "Case #" << tc << endl;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cout << Table[i][j] << ' ';
}
cout << endl;
}
}
return 0;
}