fork download
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::endl;
  5.  
  6. void ShowRow(int value, int length_of_row) {
  7. for (int i = 0; i < length_of_row; ++i) {
  8. cout << value;
  9. }
  10. cout << endl;
  11. }
  12.  
  13. int GetIncrement(int start_value, int end_value) {
  14. int increment = 0;
  15. if (start_value > end_value) {
  16. increment = -1;
  17. } else {
  18. increment = 1;
  19. }
  20. return increment;
  21. }
  22.  
  23. void ShowTriangle(int value, int start_lenght, int end_length) {
  24. int increment = GetIncrement(start_lenght, end_length);
  25. for (int i = start_lenght; i != end_length; i += increment) {
  26. ShowRow(value, i);
  27. }
  28. ShowRow(value, end_length);
  29. }
  30.  
  31. void ShowSnake(int start_value, int end_value, int start_lenght,
  32. int end_length) {
  33. int current_value = start_value;
  34. const int increment = GetIncrement(start_value, end_value);
  35. while (current_value <= end_value) {
  36. ShowTriangle(current_value, start_lenght, end_length);
  37. current_value += increment;
  38. std::swap(start_lenght, end_length);
  39. }
  40. }
  41.  
  42. int main() { ShowSnake(1, 5, 9, 4); }
  43.  
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
111111111
11111111
1111111
111111
11111
1111
2222
22222
222222
2222222
22222222
222222222
333333333
33333333
3333333
333333
33333
3333
4444
44444
444444
4444444
44444444
444444444
555555555
55555555
5555555
555555
55555
5555