16进制转化8进制---map

时间:2021-09-22 12:44:39
#include "stdio.h"
#include "string.h"
#include "string"
#include "iostream"
#include "math.h"
#include "map"
#include "queue"
#include "malloc.h"
using namespace std;
map<char,string> mp; int main()
{
mp['']="";mp['']="";mp['']="";mp['']="";mp['']="";
mp['']="";mp['']="";mp['']="";mp['']="";mp['']="";
mp['A']="";mp['B']="";mp['C']="";mp['D']="";mp['E']="";
mp['F']="";
int a;
char test[];
cin >> a;
while(a--)
{ memset(test,,sizeof(test));
string b="";
cin>>test;
for(int i = ;i < strlen(test);i++)
{
b += mp[test[i]];
}
int len = b.length();
if(len%==)
b = "" + b;
else if(len%==)
b = "" + b;
int flag = ;
for(int i = ;i < b.length(); i = i + )
{
int num = *(b[i]-'')+*(b[i+]-'')+(b[i+]-'');
if(num)
flag = ;
if(flag)
cout<<num; }
cout<<endl;
}
return ;
}

但是这个跑在蓝桥杯的题目中会超时,因为map查询是对数时间复杂度放在循环中会超时

更改map后通过

#include "stdio.h"
#include "string.h"
#include "string"
#include "iostream"
#include "math.h"
#include "map"
#include "queue"
using namespace std;
map<char,string> mp; int main()
{
int a;
string test;
cin >> a;
while(a--)
{
string b="";
cin>>test;
for(int i=;i<test.length();i++)//遍历,字符串上加上每一位
{
switch(test[i])
{
case '':b+="";break;
case '':b+="";break;
case '':b+="";break;
case '':b+="";break;
case '':b+="";break;
case '':b+="";break;
case '':b+="";break;
case '':b+="";break;
case '':b+="";break;
case '':b+="";break;
case 'A':b+="";break;
case 'B':b+="";break;
case 'C':b+="";break;
case 'D':b+="";break;
case 'E':b+="";break;
case 'F':b+="";break;
default:break;
}
}
int len = b.length();
if(len%==)
b = "" + b;
else if(len%==)
b = "" + b;
int flag = ;
for(int i = ;i < b.length(); i = i + )
{
int num = *(b[i]-'')+*(b[i+]-'')+(b[i+]-'');
if(num)
flag = ;
if(flag)
cout<<num;
}
cout<<endl;
} return ;
}