9 条题解

  • 0
    @ 2023-3-30 14:20:14

    $\texttt{\large\color{#12a1e8}本蒟蒻的第六篇\color{#92a1e8}题\color{#92b1d8}解}$

    进制转换(十六转十){\large\color{#92a1e8}进制转换(十六转十)}

    难度:普及-\texttt{\small\color{#e57321}难度:普及-}


    我们首先要理解进制转换的含义,很简单,每一位求出来,乘以权值,加起来即可(这是笔者第一次被卡住的题目,虽然依旧是全班最先解决的人


    Python{\large\color{#52a1e8}Python:}

    ord()可以用于求字符串ASCII码,以此进行转换

    思路:从左到右算,加到summ中,每向右一位就乘以对应权值即可

    a=str(input())
    n=0
    summ=0
    while n<len(a):
        summ*=16
        if(a[n]>='0' and a[n]<='9'):
            summ+=ord(a[n])-ord('0')
        else:
            if(a[n]>='a' and a[n]<='f'):
                summ+=ord(a[n])-ord('a')+10
            else:
                summ+=ord(a[n])-ord('A')+10
        n+=1
    print(summ)
    

    C++{\large\color{#52a1e8}C++:}

    首次做这题的代码,码风欠佳,与python写法不同,思路是求出每一位的值,在最后统一计算

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=1000;
    string x;
    long long a,b[maxn],c[maxn],d,i,max,q,w,e,r,t,y,u,o,p;
    int main(){
    	cin>>x;
    	i=0;
    	while(i<x.length()){
    		
    		if(x[i]>='0'&&x[i]<='9'){
    			b[i]=x[i]-48;
    		}
    		else if(x[i]>='A'&&x[i]<='F'){
    			b[i]=x[i]-55;
    		}
    		else{
    			b[i]=x[i]-87;
    		}
    		i++;
    	}
    	e=x.length();
    	i=0;
    	while(i<x.length()){
    		
    		y=pow(16,(e-i-1));
    		d=d+b[i]*y;
    		i++;
    	}
    	cout<<d;
    	return 0;
    }
    
    • @ 2023-12-19 10:51:01

      哥哥你好厉害啊❤️

信息

ID
60
时间
1000ms
内存
256MiB
难度
5
标签
递交数
592
已通过
245
上传者