BASIC-10 十进制转十六进制
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int a,l1; string s1;
int main()
{
scanf("%d",&a);
// 记得特判0
if (a==0)
{
printf("0");
return 0;
}
while(a)
{
int tmp=a%16;
switch(tmp)
{
// string字符串可以通过以下方式直接从左起插入字符
case 0: s1="0"+s1; break;
case 1: s1="1"+s1; break;
case 2: s1="2"+s1; break;
case 3: s1="3"+s1; break;
case 4: s1="4"+s1; break;
case 5: s1="5"+s1; break;
case 6: s1="6"+s1; break;
case 7: s1="7"+s1; break;
case 8: s1="8"+s1; break;
case 9: s1="9"+s1; break;
case 10: s1="A"+s1; break;
case 11: s1="B"+s1; break;
case 12: s1="C"+s1; break;
case 13: s1="D"+s1; break;
case 14: s1="E"+s1; break;
case 15: s1="F"+s1; break;
default: break;
}
a/=16;
}
l1=s1.length();
cout<<s1;
return 0;
}
BASIC-11 十六进制转十进制
#include<bits/stdc++.h>
#define ll long long
using namespace std;
string s1,s2="";
int l1,l2;ll ans=0;
int main()
{
cin>>s1;
l1=s1.length();
for (int i=0;i<l1;i++)
{
switch(s1[i])
{
case '0': s2+="0000"; break;
case '1': s2+="0001"; break;
case '2': s2+="0010"; break;
case '3': s2+="0011"; break;
case '4': s2+="0100"; break;
case '5': s2+="0101"; break;
case '6': s2+="0110"; break;
case '7': s2+="0111"; break;
case '8': s2+="1000"; break;
case '9': s2+="1001"; break;
case 'A': s2+="1010"; break;
case 'B': s2+="1011"; break;
case 'C': s2+="1100"; break;
case 'D': s2+="1101"; break;
case 'E': s2+="1110"; break;
case 'F': s2+="1111"; break;
default: break;
}
}
l2=s2.length();
ll tmp=1;
for (int i=l2-1;i>=0;i--)
{
ans+=tmp*(s2[i]-'0');
tmp*=2;
}
printf("%lld",ans);
return 0;
}
BASIC-12 十六进制转八进制
#include<bits/stdc++.h>
using namespace std;
int n,l1,l2; string s1,s2;// s1存原始十六进制数,s2存转换的二进制数
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
cin>>s1;
s2=""; //初始化
l1=s1.length();
for (int j=0;j<l1;j++)
{
switch(s1[j]) // 十六进制转二进制
{
case '0': s2+="0000"; break;
case '1': s2+="0001"; break;
case '2': s2+="0010"; break;
case '3': s2+="0011"; break;
case '4': s2+="0100"; break;
case '5': s2+="0101"; break;
case '6': s2+="0110"; break;
case '7': s2+="0111"; break;
case '8': s2+="1000"; break;
case '9': s2+="1001"; break;
case 'A': s2+="1010"; break;
case 'B': s2+="1011"; break;
case 'C': s2+="1100"; break;
case 'D': s2+="1101"; break;
case 'E': s2+="1110"; break;
case 'F': s2+="1111"; break;
default: break;
}
}
l2=s2.length();
if (l2%3==1) s2="00"+s2; // 八进制数是三位三位看的,长度不足三的倍数的话需要补充前导0
else if (l2%3==2) s2="0"+s2;
int tmp,flag=0; // flag=0默认输出有前导0
for (int j=0;j<l2;j+=3)
{
tmp=4*(s2[j]-'0')+2*(s2[j+1]-'0')+(s2[j+2]-'0');
if (tmp) flag=1; // 避免输出结果带有前导0
if (flag) cout<<tmp;
}
printf("\n");
}
return 0;
}
switch case的妙用,一万年没写题,基本忘的差不多了
康复训练的康复训练
ACM已无缘,蓝桥杯还是想试试,估计是带学里最后一次机会了