Pagini recente » Cod sursa (job #2665141) | Cod sursa (job #2794018) | Cod sursa (job #3252585) | Cod sursa (job #3224236) | Cod sursa (job #2494258)
#include <bits/stdc++.h>
using namespace std;
int a[1005][1005],n,k,l,c;
int main()
{
freopen("tablete.in","r",stdin);
freopen("tablete.out","w",stdout);
cin.tie(0);
cin.sync_with_stdio(0);
cin>>n>>k;
for(int i=1; i<=n; ++i)
{
if(l!=0)
{
a[i][1]=l;
l=0;
}
else
{
c++;
a[i][1]=c;
}
for(int j=2; j<k; ++j)
{
c++;
a[i][j]=c;
}
if(c%2)
{
c++;
a[i][k]=c;
}
else
{
c+=2;
a[i][k]=c;
l=c-1;
}
for(int j=k+1; j<=n; ++j)
{
++c;
a[i][j]=c;
}
}
if(l!=0)
{
for(int j=n; j>1; --j)a[n][j]=a[n][j-1];
a[n][1]=a[n-1][n];
a[n-1][n]=l;
if(a[n][1]>a[n][2])swap(a[n][1],a[n][2]);
}
for(int i=1; i<=n; ++i)
{
for(int j=1; j<=n; ++j)
cout<<a[i][j]<<' ';
cout<<'\n';
}
return 0;
}