输入只有一行,包含三个整数a,n,b。a表示其后的n 是a进制整数,b表示欲将a进制整数n转换成b进制整数。a,b是十进制整数,2 =< a,b <= 16。 数据可能存在包含前导零的情况。
可能有多组测试数据,对于每组数据,输出包含一行,该行有一个整数为转换后的b进制数。输出时字母符号全部用大写表示,即(0,1,...,9,A,B,...,F)。
15 Aab3 7
210306
//记得用do{ }while();这道题的测试数据没有0,所以用while也能通过测试。 //但是在电脑上0无法输出 #include<stdio.h> #include<string1.h> int main() { int a, b; char str[40]; while (scanf("%d%s%d", &a, str, &b) != EOF) { long temp = 0; int size = strlen(str); for (int i = 0; i < size; i++) { if (str[i] <= 'f'&&str[i] >= 'a') { temp = (str[i] - 'a') + 10 + temp * a; } else if (str[i] <= 'F'&&str[i] >= 'A') { temp = (str[i] - 'A') + 10 + temp * a; } else temp = (str[i]-'0') + temp * a; } char str2[40]; int p = 0; do{ if (temp%b >= 10) { str2[p++] = temp % b - 10 + 'A'; } else str2[p++] = temp % b + '0'; temp /= b; } while (temp); for (int i = --p; i >= 0; i--) { printf("%c", str2[i]); } printf("\n"); } return 0; }
#include<stdio.h>//本道题的难点主要解决abcd这些值 #include<string.h>//思想:善于利用数组的下标代表abcd这些值 #include<math.h> int main() { char a[17]="0123456789ABCDEF"; char b[100],c[100];int m,n,i,j,sum,num; scanf("%d%s%d",&m,b,&n); for(i=strlen(b)-1;i>=0;i--)//0.小写字母转换成大写字母 if(b[i]>='a'&&b[i]<='z') b[i]=b[i]-32; num=0;sum=0;//1.m进制转换成十进制 for(i=strlen(b)-1;i>=0;i--) for(j=0;j<17;j++) if(a[j]==b[i])//找下标做实际值 {sum+=j*pow(m,num++);break;} num=0;//2.sum转换成n进制 while(sum) { i=sum%n;//得到的余数做a的下标 c[num]=a[i];//根据下标找c[]的值 sum/=n;num++; } for(i=num-1;i>=0;i--)//倒序输出 printf("%c",c[i]); printf("\n"); }
#include<bits/stdc++.h>
char n[65];
int main(){
long s=0;
int a,b;
while(scanf("%d %s %d ",&a,n,&b)!=EOF){
int l=strlen(n);
for(int i=l-1;i>=0;i--)
if(n[i]>='0' && n[i]<='9')
s=s+(n[i]-'0')*pow(a,l-i-1);
else if(n[i]>='a' && n[i]<='f')
s=s+(n[i]-'a'+10)*pow(a,l-i-1);
else s=s+(n[i]-'A'+10)*pow(a,l-i-1);
for(int i=0;s;i++){
n[i]=s%b; s/=b; l=i;
}
for(int i=l;i>=0;i--)
if(n[i]>=10) printf("%c",n[i]-10+'A');
else printf("%d",n[i]);
printf("\n");
}
}
#include<bits/stdc++.h> using namespace std; int ChartoInt(char ch) { if(ch>='0'&&ch<='9') return ch-'0'; else if(ch>='a'&&ch<='b') return ch-'a'+10; else return ch-'A'+10; } char InttoChar(int num) { if(num>=0&&num<=9) return num+'0'; else return num-10+'A'; } int main() { int a,b;//a进制转化为b进制 string num; while(cin>>a>>num>>b) { long number=0; stack<char>result; for(int i=0;i<num.size();i++)//将a进制转换为十进制 number=number*a+ChartoInt(num[i]); while(number!=0)//将十进制转换为b进制 { result.push(InttoChar(number%b)); number/=b; } while(!result.empty()) { cout<<result.top(); result.pop(); } cout<<endl; } return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <vector> using namespace std; vector<char> myVector; int toInt(char c) { if (c >= '0' && c <= '9') { return c - '0'; } else if (c >= 'a' && c <= 'z') { return c - 'a' + 10; } else { return c - 'A' + 10; } } long convertToTen(string str, int a) { long res = 0; for (int i = 0; i < str.size(); i++) { res *= a; res += toInt(str[i]); } return res; } char toChar(int c) { if (c < 10) { return c + '0'; } else { return c - 10 + 'A'; } } void convertToN(long ret, int b) { if (ret == 0) { myVector.push_back('0'); } while (ret) { myVector.push_back(toChar(ret % b)); ret /= b; } for (int i = myVector.size() - 1; i >= 0; i--) { printf("%c", myVector[i]); } cout << endl; } int main() { int a, b; string str; while (cin >> a >> str >> b) { long ret = convertToTen(str, a); convertToN(ret, b); myVector.clear(); } return EXIT_SUCCESS; }
#include <stdio.h> #include <string.h> char IntToChar(int x) { if(x<10) return x+'0'; else return x-10+'A'; } int CharToInt(char c){ if(c>='0'&&c<='9') return c-'0'; else if(c>='A'&&c<='Z') return c-'A'+10; else return c-'a'+10; } int main() { char s[100],ans[100]; int m,n; int len,i; long long num; while(scanf("%d %s %d",&m,&s,&n)!=EOF) { len=strlen(s); for(num=0,i=0;i<len;i++) { num*=m; num+=CharToInt(s[i]); } i=0; while(num>0) { ans[i++]=IntToChar(num%n); num/=n; } while(i>0) printf("%c",ans[--i]); printf("\n"); } }
比较简单,注意大小写即可。
#include <bits/stdc++.h> using namespace std; int main() { int a,b; string n; while(cin>>a>>n>>b) { long num=0; string result=""; while(n[0]=='0') n=n.substr(1); int k=1; for(int i=n.length()-1;i>=0;--i) { if(n[i]>='0'&&n[i]<='9') num+=(n[i]-'0')*k; else if(n[i]>='a'&&n[i]<='z') num+=(n[i]-'a'+10)*k; else if(n[i]>='A'&&n[i]<='Z') num+=(n[i]-'A'+10)*k; k*=a; } while(num!=0) { if(num%b<=9) result=to_string(num%b)+result; else result=string(1,num%b-10+'A')+result; num/=b; } cout<<result<<endl; } return 0; }
#include <iostream> using namespace std; int main() { long long a, b, n; char s[0x71], *p; while (cin >> a >> (p = s + 0x30) >> b) { for (n = 0; *p; ++p) n = n * a + *p - (*p < 'A' ? '0' : *p < 'a' ? 'A' - 10: 'a' - 10); for (p -= !n; n; n /= b) *p += (*--p = n % b) > 9 ? 'A' - 10 : '0'; cout << p << endl; } return 0; }
#include<stdio.h> (737)#include<string.h> char s[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; void help(char* ret, int a, char* ans, int b) { int len=strlen(ret), cnt = 0, k = 0, t; for(int i = 0; i < len; ) { k = 0; for(int j = i; j < len; j++) { if(ret[j] >= 'A' && ret[j] <= 'F') { t = (k*a+ret[j]-'A'+10); ret[j] = s[t/b]; k = t%b; } else { t = (k*a+ret[j]-'0'); ret[j] = s[t/b]; k = t%b; } } ans[cnt++] = s[k]; while(ret[i] == '0') i++; } ans[cnt]='\0'; return ; } int main() { int a, b,len; char ret[1000], ans[1000]; while(scanf("%d %s %d", &a, ret, &b)!=EOF) { len = strlen(ret); for(int i = 0; i < len; i++) if(ret[i] >= 'a' && ret[i] <= 'z') ret[i] -= 32; help(ret,a,ans,b); len = strlen(ans); for(int i = len-1; i > -1; i--) printf("%c", ans[i]); printf("\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; map<char,int>m; vector<char>result; int sum=0; int x_to_ten(int a,string n){ int j=0; for(auto i=n.rbegin() ;i!=n.rend();i++){ if(*i!=0){ if(*i>='A') sum+=m[*i]*pow(a,j); else sum+=(*i-'0')*pow(a,j); } j++; } return sum; } void ten_to_x(int b,int n){ while(n!=0){ if(n%b<10) result.push_back(n%b+'0'); else result.push_back((char)(n%b+55)); n/=b; } for(int i=result.size()-1;i>=0;i--){ cout<<result[i]; } } int main(){ string n; int a,b; m['A']=10;m['a']=10; m['B']=11;m['b']=11; m['C']=12;m['c']=12; m['D']=13;m['d']=13; m['E']=14;m['e']=14; m['F']=15;m['f']=15; cin>>a>>n>>b; ten_to_x(b,x_to_ten(a,n)); //cout<<x_to_ten(a,n); return 0; }
又臭又长
#include<string>
using namespace std;
string change(int a, string n, int b){
long dec = 0;
long temp = 1;
for(int i = n.length() - 1; i >= 0; i--){
if(n[i] >= 'A' && n[i] <= 'F'){
dec += temp * (n[i] - 'A' + 10);
}
else if(n[i] >= 'a' && n[i] <= 'f'){
dec += temp * (n[i] - 'a' + 10);
}
else{
dec += temp * (n[i] - '0');
}
temp *= a;
}
string ret = "";
while(dec){
temp = dec % b;
if(temp > 9){
ret += (char)(temp - 10 + 'A');
}
else{
ret += (char)(temp + '0');
}
dec /= b;
}
return ret;
}
int main(){
int a, b;
string n;
while(cin >> a >> n >> b){
string ret = change(a, n, b);
for(int i = ret.length() - 1; i >= 0; i--){
cout << ret[i];
}
cout << endl;
}
return 0;
}
#include <cstdio> #include <iostream> #include <cmath> #include <vector> using namespace std; int to; vector<int> sto; int k; long toTen(string s){ long sum = 0; for(int i =s.length()-1,j=0;i >= 0;i--,j++){ if(s[i]>='0'&&s[i]<='9') sum+=(s[i]-'0')*pow(k,j); else if(s[i]>='A'&&s[i]<='F') sum+=(s[i]-'A'+10)*pow(k,j); else if(s[i]>='a'&&s[i]<='f') sum+=(s[i]-'a'+10)*pow(k,j); } return sum; } void toStr(long num){ while(num!=0){ sto.push_back(num%to); num/=to; } } int main(){ cin>>k; string s; cin>>s; long numTen = toTen(s); //cout<<numTen; cin>>to; toStr(numTen); for(int i = sto.size()-1;i >= 0 ;i--){ if(sto[i]>9) cout<<(char)(sto[i]-10+'A'); else cout<<sto[i]; } return 0; }
#include<iostream> #include<algorithm> #include<string> #include<cstring> using namespace std; int main(){ int a,b; string str; char ch[30]; int num[30]; while(cin>>a>>str>>b){ long total=0; strcpy(ch,str.c_str()); for(int i=0;i<str.length();i++){ if(ch[strlen(ch)-1-i]>='a'&&ch[strlen(ch)-1-i]<='f') total+=(ch[strlen(ch)-1-i]-'a'+10)*pow(a,i); else if(ch[strlen(ch)-1-i]>='A'&&ch[strlen(ch)-1-i]<='F') total+=(ch[strlen(ch)-1-i]-'A'+10)*pow(a,i); else if(ch[strlen(ch)-1-i]>='0'&&ch[strlen(ch)-1-i]<='9') total+=(ch[strlen(ch)-1-i]-'0')*pow(a,i); } int temp=0; while(total>0){ num[temp++]=total%b; total/=b; } for(int i=temp-1;i>=0;i--){ if(num[i]>=10) cout<<(char)(num[i]-10+'A'); else cout<<num[i]; } cout<<endl; } }
#include<iostream> #include<math.h> #include<algorithm> #include<string> using namespace std; int main(){ int charTonumber[256]; for(int i='0';i<='9';i++){ charTonumber[i]=i-'0'; } for(int i='a';i<='f';i++){ charTonumber[i]=i-'a'+10; } for(int i='A';i<='F';i++){ charTonumber[i]=i-'A'+10; } char numberToChar[20]; for(int i=0;i<=9;i++){ numberToChar[i]=i+'0'; } for(int i=10;i<16;i++){ numberToChar[i]=i-10+'A'; } int beforeChange; string number; int afterChange; while(cin>>beforeChange>>number>>afterChange){ int changeNumber=0; for(int i=0;i<number.size();i++){ changeNumber+=charTonumber[number.at(i)]*(int)pow(beforeChange+0.0,number.size()-1.0-i); } string result; while(changeNumber>0){ int temp=changeNumber%afterChange; result+=numberToChar[temp]; changeNumber/=afterChange; } reverse(result.begin(),result.end()); cout<<result<<endl; } return 0; }
while True: try: baseChar = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" numInput = input().split() a = int(numInput[0]) b = int(numInput[2]) base10 = 0 temp = numInput[1].upper() index = 0 while temp: #先转成10进制 base10 += baseChar.index(temp[-1])*a**index index += 1 temp = temp[:-1] result = [] while base10 > 0: #再转成b进制 base10, index = divmod(base10, b) result.append(baseChar[index]) print("".join(result[::-1])) except Exception: break
运行时间:3ms
占用内存:384k