Pagini recente » Cod sursa (job #2980497) | Cod sursa (job #1405392) | Cod sursa (job #1959301) | Cod sursa (job #426901) | Cod sursa (job #2257408)
#include <fstream>
using namespace std;
ifstream fin("tablete.in");
ofstream fout("tablete.out");
int n,m,k,v[1006][1006],i,j, aux;
int main()
{
fin>>n>>m;
if(n%2==0&&m%2==0)for(i=1;i<=n;i++)for(j=1;j<=n;j++) {v[i][j]=n*(i-1)+j;}
if(n%2==0&&m%2!=0)
{
for(i=1;i<=n;i++)
v[i][1]=i;
k=n*n-n+1;
for(i=1;i<=n;i++)
{
v[i][n]=k;
k++;
}
k=n+1;
for(i=1;i<=n;i++)
for(j=2;j<=n-1;j++)
{
v[i][j]=k;
k++;
}
}
if(n%2!=0 && m%2==0)
{
for(i=1;i<=n;i++)
v[i][1]=i;
for(i=2;i<=n;i++)
v[n][i]=n+i-1;
k=2*n;
for(i=1;i<=n-1;i++)
for(j=2;j<=n;j++)
{
v[i][j]=k;
k++;
}
}
if(n%2!=0&&m%2!=0)
{
for(i=1;i<=n;i++)
v[i][1]=i;
for(i=2;i<=n-1;i++)
v[n][i]=n+i-1;
v[n][n]=n*n;
k=n+n-1;
for(i=1;i<=n-1;i++)
for(j=2;j<=n;j++)
{
v[i][j]=k;
k++;
}
aux = v[n][m];
v[n][m] = v[n - 1][1];
v[n - 1][1] = aux;
for(i = m; i>1; i--)
{
aux = v[n][i];
v[n][i] = v[n][i - 1];
v[n][i - 1] = aux;
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
fout<<v[i][j]<<" ";
fout<< endl;
}
return 0;
}