• 任意进制转化/模板(c++/ java)


    http://www.tzcoder.cn/acmhome/problemdetail.do?&method=showdetail&id=6198

    c++

    #include<bits/stdc++.h>
    using namespace std;
    #define MAXN  3005
    int  t[MAXN],A[MAXN];
    char OldData[MAXN], NewData[MAXN],oldData[MAXN];
    int olds, news;
    void trans()
    {
        int i, len, k;
        memset(t,0,sizeof t);
        memset(A,0,sizeof A);
        len = strlen(OldData);
        for(i=len; i>=0; --i)
            t[len-1-i] = OldData[i] - (OldData[i]<58 ? 48 : OldData[i]<97 ? 55 : 61);
        for(k=0; len;)
        {
            for(i=len; i>=1; --i)
            {
                t[i-1] += t[i]%news*olds;
                t[i] /= news;
            }
            A[k++] = t[0] % news;
            t[0] /= news;
            while(len>0 && !t[len-1]) --len;
        }
        NewData[k] = '';
        for(i=0; i<k; ++i)
            NewData[k-1-i] = A[i] + (A[i]<10 ? 48 : A[i]<36 ? 55 : 61);
    }
    int main()
    {
      //  for(int i=1;i<=2000;i++)cout<<1;
        while(cin>>oldData>>olds>>news)
        {
    
            if(oldData[0]=='-'){
                int l=strlen(oldData);
                for(int i=1;i<l;i++)OldData[i-1]=oldData[i];
                OldData[l-1]='';
                trans();
                if(strcmp(NewData,"0")!=0)cout<<"-"<<NewData<<endl;
                else cout<<0<<endl;
            }
            else {
                strcpy(OldData,oldData);
                trans();
                cout<<NewData<<endl;
            }
        }
    
    }
    View Code

     java:

    import java.io.*;
    import java.math.BigInteger;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Scanner;
    import java.util.StringTokenizer;
    import java.io.IOException;
    import java.io.InputStream;
    
    public class Main {
    
        public static void main(String args[]) {
            Scanner sc = new Scanner(System.in);
            while(sc.hasNext()) {
                String s = sc.next();
                int r1 = sc.nextInt();
                int r2 = sc.nextInt();
                String a = new BigInteger(s,r1).toString(r2);
                System.out.println(a.toUpperCase());
            }
        }
    
    }
    View Code
  • 相关阅读:
    索引与完整性约束(note6)
    数据库查询语句(note4)
    select 查询(note3)
    数据库创建(note2)
    mysql(note1)
    day 05
    day 04 字符串
    博文索引
    ubuntu+xen的编译
    hexo+github搭建个人网站
  • 原文地址:https://www.cnblogs.com/ydw--/p/12613801.html
Copyright © 2020-2023  润新知