Monday, November 13, 2017

Inner Reduce Pattern Printing

Given a number N, the program must print the pattern as described below.

Input Format:
The first line contains the value of the N which represent the number N.

Boundary Conditions: 2 <= N <= 9

Output Format:
The pattern as described below in the Example Input/Output

Example Input/Output 1:
Input:
4
Output:
4444444
4333334
4322234
4321234
4322234
4333334
4444444

Solution 1:
n=int(input())
a=[[0 for _ in range(2*n-1)]for _ in range(2*n-1)]
for i in range(n,-1,-1):
    y=n-i
    for x in range(y,2*n-1-y):
        a[y][x]=i
        a[x][y]=i
    y=2*n-2-y
    for x in range(n-i,y+1):
        a[y][x]=i
        a[x][y]=i
for i in a:
    print(''.join(str(j) for j in i))

Solution 2:
#include <iostream>
using namespace std;
int main()
{
 int i,x,y,n;
 cin>>n;
 int a[2*n-1][2*n-1];
 for(i=n;i>=0;i--)
 {
     y=n-i;
     for(x=y;x<(2*n-1-y);x++)
      {
          a[y][x]=i;
          a[x][y]=i;
      }
    y=2*n-2-y;
    for(x=n-i;x<=y;x++)
    {
        a[y][x]=i;
        a[x][y]=i;
    }
 }
 for(x=0;x<(2*n-1);x++)
 {
    for(y=0;y<(2*n-1);y++)
     cout<<a[x][y];
    cout<<endl;
 }
}

Solution 3:
#include<iostream>
using namespace std;
 int main()
{
  int n;
  cin>>n;
  for(int i=0;i<2*n-1;i++)
  {
     int k=i<n?i:2*(n-1)-i;
     for(int j=0;j<k;j++)
       cout<<n-j;
     for(int j=0;j<2*(n-k)-1;j++)
       cout<<n-k;
     for(int j=k-1;j>=0;j--)
       cout<<n-j;
     cout<<endl;
  }
  return 0;
}

Share: