蓝桥的一题进制转换http://blog.csdn.net/laichilizi/article/details/79381732
#include<bits/stdc++.h> using namespace std; void Arbitrary_change(int n,int m){ char s[100]; int i; for(i=0;n>0;i++) { if(n%m<10) s[i] = n%m+'0'; else s[i] = n%m-10+'A'; n/=m; } if(m == 8) { printf("Octal: "); int temp = i; while(temp++<4) printf("0"); } else if(m == 16) { printf("Hex: 0x"); int temp = i; while(temp++<3) printf("0"); } for(n=i;n>0;n--) printf("%c",s[n-1]); printf(" "); } int Ten_change(int n,string str) { int ans = 0, sum = 1; for(int i = str.length(); i > 0; i--) { if(str[i-1] >='A') ans = ans + (str[i - 1] - 'A'+10) * sum; else ans = ans + (str[i - 1] - '0') * sum; //cout<<ans<<endl; sum *= n; } return ans; //printf("%d ", ans); } int main() { string str; cin>>str; int ans2 = Ten_change(16,str); Arbitrary_change(ans2,16); cout<<"Decimal: "<<ans2<<endl; Arbitrary_change(ans2,8); return 0;