数学问题-进制转换

模板:

struct nlist{
    vector<int> v;
    int r;
    nlist(){}
    nlist(string s,int r):r(r){
        for(int i=0;i){
            v.push_back(s[i]-48);
        }
    }
    print(){
        for(int i=0;i){
            printf("%d ",v[i]);
        }
        puts("");
    }
}; 

void converse(const nlist& a,nlist& b,int r){
    int mul=1,i,sum=0,t;
    for(i=a.v.size()-1;i>=0;i--){
        sum+=mul*a.v[i];
        mul*=a.r;
    }
    while(sum){
        t=sum%r;
        b.v.push_back(t);
        sum/=r;
        i++;
    }
    reverse(b.v.begin(),b.v.end());
}

 


 

1.负数的进制

#include 
#include 
#include 
#include <string.h>
#include <string>
#include 
#include <set>
#include 
#include 
#include 
#include 

#define I scanf
#define OL puts
#define O printf
#define F(a,b,c) for(a=b;a#define FF(a,b) for(a=0;a#define FG(a,b) for(a=b-1;a>=0;a--)
#define LEN 100
#define MAX 0x06FFFFFF
#define V vector

using namespace std;


int main(){
//    freopen("进制转换.txt","r",stdin);
    int n,r;
    I("%d%d",&n,&r);
    printf("%d",n);
    string ans;
    while(n){
        int t=n%r;
        n/=r;
        if(t<0){
            t-=r;
            n+=1; 
        }
        if(t>9){
            ans+='A'+t-10;
        }else{
            ans+='0'+t;
        }
    }
    reverse(ans.begin(),ans.end());
    printf("=%s(base%d)",ans.c_str(),r);
    return 0;
    
    
}
View Code

 

转载于:https://www.cnblogs.com/TQCAI/p/8453951.html

你可能感兴趣的:(数学问题-进制转换)