fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4.  
  5. int main()
  6. {
  7. int cols = 0;
  8. do
  9. {
  10. std::cout << "Enter the number of columns: " << std::flush;
  11. } while(!(std::cin >> cols) || cols <= 0); std::cout << std::endl;
  12. int total = std::pow(3.0, cols);
  13. for(int i = 0; i < total; ++i)
  14. {
  15. for(int j = cols-1; j >= 0; --j)
  16. {
  17. std::cout << (i/static_cast<int>(std::pow(3.0, j)))%3;
  18. }
  19. std::cout << std::endl;
  20. }
  21. }
Success #stdin #stdout 0s 3300KB
stdin
4
stdout
Enter the number of columns: 
0000
0001
0002
0010
0011
0012
0020
0021
0022
0100
0101
0102
0110
0111
0112
0120
0121
0122
0200
0201
0202
0210
0211
0212
0220
0221
0222
1000
1001
1002
1010
1011
1012
1020
1021
1022
1100
1101
1102
1110
1111
1112
1120
1121
1122
1200
1201
1202
1210
1211
1212
1220
1221
1222
2000
2001
2002
2010
2011
2012
2020
2021
2022
2100
2101
2102
2110
2111
2112
2120
2121
2122
2200
2201
2202
2210
2211
2212
2220
2221
2222