lundi 7 novembre 2016

How to get the inner side number pattern

So this problem going like this :

Input : 3 
Output :  1 1 1
          1 2 1
          1 1 1
Input 7 :  
          1 1 1 1 1 1 1
          1 2 2 2 2 2 1 
          1 2 3 3 3 2 1 
          1 2 3 4 3 2 1 
          1 2 3 3 3 3 1 
          1 2 2 2 2 2 1 
          1 1 1 1 1 1 1 

This is what I've done :

 #include<iostream>
 using namespace std;

int main() {
   int n;
   int indeks = 1;
   cin >> n;
   for (int i = 1; i <= n; i++)
   {
       for (int j = 1; j <= n; j++) {
           if (j != 1 && i != 1 && j != n && i != n){
               if (i == n/2+1 && j == n/2+1)
                   cout << indeks + i - 1;
               else
                   cout << indeks + 1;
           }
           else
               cout << indeks;
       }
       cout << "\n";
   }
}

Aucun commentaire:

Enregistrer un commentaire