UVa355 - The Bases Are Loaded

简介: UVa355 - The Bases Are Loaded
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <stack>usingnamespacestd;
intchange1(chara)
{
if(a>='0'&&a<='9')
returna-'0';
elsereturna-'A'+10;
}
voidsolve(unsignedlonglongn, intk)
{
stack<int>ans;
inttemp;
if(n==0) {
printf("0");
return;
    }
while(n) {
ans.push(n%k);
n/=k;
    }
while(!ans.empty()) {
temp=ans.top();
ans.pop();
if(temp<=9)
printf("%d",temp);
elseprintf("%c",temp-10+'A');
    }
}
intmain()
{
inti, b1, b2, len, t;
charstr[15];
boolflag;
unsignedlonglongsum, k;
while(cin>>b1>>b2) {
cin>>str;
len=strlen(str);
sum=0;
k=1;
flag=true;
for(i=len-1; i>=0; i--) {
t=change1(str[i]);
if( (t>=b1||t<0) ||str[i]=='-') {
cout<<str<<" is an illegal base "<<b1<<" number"<<endl;
flag=false;
break;
            }
sum+=t*k;
k*=b1;
        }
if(flag) {
cout<<str<<" base "<<b1<<" = ";
solve(sum, b2);
cout<<" base "<<b2<<endl;
        }
    }
return0;
}
目录
相关文章
UVa11958 - Coming Home
UVa11958 - Coming Home
52 0
UVa11565 - Simple Equations
UVa11565 - Simple Equations
54 0
UVa389 - Basically Speaking
UVa389 - Basically Speaking
40 0
About the Importance of Aim in Life
Have an aim in life, or your energies will all be wasted.   ---R. Peters 人生应该树立目标,否则你的精力会白白浪费。 ---彼得斯
792 0
uva 1326 - Jurassic Remains
点击打开链接uva 1326 题意:给定n个由大写字母组成的字符串,选择尽量多的串使得每个大写字母都能出现偶数次 分析: 1 在一个字符串中每个字符出现的次数是无关的,重要的是只是这些次数的奇偶性。
928 0
uva 11384 Help is needed for Dexter
点击打开链接uva 11384 思路:找规律 分析: 1 题目说给定一个小于10^9的数,现在有n个数要求经过最少的步骤使得这个序列的所有数都为0,求这个最少的步骤 2 很明显的找规律题,题目明确说明每一次可以选择任意个的数减去一个正整数...
780 0