daily pastebin goal
16%
SHARE
TWEET

Untitled

a guest Nov 22nd, 2017 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. string reverseStr(string& line) {
  7.     string reverseLine = "";
  8.     for (int i = line.length() - 1; i >= 0; --i) {
  9.         reverseLine += line[i];
  10.     }
  11.     return reverseLine;
  12. }
  13.  
  14. string makeNewLine(string& oldLine) {
  15.     string seqZero = "", line = reverseStr(oldLine);
  16.     while (line.find('0') == 0) {
  17.         seqZero += "0";
  18.         line.erase(0, 1);
  19.     }
  20.     int posZero = line.find('0');
  21.     if (posZero != -1) {
  22.         int num = line[posZero - 1];
  23.         line[posZero - 1] = line[posZero];
  24.         line[posZero] = num;
  25.     }
  26.     seqZero += line;
  27.     line = reverseStr(seqZero);
  28.     return line;
  29. }
  30.  
  31. void makeSeq(int n, int k) {
  32.     string line = "";
  33.     for (int i = 0; i < n - k; ++i) {
  34.         line += "0";
  35.     }
  36.     for (int i = 0; i < k; ++i) {
  37.         line += "1";
  38.     }
  39.     cout << line << endl;
  40.     while (line.find('0') != k) {
  41.         string newL = makeNewLine(line);
  42.         line = newL;
  43.         cout << line << endl;
  44.     }
  45. }
  46.  
  47. int main() {
  48.     int n, k;
  49.     cin >> n >> k;
  50.     if (k == 0) {
  51.         for (int i = 0; i < n; ++i) {
  52.             cout << "0";
  53.         }
  54.     } else if (k == n) {
  55.         for (int i = 0; i < n; ++i) {
  56.             cout << "1";
  57.         }
  58.     } else {
  59.         makeSeq(n, k);
  60.     }
  61.     return 0;
  62. }
RAW Paste Data
Top