Cod sursa(job #2316841)

Utilizator cameliapatileaPatilea Catalina Camelia cameliapatilea Data 12 ianuarie 2019 14:58:52
Problema Tablete Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.91 kb
#include <iostream>
#include <fstream>
using namespace std;

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

int  matrice[1001][1001];

int n, k, nr;
int main() {


    int i, j;
    f >> n >> k;

    for(i = 1;i <= n; i++)
    {
        for(j = 1; j <= n; j++)
        {
            nr++;
            matrice[i][j] = nr;
        }
    }


    if(n % 2 == 0)
    {
        if(k % 2 == 1)
        {
            for(i = 1;i <= n; i= i + 2)
            {
                nr = matrice[i][1];
                for(j = 1; j < n; j++)
                    matrice[i][j] = matrice[i][j + 1];

                matrice[i][n] = matrice[i + 1][n];

                for(j = n; j > 1;j--)
                    matrice[i+1][j]=matrice[i+1][j-1];

                matrice[i + 1][1] = nr;
            }
        }
    }

    if(n % 2 == 1)
    {
        if(k % 2 == 0)
        {

            for(i = 1;i < n; i = i + 2)
            {
                nr = matrice[i][n];
                matrice[i][n] = matrice[i + 1][n];
                for(j = n;j > 1 ; j--)
                    matrice[i + 1][j] = matrice[i + 1][j - 1];

                matrice[i + 1][1] = nr;
            }
        }

        if(k % 2 == 1)
        {

            for(i = 2; i + 3 <= n;i =i + 2)
            {
                nr = matrice[i - 1][1];

                for(j = 1; j < n; j++)
                    matrice[i - 1][j] = matrice[i - 1][j + 1];

                matrice[i - 1][j] = matrice[i][1];
                matrice[i][1] = nr;

            }
            nr = matrice[n - 2][1];

            for(j = 1;j < n; j++)
                matrice[n - 2][j] = matrice[n - 2][j + 1];


            matrice[n - 2][n] = matrice[n][n];

            for(j = n;j > 1; j--)
                matrice[n][j] = matrice[n][j - 1];

            matrice[n][1] = nr;
        }

    }
    for(i = 1;  i<= n; i++)
    {
        for(j = 1;  j<= n; j++)
            g<<matrice[i][j] << " ";
        g << "\n";
    }

}