📄 pex2_1.cpp
字号:
#include <iostream.h>
#include <iomanip.h>
void BaseOut(unsigned int n, int b)
{
// outputbuf holds the converted number, p moves
// along and does the work. the result is left-justified
// in a field of 10 spaces
char outputbuf[11] = " ", *p = outputbuf+10;
// NULL-terminate the array
*p = 0;
// find the digits in base b right to left
while (n != 0)
{
// digit is remainder. convert to ASCII
*--p = n % b + '0';
// divide by base
n = n/b;
}
cout << outputbuf;
}
void main (void)
{
cout << "Number Base 2 Base 4 Base 5 Base 8 Base 9" << endl;
for (int i = 2; i <= 50; i++)
{
cout << setw(2) << i << " ";
BaseOut(i,2);
cout << " ";
BaseOut(i,4);
cout << " ";
BaseOut(i,5);
cout << " ";
BaseOut(i,8);
cout << " ";
BaseOut(i,9);
cout << endl;
}
}
/*
<Run>
Number Base 2 Base 4 Base 5 Base 8 Base 9
2 10 2 2 2 2
3 11 3 3 3 3
4 100 10 4 4 4
5 101 11 10 5 5
6 110 12 11 6 6
7 111 13 12 7 7
8 1000 20 13 10 8
9 1001 21 14 11 10
10 1010 22 20 12 11
... ... ... ... ... ...
45 101101 231 140 55 50
46 101110 232 141 56 51
47 101111 233 142 57 52
48 110000 300 143 60 53
49 110001 301 144 61 54
50 110010 302 200 62 55
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -