#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<limits.h> #include<algorithm> #include<queue> #include<vector> #include<set> #include<stack> #include<string> #include<sstream> #include<map> #include<cctype> using namespace std; char bin[500]; int len; void T2B(int T,int BC) //進制轉換 T:需要轉換的數字 BC:十進制到BC進制 { memset(bin,0,sizeof(bin));//數組歸零 len = 0; int shang = 0; while(T>=1) { shang = T % BC; T /= BC; bin[len++] = 48 + shang; } strrev(bin); //反轉字符串 } int main() { for(int i=0;i<31;i++) { T2B(i,2); for(int j=0;j<5-len;j++) { cout<<0; } cout<<bin<<endl; } }
進制轉換 從十進制轉換到任意進制 注意bin的長度即可