Я предполагаю, что вы знакомы с циклами, массивами, матрицами, типами данных, функциями.
(Если не посмотрите ЧАСТЬ-I и начните читать с учебника)
Однако для этой статьи будет достаточно знания только петель.
Я настоятельно рекомендую вам попробовать запустить весь код одновременно, пока вы читаете и разбираетесь в статье.
Давай начнем
Я часто находил, что лучший способ практиковаться и хорошо разбираться в петлях - это делать выкройки.
Схема 1:
Print this pattern ********** ********** ********** **********
Программа:
#include<iostream> using namespace std; int main() { cout<<“**********”<<endl; cout<<“**********”<<endl; cout<<”**********”<<endl; cout<<“**********”<<endl; return 0; } This works, but we can do better. Remember a program is most useful where you have to do something repeatedly. #include<iostream> using namespace std; int main() { for(int i=0;i<4;i++) cout<<“**********”<<endl; return 0; } We can do still better. Remember a program is most useful where you have to do something repeatedly. #include<iostream> using namespace std; int main() { for(int i=0;i<4;i++) { for(int j=0;j<10;j++) { cout<<“*"; } cout<<endl; // next line after printing the complete row } return 0; } You are printing '*' for 10 times for a row, which is printed 4 times. It can also be seen as j 0 1 2 3 4 5 6 7 8 9 i 0 00 01 02 03 04 05 06 07 08 09 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 26 27 28 29 3 30 31 32 33 34 35 36 37 38 39 you have to print * on each location produced by combination of i & j. (you will see why this approach is important)
Схема 2:
Print this pattern * * * * *
Программа:
j 0 1 2 3 4 i 0 00 01 02 03 04 1 10 11 12 13 14 2 20 21 22 23 24 3 30 31 32 33 34 4 40 41 42 43 44 you have to print * on locations that are highlighted. Can you find a property that holds true for the diagonal. Yes i == j is true for this diagonal. #include<iostream> using namespace std; int main() { for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { if(i==j) // The required condition cout<<"*"; else cout<<" "; } cout<<endl; } return 0; }
Шаблон 3:
Print this pattern * * * * *
Программа:
j 0 1 2 3 4 i 0 00 01 02 03 04 1 10 11 12 13 14 2 20 21 22 23 24 3 30 31 32 33 34 4 40 41 42 43 44 you have to print * on locations that are highlighted. Can you find a property that holds true for the diagonal. Yes i + j == 4 is true for this diagonal. #include<iostream> using namespace std; int main() { for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { if(i+j==4) // The required condition cout<<"*"; else cout<<" "; } cout<<endl; } return 0; }
Теперь вы можете удалить все шаблоны.
Шаблон 4:
Print this pattern * * * * * * * * * * * * * * *
Программа:
j 0 1 2 3 4 i 0 00 01 02 03 04 1 10 11 12 13 14 2 20 21 22 23 24 3 30 31 32 33 34 4 40 41 42 43 44 you have to print * on locations that are highlighted. Can you find a property that holds true for the diagonal. The diagonal holds property i+j == 4, anything below the diagonal will have higher value of i and j than on the diagonals therefore the required condition is i+j >= 4 #include<iostream> using namespace std; int main() { for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { if(i+j>=4) // The required condition cout<<"*"; else cout<<" "; } cout<<endl; } return 0; }
Шаблон 5:
Print this pattern * * * * * * * * * * * * * * *
Программа:
j 0 1 2 3 4 i 0 00 01 02 03 04 1 10 11 12 13 14 2 20 21 22 23 24 3 30 31 32 33 34 4 40 41 42 43 44 you have learned the trick now, try it yourself. #include<iostream> using namespace std; int main() { for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { if(j>=i) // The required condition cout<<"*"; else cout<<" "; } cout<<endl; } return 0; } The diagonal holds property i==j, anything on right of the diagonal would have higher value of j than on the diagonal.
Шаблон 6:
Print this pattern * * * * * * * * * * * * * * * * * * * * * * * * *
Программа:
j 0 1 2 3 4 5 6 7 8 i 0 00 01 02 03 04 05 06 07 08 1 10 11 12 13 14 15 16 17 18 2 20 21 22 23 24 25 26 27 28 3 30 31 32 33 34 35 36 37 38 4 40 41 42 43 44 45 46 47 48 Often times a pattern can be broken into simpler patterns that we already know about. Like in this case j 0 1 2 3 4 4 5 6 7 8 i 0 00 01 02 03 04 04 05 06 07 08 1 10 11 12 13 14 14 15 16 17 18 2 20 21 22 23 24 24 25 26 27 28 3 30 31 32 33 34 34 35 36 37 38 4 40 41 42 43 44 44 45 46 47 48 you have learned the trick now, try it yourself. #include<iostream> using namespace std; int main() { for(int i=0;i<5;i++) { for(int j=0;j<9;j++) { if(j>=i && j<=4) // The required condition for 1st part cout<<"*"; else if(i+j<=8 && j>4) // condition for 2nd part cout<<"*"; // observe j>4 and not j>=4 else cout<<" "; } cout<<endl; } return 0; }
Шаблон 6:
Print this pattern * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Попробуйте сами.
Схема 7. ( Сначала попробуйте сами , а затем просмотрите код)
Print this pattern 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 4 3 2 1
Программа:
j 0 1 2 3 4 5 6 7 8 i 0 00 01 02 03 04 05 06 07 08 1 10 11 12 13 14 15 16 17 18 2 20 21 22 23 24 25 26 27 28 3 30 31 32 33 34 35 36 37 38 4 40 41 42 43 44 45 46 47 48 #include<iostream> using namespace std; int main() { for(int i=0;i<5;i++) { int n = 1; // to start printing from 1 for(int j=0;j<=4;j++) { if(i+j>=4) { cout<<n; // print and increments till middle n++; } else { cout<<" "; } } n--; // adjust the value to start decreasing for(int j=5;j<10;j++) { if(j-i<=4) { n--; // print and decrements after middle cout<<n; } else { cout<<" "; } } cout<<endl; } return 0; }
Если вы здесь и пробовали сгенерировать все вышеперечисленные шаблоны. Вы готовы решать все шаблоны, с которыми вам когда-либо придется столкнуться.