Cod sursa(job #3335838)

Utilizator andrei0simionAndrei Simion andrei0simion Data 23 ianuarie 2026 17:55:59
Problema Tablete Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

ifstream in("tablete.in");
ofstream out("tablete.out");

int main()
{
    int n;
    in >> n;
    
    int k;
    in >> k;
    
    int nrCrt = 1;
    int mat[n][n];
    for(int y = 0; y < n; y++)
    {
        for(int x = 0; x < k; x++)
        {
            mat[x][y] = nrCrt;
            nrCrt++;
        }
    }
    for(int y = 0; y < n; y++)
    {
        for(int x = k; x < n; x++)
        {
            mat[x][y] = nrCrt;
            nrCrt++;
        }
    }
    
    if(k % 2 == 1)
    {
        int pn = n % 2 == 0 ? n : n - 1;
        for(int y = 0; y < pn; y++)
        {
            if(mat[k - 1][y] % 2 == 0)
                continue;
            
            int tmp = mat[0][y + 1];
            mat[0][y + 1] = mat[k - 1][y];
            mat[k - 1][y] = tmp;
        }
        
        if(n % 2 == 1)
        {
            int tmp = mat[k - 1][n - 1];
            for(int x = k - 1; x < n - 1; x++)
                mat[x][n - 1] = mat[x + 1][n - 1];
            mat[n - 1][n - 1] = mat[k][n - 2];
            
            mat[k][n - 2] = tmp;
        }
    }
    
    for(int y = 0; y < n; y++)
    {
        for(int x = 0; x < n; x++)
            out << mat[x][y] << " ";
        out << "\n";
    }
    
    return 0;
}