题目1080:进制转换(任意进制直接转换方法)
题目链接:http://ac.jobdu.com/problem.php?pid=1080
详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus
参考代码:
// // 1080 进制转换.cpp // Jobdu // // Created by PengFei_Zheng on 10/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. // #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <cmath> using namespace std; int m,n; char x[10010]; int main(){ while(scanf("%d%d",&m,&n)!=EOF){ int from[10010]={0}; int to[10010]={0}; scanf("%s",x); int len = (int)strlen(x); for(int i = 0; i < len ; i++){ if(x[i]>='0' && x[i]<='9') from[i] = x[i]-'0'; else from[i] = x[i]-'A'+10; } int size = 0; while(true){ int i = 0 ; while(i<len&&from[i]==0) i++; if(i==len) break; int remain = 0; for(; i < len ; i++){ int tmp = from[i] + remain * m ; from[i] = tmp/n; remain = tmp%n; } to[size++] = remain; } if(size==0) printf("%d\n",0); else { for(int i = size - 1 ; i >=0 ; i-- ){ if(to[i]>9) printf("%c",to[i]-10+'a'); else printf("%c",to[i]+'0'); } printf("\n"); } } return 0; } /************************************************************** Problem: 1080 User: zpfbuaa Language: C++ Result: Accepted Time:50 ms Memory:1532 kb ****************************************************************/
作者: 伊甸一点
出处: http://www.cnblogs.com/zpfbuaa/
本文版权归作者伊甸一点所有,欢迎转载和商用(须保留此段声明),且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文链接 如有问题, 可邮件(zpflyfe@163.com)咨询.