POJ 1312 Numerically Speaking

Posted

tags:

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

Numerically Speaking

 

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
*

samp 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

思路:可以看成是十进制到二十六进制的转化,包含了大整数的加减乘除,全程用手绘算法。。。

代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
char a[1000];
int b[1000];
int num[1005];
char d[1005];
char z[1005];
void fun1()
{
    memset(num, 0, sizeof(num));
    int O;
    for(int i = strlen(a) - 1; i >= 0; i--)
    {
        int o = 1000;
        int len = 0;
        b[0] = a[i] - a + 1;
        int rem = 0;
        for(int k = 0; k <= strlen(a) - 1 - i; k++)
        {
            for(int j = 0; j <= len; j++)
            {
                rem += b[j]*26;
                if(k == 0)rem /= 26;
                b[j] = rem%10;
                rem = rem/10;
            }
            while(rem != 0)
            {
                b[++len] = rem%10;
                rem = rem/10;
            }
        }
        int l = 0;
        int sum = 0;

        while(l <= len)
        {
            sum += num[o] + b[l];
            num[o] = sum%10;
            sum = sum/10;

            o--;
            l++;
            if(l == len+1 && sum != 0){num[o] = sum%10;o--;}
        }
        O = o;

    }
    for(int i = 0; i < strlen(a); i++)
    cout << a[i];
    for(int i = strlen(a); i < 22; i++)
    cout <<  ;
    int ji = (1000 - (O))%3;
    int j = 0;
    for(int q = O+1; q <= 1000; q++)
    {
        j++;
        cout << num[q];
        if(1000 - O > 3)
        {
            if(j%3 == 0 && q != 1000)cout << ,;
            if(j == ji){cout << ,;j = 0;ji = -1;}
        }

    }
    cout << endl;
}
void fun2()
{
    memset(z, 0, sizeof(z));
    memset(d, 0, sizeof(d));
    int len = strlen(a);
    strcpy(z, a);
    int j = 0;
    int bu = 0;
    while(1)
    {
        int sum = 0;
        int s = 0;
        int haha = 0;
        for(int i = 0; i < len; i++)
        {
            sum += a[i] - 48;
            int temp = sum/26;
            if(haha == 0 && temp != 0)haha = 1;
            if(haha == 1)a[s++] = temp + 48;
            sum = sum%26;
            sum *= 10;
        }
        len = s;
        sum /= 10;
        if(sum <= 0){d[j] = 26 + a -  1; bu = -1;j++;}
        else {d[j++] = sum + a - 1 + bu;bu = 0;}
        if(len <= 0)
        break;
    }
    for(int i = j-1; i >= 0; i--)
    cout << d[i];
    for(int i = 0; i < 22-j; i++)
    {
        cout <<  ;
    }
    int ji = 0;
    int ans = strlen(z)%3;
    for(int i = 0; i < strlen(z); i++)
    {
        ji++;
        cout << z[i];
        if(strlen(z) > 3)
        {
            if(ji%3 == 0 && i != strlen(z) - 1)cout << ,;
            if(i+1 == ans && ans != 0){cout << ,;ji = 0;}

        }
    }
    cout << endl;
}
int main()
{
    while(cin >> a)
    {
        if(a[0] == *)break;
        if(a[0] >= 0 && a[0] <= 9)fun2();
        else if(a[0] >= a && a[0] <= z)fun1();
    }
    return 0;
}

 

以上是关于POJ 1312 Numerically Speaking的主要内容,如果未能解决你的问题,请参考以下文章

poj 1312

Bzoj1312 / POJ3155 Neerc2006 Hard Life

HDU 1314 Numerically Speaking(大数加减乘除+另类二十六进制互相转换)

Warning message:“glm.fit: fitted probabilities numerically 0 or 1 occurred“

MOPSO 多目标例子群优化算法

getField方法