算法提高 P0102(栈)

来源:互联网 发布:淘宝现在怎么搜高仿鞋 编辑:程序博客网 时间:2024/05/17 18:46

传送门: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;}
原创粉丝点击