20220814大疆C++开发B卷,base64
20220814大疆c++开发
人麻了,读题至少读了接近半个小时,没有解释可太伤了,最后只过了,86%,没有调出来,非常可惜,各路大佬来看看我到底是哪里有问题,欢迎批评指正。
#include<bits/stdc++.h>
using namespace std;
int get10(char c)
{
if(c>='0'&&c<='9')
return c-'0';
else if(c>='a'&&c<='f')
return c-'a'+10;
else if(c>='A'&&c<='F')
return c-'A'+10;
}
char tobase64(int num)
{
if(num>=0&&num<=25)
return num+'A';
else if(num>=26&&num<=51)
return num+'a'-26;
else if(num>=52&&num<=61)
return num+'0'-52;
else if(num==62)
return '+';
else if (num==63)
return '/';
}
int main()
{
string str;
while(cin>>str)
{
int len=str.length();
if(len==0)
{
cout<<"==="<<endl;
continue;
}
int last=0;
for(int i=0; i<len; i++)
{
int now=get10(str[i]);
if(i%3==0)
{
last=now;
}
else if(i%3==1)
{
cout<<tobase64(now/4+last*4);
last=now&3;
}
else if(i%3==2)
{
cout<<tobase64(last*16+now);
last=0;
}
}
if((len/2)%3==2)
{
cout<<tobase64(last*4);
cout<<"="<<endl;
}
else if((len/2)%3==1)
{
cout<<tobase64(last*16);
cout<<"=="<<endl;
}
else
cout<<endl;
}
return 0;
}
