博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu Numerically Speaking
阅读量:5300 次
发布时间:2019-06-14

本文共 4466 字,大约阅读时间需要 14 分钟。

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
#include
#include
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

你可能感兴趣的文章
电脑的自带图标的显示
查看>>
globalization与全球化
查看>>
[转载] redis 的两种持久化方式及原理
查看>>
关于在Idea 创建Maven项目时,无法在source文件下创建servlet文件问题解决!
查看>>
对 HTTP 304 的理解
查看>>
深入理解css中的margin属性
查看>>
C++ 删除字符串的两种实现方式
查看>>
电容选型
查看>>
ORA-01502: 索引'P_ABCD.PK_WEB_BASE'或这类索引的分区处于不可用状态
查看>>
Spring EL hello world实例
查看>>
百度地图API地理位置和坐标转换
查看>>
MyBatis学习总结(六)——调用存储过程
查看>>
code-代码平台服务器路径
查看>>
离线安装 Visual Studio Express 而不下载整个镜像文件的方法(转载)
查看>>
2017-2018-2偏微分方程复习题解析10
查看>>
Java抽象类和接口的比较
查看>>
iOS UI控件5-UIPickerView
查看>>
素数筛选法
查看>>
php连接postgresql数据库
查看>>
移动应用开发选型:向左还是向右?
查看>>