王道机试指南6.1进制转换

例题6.1二进制数(北京邮电大学上机)

#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
int main(){
    int a;
    stack<int> result;
    while(cin >> a){
        while(a > 0){
             result.push(a % 2);
            a/=2;
        }
       while(!result.empty()){
               cout << result.top();
           result.pop();
       }
        cout << endl;  
    }
    )
    return 0;
}

例题6.2进制转换(清华大学上机)

#include<iostream>
#include<cstdio>
#include<stack>
#include<string>
using namespace std;
string divid(string s){
    string re;
    int a = (s[0] - '0') / 2;
    int current = (s[0] - '0') % 2;
    re = to_string(a);
    for(int i = 1; i < s.size(); i++){
        a = (current * 10 + s[i] - '0') / 2;
        re += to_string(a);
        current = (s[i] - '0') % 2;
    }
    if(re[0] == '0') re = re.substr(1, re.size());
    return re;
}
int main(){
    string s;
    stack<int> st;
    while(cin >> s){
        if( s == "0"){
            cout << 0 << endl;
            continue;
        }
        while(s.size() != 0){
            if((s[s.size()-1] - '0') % 2 == 0) st.push(0);
            else st.push(1);
            s = divid(s);
        }
        while(!st.empty()){
            cout << st.top();
            st.pop();
        }
        cout << endl;
    }
    return 0;
}  

例题6.3十进制与二进制(清华复试上机)

#include<iostream>
#include<cstdio>
#include<stack>
#include<string>
#include<vector>
using namespace std;
string multiple(string str, int x){
    int carry = 0;
    for(int i = str.size() - 1; i >= 0; i--){
        int current = x * (str[i] - '0') + carry;
        str[i] = current % 10 + '0';
        carry = current / 10;
    }
    if(carry != 0){
        str = "1" + str;
    }
    return str;
}
string add1(string str, int x){
    int carry = x;
    for(int i = str.size() - 1; i >= 0; i--){
        int current = (str[i] - '0') + carry;
        str[i] = current % 10 + '0';
        carry = current / 10;
    }
    if(carry != 0){
        str = '1' + str;
    }
    return str;
}
string divid(string s){
    string re;
    int a = (s[0] - '0') / 2;
    int current = (s[0] - '0') % 2;
    re = to_string(a);
    for(int i = 1; i < s.size(); i++){
        a = (current * 10 + s[i] - '0') / 2;
        re += to_string(a);
        current = (s[i] - '0') % 2;
    }
    if(re[0] == '0') re = re.substr(1, re.size());
    return re;
}
int main(){
    string s, re;
    vector<int> binary;
    while(cin >> s){
        if( s == "0"){
            cout << 0 << endl;
            continue;
        }
        while(s.size() != 0){
            if((s[s.size()-1] - '0') % 2 == 0) binary.push_back(0);
            else binary.push_back(1);
            s = divid(s);
        }
//        while(!st.empty()){
//            re += st.top();
////            cout << st.top();
//            st.pop();
//        }
        string ans = "0";
        for(int i = 0; i < binary.size(); i++){
            ans = multiple(ans, 2);
            ans = add1(ans, binary[i]);
        }
        cout << ans << endl;
    }
    return 0;
}  

例题6.4进制转换2(清华大学上机)

#include<iostream>
#include<cstdio>
#include<stack>
#include<cctype>
using namespace std;
long long toten(string x, int m){
    long long sum = 0, a;
    for(int i = 0; i < x.size(); i++){
        if(isalpha(x[i])){
            a = x[i] - 'A' + 10;
        }else {
            a = x[i] - '0';
        }
        sum *= m;
        sum += a;
    }
    return sum;
}
void ton(long long x, int n){
    stack<int> st;
    while(x != 0){
        st.push(x % n);
        x /= n;
    }
    while(!st.empty()){
        cout << st.top();
        st.pop();
    }
}
int main(){
    int m, n;
    string x;
    cin >> m >> n >> x;
    long long ten = toten(x, m);
    ton(ten, n);
    return 0;
}  

习题6.1八进制(华中科技大学)

#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
int main(){
    int n;
    while(cin >> n){
        stack <int> st;
        while(n != 0){
            st.push(n % 8);
            n /= 8;
        }
        while(!st.empty()){
            cout << st.top();
            st.pop();
        }
        cout << endl;
    }
    return 0;
}  

习题6.2有一版A+B

#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
int main(){
    int m, a, b;
    while(cin >> m){
        if(m == 0) break;
        cin >> a >> b;
        long long sum = a + b;
        if(sum == 0){
            cout << 0;
            continue;
        }
        stack<int> st;
        while(sum != 0){
            st.push(sum % m);
            sum /= m;
        }
        while(!st.empty()){
            cout << st.top();
            st.pop();
        }
    }
    return 0;
}  

习题6.3进制转换(北京大学复试上机)

#include<iostream>
#include<cstdio>
#include<stack>
#include<string>
#include<cctype>
using namespace std;
int chartoint(char c){
    if(isalpha(c)){
        return c - 'A' + 10;
    }else{
        return c - '0';
    }
}
int main(){
    string s;
    while(cin >> s){
        int sum = 0;
        for(int i = 2; i < s.size(); i++){
            sum *= 16;
            sum += chartoint(s[i]);
        }
        cout << sum << endl;
    }
    return 0;
}  

习题6.4数制转换(北京大学复试上机)

#include<iostream>
#include<cstdio>
#include<stack>
#include<string>
#include<cctype>
using namespace std;

string delete0(string s){
    int flag = 0;
    for(int i = 0; i < s.size(); i++){
        if(s[i] == '0'){
            flag++;
            continue;
        } else break;
    }
    return s.substr(flag, s.size() - flag);
}
int chartoint(char c){
    if(islower(c)) return c - 'a' + 10;
    else if(isupper(c)) return c - 'A' + 10;
    else return c - '0';
}
int main(){
    long  a, b;
    string n;
    stack<char> st;
    cin >> a >> n >> b;
    n = delete0(n);
    long temp = 0;
    for(int i = 0; i < n.size(); i++){
        temp *= a;
        temp += chartoint(n[i]);
    }
    while(temp != 0){
        if(temp % b >= 10){
            st.push(temp % b - 10 + 'A');
        }else{
            st.push(temp % b  + '0');
        }
        temp /= b;
    }
    while(!st.empty()){
        cout << st.top();
        st.pop();
    }
    return 0;
}