输入一个十进制数,用程序将其变换成2进制数和8进制数。
每一组数据只有一行,即需要被变换的十进制整数。(输入有多组测试数据):3
对应每一组数据,输出对应的2进制和8进制,第一行输出2进制,第二行输出8进制数:11 3
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> using namespace std; int main() { int num;//输入变量 int i=0, j=0;//定义循环变量 while (cin >> num) { int a[256], b[256];//存放进制数 int temp1, temp2; int count1 = 0; int count2 = 0; temp1 = temp2 = num; do { a[count1] = temp1 % 2; count1 = count1 + 1; temp1 = temp1 / 2; } while (temp1 != 0); for (i = count1-1; i >=0; i--) { cout << a[i]; } cout << endl; do { b[count2] = temp2 % 8; count2 = count2 + 1; temp2 = temp2 / 8; } while (temp2 != 0); for (i = count2-1; i >= 0 ; i--) { cout << b[i]; } cout << endl; } return 0; }
数组存放