zoukankan      html  css  js  c++  java
  • POJ1220(大数进制转换)

    NUMBER BASE CONVERSION
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 4652   Accepted: 2132

    Description

    Write a program to convert numbers in one base to numbers in a second base. There are 62 different digits: 
    { 0-9,A-Z,a-z } 
    HINT: If you make a sequence of base conversions using the output of one conversion as the input to the next, when you get back to the original base, you should get the original number. 

    Input

    The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines will have a (decimal) input base followed by a (decimal) output base followed by a number expressed in the input base. Both the input base and the output base will be in the range from 2 to 62. That is (in decimal) A = 10, B = 11, ..., Z = 35, a = 36, b = 37, ..., z = 61 (0-9 have their usual meanings). 

    Output

    The output of the program should consist of three lines of output for each base conversion performed. The first line should be the input base in decimal followed by a space then the input number (as given expressed in the input base). The second output line should be the output base followed by a space then the input number (as expressed in the output base). The third output line is blank. 

    Sample Input

    8
    62 2 abcdefghiz
    10 16 1234567890123456789012345678901234567890
    16 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
    35 23 333YMHOUE8JPLT7OX6K9FYCQ8A
    23 49 946B9AA02MI37E3D3MMJ4G7BL2F05
    49 61 1VbDkSIMJL3JjRgAdlUfcaWj
    61 5 dl9MDSWqwHjDnToKcsWE1S
    5 10 42104444441001414401221302402201233340311104212022133030
    

    Sample Output

    62 abcdefghiz
    2 11011100000100010111110010010110011111001001100011010010001
    
    10 1234567890123456789012345678901234567890
    16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
    
    16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
    35 333YMHOUE8JPLT7OX6K9FYCQ8A
    
    35 333YMHOUE8JPLT7OX6K9FYCQ8A
    23 946B9AA02MI37E3D3MMJ4G7BL2F05
    
    23 946B9AA02MI37E3D3MMJ4G7BL2F05
    49 1VbDkSIMJL3JjRgAdlUfcaWj
    
    49 1VbDkSIMJL3JjRgAdlUfcaWj
    61 dl9MDSWqwHjDnToKcsWE1S
    
    61 dl9MDSWqwHjDnToKcsWE1S
    5 42104444441001414401221302402201233340311104212022133030
    
    5 42104444441001414401221302402201233340311104212022133030
    10 1234567890123456789012345678901234567890
    

    Source

     
    直接上模板
    /*
    ID: LinKArftc
    PROG: 1220.cpp
    LANG: C++
    */
    
    #include <map>
    #include <set>
    #include <cmath>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <cstdio>
    #include <string>
    #include <utility>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define eps 1e-8
    #define randin srand((unsigned int)time(NULL))
    #define input freopen("input.txt","r",stdin)
    #define debug(s) cout << "s = " << s << endl;
    #define outstars cout << "*************" << endl;
    const double PI = acos(-1.0);
    const double e = exp(1.0);
    const int inf = 0x3f3f3f3f;
    const int INF = 0x7fffffff;
    typedef long long ll;
    
    const int maxn = 1000;
    char str[maxn];//输入字符串
    int start[maxn], ans[maxn], res[maxn];//被除数,商,余数
    int oldBase, newBase;//转换前后进制
    
    //单个字符得到数字
    int getNum(char c) {//这里的进制字符是先数字,后大写字母,后小写字母
        if (c >= '0' && c <= '9') return c - '0';//数字
        if (c >= 'A' && c <= 'Z') return c - 'A' + 10;//大写字母
        return c - 'a' + 36;
    }
    //数字得到字符
    char getChar(int i) {
        if (i >= 0 && i <= 9) return i + '0';
        if (i >= 10 && i <= 35) return i - 10 + 'A';
        return i - 36 + 'a';
    }
    //把输入的字符串的各个数位还原成数字形式
    void change() {
        start[0] = strlen(str);//数组的0位存的是数组长度
        for (int i = 1; i <= start[0]; i ++) start[i] = getNum(str[i-1]);
    }
    
    void solve() {
        memset(res, 0, sizeof(res));//余数位初始化为空
        int y, i, j;
        while (start[0] >= 1) {
            y = 0; i = 1;
            ans[0] = start[0];
            while (i <= start[0]) {
                y = y * oldBase + start[i];
                ans[i ++] = y / newBase;
                y %= newBase;
            }
            res[++ res[0]] = y;//这一轮得到的余数
            i = 1;//找下一轮商的起始处,去掉前面的0
            while ((i <= ans[0]) && (ans[i] == 0)) i ++;
            memset(start, 0, sizeof(start));
            for (j = i; j <= ans[0]; j ++) start[++ start[0]] = ans[j];
            memset(ans, 0, sizeof(ans));
        }
    }
    
    void output() {//从高位到低位逆序输出
        for (int i = res[0]; i >= 1; i --) printf("%c", getChar(res[i]));
        printf("
    ");
    }
    
    int main() {
        //input;
        int T;
        scanf("%d", &T);
        while (T --) {
            scanf("%d %d %s", &oldBase, &newBase, str);
            printf("%d %s
    ", oldBase, str);
            change();
            solve();
            printf("%d ", newBase);
            output();
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    Linux系统管理05-----权限及归属管理
    Linux系统安装管理04----账号管理
    Linux系统管理03-----安装与管理程序
    Zabbix 监控主机
    Zabbix 页面优化
    基于 MHA 的MySQL高可用-CentOS7(理论)
    基于 MHA 的MySQL高可用-CentOS7(实例)
    部署Jumpserver环境
    GNS3连接本地服务器报错
    zabbix 安装部署
  • 原文地址:https://www.cnblogs.com/LinKArftc/p/4907629.html
Copyright © 2011-2022 走看看