Sinh các dãy nhị phân


Submit solution

Points: 2 (partial)
Time limit: 1.0s
Memory limit: 98M

Author:
Problem types
Allowed languages
Ada, Assembly, Awk, C, C++, C11, CLANG, CLANGX, Classical, COBOL, Coffee, CSC, D lang, DART, F95, FORTH, Fortrn, GAS32, GO, Haskell, Itercal, Java, kotlin, LEAN, LISP, LUA, MONOVB, Nasm, OCAML, Pascal, Perl, php, PIKE, prolog, Pypy, Python, Ruby 2, RUST, Scala, SCM, SED, SWIFT, TCL, TUR, V8JS, VB, ZIG

Nhập vào số nguyên dương n (1n9), hãy sinh các dãy nhị phân có độ dài n.

Input

Số nguyên duy nhất n

Output

Mỗi dòng một dãy nhị phân được liệt kê theo thứ tự từ điển.

Example

Input:

Copy
3

Output:

Copy
000
001
010
011
100
101
110
111
tichpx

Comments


  • 1
    TICHPX  commented on June 13, 2020, 4:14 a.m.

    code tham khảo by bitset

    Copy
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int n;
        cin>>n;
        for(int i=0; i<(1<<n); i++)
        {
            bitset<12> x(i);
            string z=x.to_string();
            cout<<z.substr(12-n)<<"\n";
        }
    }

  • 2
    dvCuong  commented on Oct. 21, 2017, 6:41 a.m. edited

    include<stdio.h>

    Copy
    char x[1000];
    int n;
    void TRY(int k){
        if(k==n){
            printf("%s\n",x+1);
            return;
        }
            x[k+1]='0'; TRY(k+1);
            x[k+1]='1'; TRY(k+1);
    
    }
    int main(){
        scanf("%d",&n);
        TRY(0);
    }