Pagini recente » Cod sursa (job #261552) | Cod sursa (job #2009446) | Cod sursa (job #1341566) | Cod sursa (job #131049) | Cod sursa (job #284808)
Cod sursa(job #284808)
#include<stdio.h>
FILE *in=fopen("tablete.in","rt");
FILE *out=fopen("tablete.out","wt");
long int nr,a[1001][1001],last;
int n,k;
void afisare()
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
fprintf(out,"%ld ",a[i][j]);
nr++;
}
fprintf(out,"\n");
}
}
int main()
{
int i,j;
fscanf(in,"%d",&n);
fscanf(in,"%d",&k);
nr=1;
if(n%2==0 && k%2==0)
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
a[i][j]=nr;
nr++;
}
}
else
if(n%2==0 && k%2==1)
{
nr=2;
for(i=1;i<=n-1;i++)
for(j=1;j<=n;j++)
{
a[i][j]=nr;
nr++;
}
a[n-1][n]=n*n;
a[n][1]=1;
nr--;
for(j=2;j<=n;j++) a[n][j]=nr++;
}
else
if(n%2==1 && k%2==0)
{
nr=1;last=1;
for(i=1;i<=n;i++)
{
if(i%2==1)
{
a[i][1]=last; nr++;
for(j=2;j<=n;j++)
{
a[i][j]=nr;
nr++;
}
}
else
{
last=nr+1;
nr++;
for(j=1;j<=n;j++)
{
a[i][j]=nr;
nr++;
}
}
}
}
afisare();
return 0;
}