题解 | 进制转换

进制转换

https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6


#include<cmath>
#include <iostream>
#include<string>
using namespace std;
using ll=long long;
int main() {
   string s;
   cin>>s;
   string k=s.substr(2);
   int j=0;ll ans=0;
   for(int i=(int)k.size()-1;i>=0;i--)
   {
      if(k[i]>='0'&&k[i]<='9')
      {
        ans=ans+(k[i]-'0')*pow(16,j++);
      }
      else if(k[i]>='A'&&k[i]<='F')
      {
        ll num=k[i]-'A'+10;
        ans+=num*pow(16,j++);
      }
   }
   cout<<ans<<endl;
}
// 64 位输出请用 printf("%lld")

#牛客春招刷题训练营#https://gw-c.nowcoder.com/api/sparta/jump/link?link=https%3A%2F%2Fwww.nowcoder.com%2Fdiscuss%2F726480854079250432

//也是个模拟吧,我讲几个点,首先肯定是好习惯开long long。

//然后就是substr的截取运用,吧ox给去掉,然后再倒序去看k的每个数。然后在外面搞个计数器j就行了

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务