# 蓝桥杯 十六进制转八进制

``````#include<string>
#include<iostream>
#include<stdlib.h>

using namespace std;

void change(string a[], int n)
{
string b;
for (int i = 0; i < a[n].size(); i++)
{
switch (a[n][i])
{

case '0':b += "0000"; break;
case '1':b += "0001"; break;
case '2':b += "0010"; break;
case '3':b += "0011"; break;
case '4':b += "0100"; break;
case '5':b += "0101"; break;
case '6':b += "0110"; break;
case '7':b += "0111"; break;
case '8':b += "1000"; break;
case '9':b += "1001"; break;
case 'A':b += "1010"; break;
case 'B':b += "1011"; break;
case 'C':b += "1100"; break;
case'D':b += "1101"; break;
case'E':b += "1110"; break;
case 'F':b += "1111"; break;
default:break;
}
}//16进制转化为2进制
if (b.size() % 3 == 1)
{
b = "00" + b;
}
if (b.size() % 3 == 2)
{
b = "0" + b;
}//2进制转化为8进制
int flag = 0;//设个东西把转成八进制后前面那个0去掉
for (unsigned int i = 0; i < b.size() - 2; i = i + 3)
{
int p = 4 * (b[i] - '0') + 2 * (b[i + 1] - '0') + (b[i + 2] - '0');
if (p)
{
flag = 1;
}
if (flag)
{
cout << p;
}

}
}

int main()
{
int n;
cin >> n;//几个16进制数
string a[30];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
for (int i = 0; i < n; i++)
{
change(a, i);
cout << endl;
}
return 0;
}
``````

THE END

)">