算法提高 P0102(栈)

传送门:http://lx.lanqiao.cn/problem.page?gpid=T427
 用户输入三个字符,每个字符取值范围是0-9,A-F。然后程序会把这三个字符转化为相应的十六进制整数,并分别以十六进制,十进制,八进制输出,十六进制表示成3位,八进制表示成4位,若不够前面补0。(不考虑输入不合法的情况)
输入
  1D5
输出
(注意冒号后面有一个空格)
  Hex: 0x1D5
  Decimal: 469
  Octal: 0725

#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
#define pb push_back
#define rep(i,a,n) for(int i=a; i<n; i++)
typedef long long ll;
typedef unsigned long long ull;
const int mod=1e9+7;
const double eps=1e-6;
const int INF=0x3f3f3f3f;
const int N=1e4+5;
int main()
{
    string str;
    while(cin>>str)
    {
        printf("Hex: 0x");
        cout<<str<<endl;
        int tmp,dec=0;
        for(int i=0; i<3; i++)
        {
            if(isalpha(str[i])) tmp=str[i]-'A'+10;
            else tmp=str[i]-'0';
            dec=dec+pow(16,3-i-1)*tmp;
        }
        printf("Decimal: %d\n",dec);
        stack<int>oct;
        while(dec)
        {
            oct.push(dec%8);
            dec/=8;
        }
        printf("Octal: ");
        if(oct.size()<4) for(int i=0; i<4-oct.size(); i++) putchar('0');
        while(!oct.empty()) printf("%d",oct.top()),oct.pop();
        puts("");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值