Cod sursa(job #2029829)

Utilizator AlexandruLuchianov1Alex Luchianov AlexandruLuchianov1 Data 30 septembrie 2017 15:16:18
Problema Tablete Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.54 kb
#include <iostream>
#include <fstream>

using namespace std;
ifstream in ("tablete.in");
ofstream out ("tablete.out");
int const nmax = 1000;
int v[1 + nmax][1 + nmax];

int main()
{
  int n , k;
  in>>n>>k;
  int nextval = 1;
  int lim = n * n;
  if(n % 2 == 0 && k % 2 == 0){
    for(int i = 1 ; i <= n;i++){
      for(int j = 1 ; j <= n ;j++){
        v[i][j] = nextval;
        nextval++;
      }
    }
  } else if(n % 2 == 0 && k % 2 == 1){
    for(int i = 1 ; i <= n ;i++){
      v[i][1] = nextval;
      nextval++;
    }
    for(int i = 1 ; i <= n ;i++){
      v[i][n] = lim - i + 1;
    }
    for(int i = 1 ; i <= n ;i++){
      for(int j = 2 ; j < n ;j++){
        v[i][j] = nextval;
        nextval++;
      }
    }
  } else if(n % 2 == 1 && k % 2 == 0){
    for(int i = 1 ; i <= n ;i++){
      v[i][1] = nextval;
      nextval++;
    }
    for(int i = 1 ; i <= n ;i++){
      for(int j = 2 ; j <= n ;j++){
        v[i][j] = nextval;
        nextval++;
      }
    }
  } else if(n % 2 == 1 && k % 2 == 1){
    for(int i = 1 ; i < n ;i++){
      v[i][1] = nextval;
      nextval++;
    }
    for(int j = 2 ; j < n ; j++){
      v[1][j] = nextval;
      nextval++;
    }
    v[n][1] = nextval;
    nextval++;
    v[1][n] = lim;
    for(int i = 2 ; i <= n ;i++){
      for(int j = 2 ; j <= n ;j++){
        v[i][j] = nextval;
        nextval++;
      }
    }
  }
  for(int i = 1 ; i <= n ;i++){
    for(int j = 1 ; j <= n ;j++){
      out<<v[i][j]<<" ";
    }
    out<<'\n';
  }
  return 0;
}