这是题目链接供大家参考:
http://39.107.118.220/problem.php?cid=1001&pid=6
题意很简单就是将十六进制转换成十八进制,但是十六进制字符串长度达到1e6啦,所以直接转换成十进制在做肯定不行,所以我们要换个做法就是转换成二进制字符串然后对每个·数字的二进制数补齐四位(卡了好久,气死我了),再三位一取转换成八进制数:
直接上AC代码:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int poww(int a,int b)
{
int ret = 1;
for(int i = 1;i <= b;i++)
{
ret = ret * a;
}
return ret;
}
int main()
{
string s;
string s1,s2;
while(cin >> s)
{
s1 = "";
stack<long long>q;
for(int i = 0;i < s.size();i++)
{
s2 = "";
int cnt = 0;
if(s[i] >= '0' && s[i] <= '9')
{
int num = s[i] - '0';
int k = num;
while(num)
{
num/=2;
cnt++;
}
if(cnt < 4)
{
for(int i = 0;i < 4 - cnt;i++) s1+='0';
}
while(k)
{
s2 += (k%2+'0');
k /= 2;
}
reverse(s2.begin(),s2.end());
s1 += s2;
}
if(s[i] >= 'A' && s[i] <= 'F')
{
int num = s[i] - 'A' + 10;
int k = num;
while(num)
{
num/=2;
cnt++;
}
if(cnt < 4)
{
for(int i = 0;i < 4 - cnt;i++) s1+='0';
}
while(k)
{
s2 += (k%2+'0');
k /= 2;
}
reverse(s2.begin(),s2.end());
s1 += s2;
}
}
//cout << s1 << endl;
for(int i = s1.size() - 1;i >= 0;i-=3)
{
int k = min(2,i);
long long sum = 0;
int ret = 0;
for(int j = i;j >= i - k;j--)
{
sum += (s1[j] - '0') * (poww(2,ret));
ret++;
}
q.push(sum);
}
int flag = 0;
while(!q.empty())
{
if(q.top() != 0)
{
flag = 1;
}
if(flag == 1)
{
cout << q.top();
}
q.pop();
}
cout << endl;
}
return 0;
}