回型方阵
1
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
for(int i=1;i<=2*n+1;i++)
if(i==1)cout<<n;
else cout<<" "<<n;
cout<<endl<<endl;
for(int j=1;j<n;j++)
{
cout<<n;
for(int h=1;h<=j;h++)
cout<<" "<<(n-h);
for(int i=1;i<=2*n-(2*j+1);i++)
cout<<" "<<(n-j);
for(int h=j;h>0;h--)
cout<<" "<<(n-h);
cout<<" "<<n<<endl<<endl;
}
for(int j=n;j>=1;j--)
{
cout<<n;
for(int h=1;h<=j;h++)
cout<<" "<<(n-h);
for(int i=1;i<=2*n-(2*j);i++)
cout<<" "<<(n-j);
for(int h=j-1;h>0;h--)
cout<<" "<<(n-h);
cout<<" "<<n<<endl<<endl;
}
for(int i=1;i<=2*n+1;i++)
if(i==1)cout<<n;
else cout<<" "<<n;
return 0;
}
2
#include <iostream>
#include <cmath>
using namespace std;
int a[100][100];
int main(int argc, char const *argv[]) {
for (int n; cin >> n;) {
int size = 2 * n + 1;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
int dist = max(abs(i - n), abs(j - n));
a[i][j] = dist;
}
}
for (int j = 0; j < size; j++) {
for (int i = 0; i < size; i++) {
cout << " " << a[i][j];
}
cout << endl;
}
}
return 0;
}
正反回型方阵
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
for(int i=1;i<=2*n+1;i++)
cout<<n<<" ";
cout<<endl;
for(int j=1;j<n;j++)
{
cout<<n<<" ";
for(int h=1;h<=j;h++)
cout<<(n-h)<<" ";
for(int i=1;i<=2*n-(2*j+1);i++)
cout<<(n-j)<<" ";
for(int h=j;h>0;h--)
cout<<(n-h)<<" ";
cout<<n<<endl;
}
for(int j=n;j>=1;j--)
{
cout<<n<<" ";
for(int h=1;h<=j;h++)
cout<<(n-h)<<" ";
for(int i=1;i<=2*n-(2*j);i++)
cout<<(n-j)<<" ";
for(int h=j-1;h>0;h--)
cout<<(n-h)<<" ";
cout<<n<<endl;
}
for(int i=1;i<=2*n+1;i++)
cout<<n<<" ";
cout<<endl;
for(int i=1;i<=6*n+1;i++)
cout<<"-";
cout<<endl;
int i,j,k;
for (i=1;i<=n*2-1;++i)
{
if(i<=n)
k=i;
else
k=2*n-i;
for (j=1;j<=k; ++j)
cout <<j<<" ";
for(j=1;j<=(n-k)*2;++j)
cout << k << " ";
for (j=k-1;j>=1;--j)
cout <<j<<" ";
cout<<endl;
}
return 0;
}
这是我对回型方阵的一些归纳,如涉及版权问题请及时与本人联系。