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;
}

 

转载于:https://www.cnblogs.com/baidongtan/archive/2012/08/31/2665833.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值