- #include <iostream>
- using namespace std;
- void decimal_binary(int x)
- {
- int temp=x,size=0;
- while (temp)
- {
- size++;
- temp=temp/2;
- }
- /*if(x<0)
- {
- size=size+1;
- int *array=new int [size];
- }
- else*/
- size=size+1;
- int *array=new int [size];
- if(x<0) array[0]=1;
- else array[0]=0;
- for(int i=size-1;i>=1;i--)
- {
- if(x>=0)
- { array[i]=x%2;
- x=x/2;
- }
- else
- { array[i]=(-x)%2;
- x=x/2;
- }
- }
- for(i=0;i<size;i++)
- {
- cout<<array[i];
- }
- cout<<"B"<<endl;
- }
- int main()
- {
- cout<<"input number:"<<endl;
- int n;
- cin>>n;
- decimal_binary(n);
- return 0;
- }
convert decimal to binary
最新推荐文章于 2022-03-07 16:34:26 发布