Skip to content

Instantly share code, notes, and snippets.

@yudegaki
Last active February 17, 2020 02:52
Show Gist options
  • Save yudegaki/31b516395109494a64558c214daad19c to your computer and use it in GitHub Desktop.
Save yudegaki/31b516395109494a64558c214daad19c to your computer and use it in GitHub Desktop.
//「000」〜「111」を出力するプログラム
#include<bits/stdc++.h>
using namespace std;
void printBit(int bit,int digit){//bitを表示する数字と何桁まで表示するかを引数で指定
string s;
for(int i = 0;i < digit;i++){
if(bit & (1LL << i)){//bitが立っていたら
s = "1" + s;
}
else {
s = "0" + s;
}
}
cout<<s<<endl;
}
int main(){
int n = 3;//今回は3桁なので
for(int i = 0; i < (1LL << n)/*pow(2,n)と同義*/ ;i++){
printBit(i,n);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment