UVA619 LA5465 POJ1312 HDU1314 ZOJ1272 Numerically Speaking大数+进制

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA619 LA5465 POJ1312 HDU1314 ZOJ1272 Numerically Speaking大数+进制相关的知识,希望对你有一定的参考价值。

Numerically Speaking
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1346 Accepted: 718

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

North Central North America 1997

问题链接UVA619 LA5465 POJ1312 HDU1314 ZOJ1272 Numerically Speaking
问题简述:(略)
问题分析:简单的二叉搜索树问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA619 LA5465 POJ1312 HDU1314 ZOJ1272 Numerically Speaking */

#include <iostream>
#include <cstdio>
#include <cctype>
#include <cstring>

using namespace std;

char s[128], num[128], ans[128];

int main()
{
    while (~scanf("%s", s) && strcmp(s, "*")) {
        int baseFrom, baseTo, len = strlen(s);
        if (isdigit(s[0])) {
            baseFrom = 10;
            baseTo = 26;
            for (int i = 0; i < len; i++)
                num[i] = s[len - 1 - i] - '0';
        } else {
            baseFrom = 26;
            baseTo = 10;
            for (int i = 0; i < len; i++)
                num[i] = s[len - 1 - i] - 'a' + 1;
        }

        int len2 = len * 2;
        for (int i = 0; i < len2; i++) {
            int d = 0;
            for (int j = len; j >= 0; j--){
                d = d * baseFrom +num[j];
                num[j] = d / baseTo;
                d = d % baseTo;
            }
            ans[i] = d;
            for (int j = 0; j < len2; j++)
                if (num[j] > baseFrom) {
                    num[j + 1] += num[j] / baseFrom;
                    num[j] %= baseFrom;
                }
        }
        int k = len2 - 1;
        while (ans[k] == 0) k--;
        if (baseTo == 10) printf("%-22s", s);
        for (int i = k; i >= 0; i--)
            if (baseTo == 10) {
                printf("%c", ans[i] + '0');
                if (i > 0 && i % 3 == 0) printf(",");
            } else
                printf("%c", ans[i] - 1 + 'a');

        if (baseTo == 26) {
            for (int j = k; j < 21; j++)
                printf(" ");
            for (int j = 0; j < len; j++) {
                printf("%c", s[j]);
                if (len - 1 > j && (len - j) % 3 == 1)
                    printf(",");
            }
        }
        printf("\\n");
    }

    return 0;
}

以上是关于UVA619 LA5465 POJ1312 HDU1314 ZOJ1272 Numerically Speaking大数+进制的主要内容,如果未能解决你的问题,请参考以下文章

UVA1445 LA4636 POJ3802 Cubist Artwork贪心

UVA275 LA5384 POJ1140 Expanding Fractions循环节

UVA12081 LA3413 POJ2769 Reduced ID Numbers同余

UVA326 LA5434 POJ1538 Extrapolation Using a Difference Table二项式

HDU4111 UVA1500 LA5760 Alice and BobSG函数+规律

2019年7月做题记录