您的位置:首页 > 其它

“盛大游戏杯”第15届上海大学程序设计联赛夏季赛暨上海高校金马五校赛 E

2017-07-13 23:10 288 查看
Claire Redfield在龙之谷游戏的一次任务中获得了一个上了锁的宝箱,上面刻了一串由小写字母构成的字符串A和一个数字m 。

经过Claire长时间研究,他发现密码是和a ,m 有关的。字符串A相当于一个26进制的数字,a 相当于0 ,b 相当于1 …….z 相当于25 。然后要将这个26进制的数转化成m 进制那就是这个宝箱的密码。

Claire觉得这个太简单了所以要你帮她完成。

输入
多组输入,每组一行,输入一个字符串A 和一个正整数m 。
字符串长度<=10,2<=m<=9



输出
每组输出一行答案,如题意。

样例输入1 复制
b 2


样例输出1
1

主要是处理26进制转化吧


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
LL modl(LL a, LL b)        //快速幂取余a^b%c
{
LL res, t;
res = 1 ;
t = a ;
while (b)
{
if (b & 1)
{
res = res * t ;
}
t = t * t ;
b >>= 1;
}
return res;
}
int main()
{
LL n;
string s;
LL a[1000];
LL g;
while(cin>>s>>g)
{
memset(a,0,sizeof(a));
LL sum=0;
reverse(s.begin(),s.end());
for(int i=0;i<s.length();i++){
LL ans=(int)(s[i]-'0')-49;
sum+=(ans*modl(26,i));
//  cout<<sum<<endl;
}
//cout<<sum<<endl;
n=sum;
int i=0;
if(n==0){
cout<<"0";
}else{
while(n){
a[i]=n%g;
n/=g;
i++;
}
for(int j=i-1;j>=0;j--){
cout<<a[j];
}
}
cout<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐