zoukankan      html  css  js  c++  java
  • hdu Numerically Speaking

    Numerically Speaking

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 163    Accepted Submission(s): 97

    Problem Description
    A developer of crossword puzzles (and other similar word games) has decided to develop a mapping between every possible word with from one to twenty characters and unique integers. The mapping is very simple, with the ordering being done first by the length of the word, and then alphabetically. Part of the list is shown below. a 1 b 2 ... z 26 aa 27 ab 28 ... snowfall 157,118,051,752 ...
    Your job in this problem is to develop a program which can translate, bidirectionally, between the unique word numbers and the corresponding words.
     
    Input
    Input to the program is a list of words and numbers, one per line starting in column one, followed by a line containing a single asterisk in column one. A number will consist only of decimal digits (0 through 9) followed immediately by the end of line (that is, there will be no commas in input numbers). A word will consist of between one and twenty lowercase alphabetic characters (a through z).
     
    Output
    The output is to contain a single line for each word or number in the input data. This line is to contain the word starting in column one, followed by an appropriate number of blanks, and the corresponding word number starting in column 23. Word numbers that have more than three digits must be separated by commas at thousands, millions, and so forth.
     
    Sample Input
    29697684282993 transcendental 28011622636823854456520 computationally zzzzzzzzzzzzzzzzzzzz *
     
    Sample Output
    elementary 29,697,684,282,993 transcendental 51,346,529,199,396,181,750 prestidigitation 28,011,622,636,823,854,456,520 computationally 232,049,592,627,851,629,097 zzzzzzzzzzzzzzzzzzzz 20,725,274,851,017,785,518,433,805,270
     
    Source
     
    Recommend
    Eddy
     
    分析:时隔一年再做这题,因为未清空数组还是没有1Y。但比其当年的不敢下手,自觉还是懂了挺多,大数模板也比较熟练了。
    关于这道题OJ的数据普遍很弱,网上很多AC代码在输入26时不是得到z而是a`,这种情况是由于直接理解为了将10进制转化为26进制。其实不然。因为对于任意n进制,每位上的数都是0~n-1,但这题是1~26。所以,博主做法是,对26取模后,若为0,则将此位定为26,并把除以26的商-1后再进行下一步。
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    int a[40], b[40], ca[40];
    
    void add(int s[], int n) {
        int t = 0, i;
        s[39] += n;
        for (i = 39; i >= 0; --i) {
            s[i] += t;
            t = s[i] / 10;
            s[i] %= 10;
        }
    }
    
    void mul(int s[], int n) {
        int t = 0, i;
        for (i = 39; i >= 0; --i) {
            s[i] = s[i] * n + t;
            t = s[i] / 10;
            s[i] %= 10;
        }
    }
    
    void sub(int s[], int n) {
        int t = 0, i;
        s[39] -= n;
        for (i = 39; i >= 0; --i) {
            if (s[i] >= 0) {
                break;
            } else {
                s[i + 1]--;
                s[i] += 10;
            }
        }
    }
    
    int div_mod(int s[], int n) {
        int t = 0, i;
        for (i = 0; i < 40; ++i) {
            s[i] += 10 * t;
            t = s[i] % n;
            s[i] /= n;
        }
        return t;
    }
    
    int is_zero(int s[]) {
        int i;
        for (i = 0; i < 40; ++i)
            if (s[i])
                return 0;
        return 1;
    }
    char ss[40];
    
    int main() {
        int le, i, ans;
        while (scanf("%s", ss) != EOF) {
            if (ss[0] == '*')
                break;
            le = strlen(ss);
            memset(a, 0, sizeof (a));
            memset(b, 0, sizeof (b));
            memset(ca, 0, sizeof (ca));
            if (ss[0] <= '9' && ss[0] >= '0') {
                for (i = 39; le > 0; --i)
                    ca[i] = a[i] = ss[--le] - '0';
    
                for (i = 39; i >= 0; --i) {
                    ans = div_mod(a, 26);
                    if (!ans) {
                        b[i] = 26;
                        sub(a, 1);
                    } else
                        b[i] = ans;
                    if (is_zero(a))
                        break;
                }
                for (i = 0; i < 40; ++i)
                    a[i] = ca[i];
            } else if (ss[0] <= 'z' && ss[0] >= 'a') {
                for (i = 39; le > 0; --i)
                    b[i] = ss[--le] - 'a' + 1;
                for (i = 0; i < 40; ++i) {
                    mul(a, 26);
                    add(a, b[i]);
                }
            }
            for (i = 0; i < 40; ++i)
                if (b[i])
    
                    break;
            ans = i;
            for (; i < 40; ++i)
                printf("%c", b[i] + 'a' - 1);
            ans -= 18;
            while (ans--)
                printf(" ");
            for (i = 0; i < 40; ++i)
                if (a[i])
                    break;
            for (; i < 40; ++i) {
                printf("%c", a[i] + '0');
                if (i < 39 && i % 3 == 0)
                    printf(",");
            }
            printf("\n");
        }
        return 0;
    }
  • 相关阅读:
    360手机青春版原装充电器
    然并卵却很拉风!360超级充电器评测_天极网
    39元超值!360超级充电器拆解与评测
    99元紫米10000mAh移动电源开箱拆解
    自己DIY PCB电路板制作(简单方便快捷)
    Easy EDA:在线创客软件服务是未来的趋势 | 雷锋网
    EasyEDA
    我的那些年(9)~我来团队了,Mvc兴起了
    我的那些年(8)~去朋友公司做网站开发
    我的那些年(7)~第一份互联网工作
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2665833.html
Copyright © 2011-2022 走看看