-
-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create Question.md * Add solution of Task 1 cpp problem
- Loading branch information
1 parent
1116bb1
commit 801bf25
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
Task 1/2D Array - Alternating rectangles of O and X/Question.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# 2D Array - Alternating rectangles of O and X | ||
Write a code which inputs two numbers m and n and creates a matrix of size m x n (m rows and n columns) in which every elements is either X or 0. The Xs and 0s must be filled alternatively, the matrix should have outermost rectangle of Xs, then a rectangle of 0s, then a rectangle of Xs, and so on.. | ||
|
||
### Input Format | ||
You need to tell that how many rows and columns your matrix need to have and these values should be separated by space. | ||
### Constraints | ||
1 <= n,m <= 1000 | ||
### Output Format | ||
Print the resultant matrix to the stdout. | ||
### [Link of the question](https://www.techgig.com/practice/question/2d-array-alternating-rectangles-of-o-and-x/MXExbVBCMm83Y0ROcG42RXg2V1hhMy83Skwxdjh0ZWZjZEpqOE1uWEs1QzZINW9DR0dPYXpXR1pvVzJ5N3BpSA==/1) | ||
## Sample TestCase 1 | ||
#### input | ||
``` | ||
6 7 | ||
``` | ||
#### output | ||
``` | ||
X X X X X X X | ||
X 0 0 0 0 0 X | ||
X 0 X X X 0 X | ||
X 0 X X X 0 X | ||
X 0 0 0 0 0 X | ||
X X X X X X X | ||
``` | ||
**Time Limit(X)** : 1.00 sec(s) for each input. | ||
|
||
**Memory Limit** : 512 MB |
78 changes: 78 additions & 0 deletions
78
Task 1/2D Array - Alternating rectangles of O and X/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include<iostream> | ||
using namespace std; | ||
int main() | ||
{ | ||
int m, n; | ||
cin>>m>>n; | ||
if(n>=1 && m <= 1000 && m>=1 && n<=1000) | ||
{ | ||
int i, j, k=0, p=0, checkr=0, checkc=0; | ||
char mat[m][n]; | ||
|
||
for (i=1; i<=m; i++) | ||
{ | ||
p=0; | ||
checkc=0; | ||
for(j=1; j<=n; j++) | ||
{ | ||
if((checkr==0 && j>=k+1 && j<=n-k) || (checkc==0 && i>=p+1 && i<m-p)) | ||
{ | ||
mat[i][j]='X'; | ||
cout<<mat[i][j]; | ||
} | ||
else | ||
{ | ||
mat[i][j]='0'; | ||
cout<<mat[i][j]; | ||
} | ||
if(n%2==0) | ||
{ | ||
if(j<n/2) | ||
p++; | ||
else if(j==n/2) | ||
p=p; | ||
else | ||
p--; | ||
} | ||
else if(n%2==1) | ||
{ | ||
if(j<(n+1)/2) | ||
p++; | ||
else | ||
p--; | ||
} | ||
if(n%2==0 && j==n/2) | ||
checkc=checkc; | ||
else | ||
checkc=1-checkc; | ||
if(j<=n-1) | ||
cout<<" "; | ||
} | ||
if(m%2==0) | ||
{ | ||
if(i<m/2) | ||
k++; | ||
else if(i==m/2) | ||
k=k; | ||
else | ||
k--; | ||
} | ||
else if(m%2==1) | ||
{ | ||
if(i<(m+1)/2 ) | ||
k++; | ||
else | ||
k--; | ||
} | ||
|
||
if(m%2==0 && i==m/2) | ||
checkr=checkr; | ||
else | ||
checkr=1-checkr; | ||
if(i<=m-1) | ||
cout<<"\n"; | ||
} | ||
|
||
return 0; | ||
} | ||
} |