-
Notifications
You must be signed in to change notification settings - Fork 10
/
04_pattern_mountain.cpp
146 lines (116 loc) · 3.96 KB
/
04_pattern_mountain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Pattern Mountain
Take N (number of rows), print the following pattern (for N = 4).
1 1
1 2 2 1
1 2 3 3 2 1
1 2 3 4 3 2 1
Constraints: 0 < N < 10
Sample Input: 4
Sample Output: 1 1
1 2 2 1
1 2 3 3 2 1
1 2 3 4 3 2 1
Explanation: Each number is separated from other by a tab.
*/
#include<iostream>
using namespace std;
int main() {
int total_rows;
cin >> total_rows;
int total_cols = total_rows + (total_rows-1);
int total_space = total_cols - 2;
int row = 1;
while (row <= total_rows){
int col = 1;
int val = 1;
if (total_rows == 1){ // for first row only
cout << val << "\t";
break;
}
// printing first pattern
while (col <= row){
cout << val << "\t";
val ++;
col++;
}
// printing spaces
col = 1;
while (col <= total_space){
cout << " " << "\t";
col ++;
}
total_space -= 2;
//printing second pattern
col = 1;
if (row == total_rows){ // for last row only
val--;
col ++;
}
while (col <= row){
cout << --val << "\t";
col++;
}
cout << endl;
row++;
}
return 0;
}
/*
In Pattern Question, there is a common method which we teach in course viz, taking nsp, nst, csp, cst and row variables.
Pattern Hack: Always first Try to first print pattern by ignoring the value to be printed then accommodate your value in that pattern. For e.g.,
1 1
1 2 2 1
1 2 3 3 2 1
1 2 3 4 3 2 1
View it as:-
* *
* * * *
* * * * * *
* * * * * * *
Short Info about the variables:-
1. nsp (number of spaces)-> Number of spaces in very First Line of the pattern.
2. nst (number of stars)-> Number of stars in very first line of the pattern.
3. csp (counter of spaces)-> counter of spaces that will print the required number of spaces and will be initialized with 1 and incremented upto nsp.
4. cst (counter of stars)-> counter of spaces that will print the required number of stars and will be initialized with 1 and incremented upto nst.
5. rows -> It will be initialized with 1 and will go upto the total number of rows in the pattern.
Given pattern is seen as first work to print the numbers in increasing order then print the spaces accordingly and then the same pattern of numbers but in reverse order.
So first do work for numbers then for spaces and then for numbers again.
And then update variables accordingly for next iterations.
Each number is separated from other by a tab ao use ' \t' for tab space.
Code:
import java.util.Scanner;
public class patternMountain {
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int nsp=2*n-3; //initializing number of spaces
int nst=1; //initializing number of numbers
for(int i=1;i<=n;i++)
{
int num=1;
//work for numbers
for(int cst=1;cst<=nst;cst++)
{
if(cst!=n)
System.out.print(num+"\t");
num++;
}
//work for spaces
for(int csp=1;csp<=nsp;csp++)
{
System.out.print(" \t");
}
//work for numbers
for(int cst=num-1;cst>=1;cst--)
{
System.out.print(cst+"\t");
}
//preparation for next iteration
nsp=nsp-2;
nst++;
System.out.println();
}
}
}
*/