原题代号:HDU 1314

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1314

Numerically Speaking

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

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

(注意:数字是从第23列开始写,之前空格不能丢了)

解题思路:这个题类似二十六进制的互相转换,只不过a相当于1,z相当于26而已;只不过要考虑数字转换成字母时的特殊地方(26/26=1余0,但是26是z,所以如果余数为0则字母为z并且商-=1)
AC代码:

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <math.h>
# include <algorithm>
using namespace std;
# define pi acos(-1.0)
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define For(i,n,a) for(int i=n; i>=a; --i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define Fo(i,n,a) for(int i=n; i>a ;--i)
typedef long long LL;
typedef unsigned long long ULL; //string比较函数:相等返回0,str1>str2返回1,str1<str2返回-1.
int Compare(string str1,string str2)
{
if(str1.length() > str2.length()) return ;
else if(str1.length() < str2.length()) return -;
else return str1.compare(str2);
} string Big_Plus(string str1,string str2)
{
string ans;
int len1=str1.length();
int len2=str2.length();
//将长度较小的前面补0,使两个string长度相同
if(len1<len2){
for(int i=;i<=len2-len1;i++){
str1=""+str1;
}
}else {
for(int i=;i<=len1-len2;i++){
str2=""+str2;
}
}
int len=max(len1,len2);
int carry=;
for(int i=len-;i>=;i--){
int tmp=str1[i]-''+str2[i]-''+carry;
carry=tmp/;
tmp%=;
ans=char(tmp+'')+ans;
}
if(carry) ans=char(carry+'')+ans;
return ans;
} //支持大数减小数
string Big_Sub(string str1,string str2)
{
string ans;
int carry=;
int difference=str1.length()-str2.length();//长度差
for(int i=str2.length()-;i>=;i--){
if(str1[difference+i]<str2[i]+carry){
ans=char(str1[difference+i]+-str2[i]-carry+'')+ans;
carry=;
}else {
ans=char(str1[difference+i]-str2[i]-carry+'')+ans;
carry=;
}
}
for(int i=difference-;i>=;i--){
if(str1[i]-carry>=''){
ans=char(str1[i]-carry)+ans;
carry=;
}else {
ans=char(str1[i]-carry+)+ans;
carry=;
}
}
//去除前导0
ans.erase(,ans.find_first_not_of(''));
if(ans.empty()) ans="";
return ans;
} string Big_Mul(string str1,string str2)
{
string ans;
int len1=str1.length();
int len2=str2.length();
for(int i=len2-;i>=;i--){
string tmpstr="";
int data=str2[i]-'';
int carry=;
if(data!=){
for(int j=;j<=len2--i;j++){
tmpstr+="";
}
for(int j=len1-;j>=;j--){
int t=data*(str1[j]-'')+carry;
carry=t/;
t%=;
tmpstr=char(t+'')+tmpstr;
}
if(carry!=) tmpstr=char(carry+'')+tmpstr;
}
ans=Big_Plus(ans,tmpstr);
}
ans.erase(,ans.find_first_not_of(''));
if(ans.empty()) ans="";
return ans;
} //正数相除,商为quotient,余数为residue void Big_Div(string str1,string str2,string& quotient,string& residue)
{
quotient=residue="";//商和余数清空
if(str2==""){//;判断除数是否为0
quotient=residue="ERROR";
return;
}
if(str1==""){//判断被除数是否为0
quotient=residue="";
return;
}
int res=Compare(str1,str2);
if(res<){//被除数小于除数
quotient="";
residue=str1;
return;
}else if(res==){
quotient="";
residue="";
return ;
}else {
int len1=str1.length();
int len2=str2.length();
string tmpstr;
tmpstr.append(str1,,len2-);//将str1的前len2位赋给tmpstr
for(int i=len2-;i<len1;i++){
tmpstr=tmpstr+str1[i];//被除数新补充一位
tmpstr.erase(,tmpstr.find_first_not_of(''));//去除前导0
if(tmpstr.empty()) tmpstr="";
for(char ch='';ch>='';ch--){//试商
string tmp,ans;
tmp=tmp+ch;
ans=Big_Mul(str2,tmp);//计算乘积
if(Compare(ans,tmpstr)<=){//试商成功
quotient=quotient+ch;
tmpstr=Big_Sub(tmpstr,ans);//减掉乘积
break;
}
}
}
residue=tmpstr;
}
quotient.erase(,quotient.find_first_not_of(''));
if(quotient.empty()) quotient="";
} string change(int num)
{
string n="";
stack<char>M;
while(num>)
{
M.push(num%+'');
num/=;
}
while(!M.empty())
{
n+=M.top();
M.pop();
}
return n;
} int change(string num)
{
int n=num[]-'';
for(int i=;i<num.size();i++)
n=n*+num[i]-'';
return n;
} int main()
{
//freopen("in.txt", "r", stdin);
string s;
while(cin>>s,s[]!='*')
{
if('a'<=s[]&&s[]<='z')
{
string num=change(s[]-'a'+);
for(int i=;i<s.size();i++)
{
num=Big_Plus(Big_Mul(num,change()),change(s[i]-'a'+));
}
cout<<s;
for(int i=s.size()+;i<;i++)cout<<' ';
for(int i=;i<num.size();i++)
{
if(i)
printf((num.size()-i)%==?",%c":"%c",num[i]);
else
printf("%c",num[i]);
}
cout<<endl;
}
else if(''<=s[]&&s[]<='')
{
stack<char>M;
string str1,str2;//商和余数
string str="",str3=s;
while(Compare(str3,"")>)//str1>0
{
Big_Div(str3,"",str1,str2);
str3=str1;
if(str2=="")
{
M.push('z');
str3=Big_Sub(str3,"");
}
else
{
M.push(change(str2)+'a'-);
}
}
while(!M.empty())
{
str+=M.top();
M.pop();
}
cout<<str;
for(int i=str.size()+;i<;i++)cout<<' ';
for(int i=;i<s.size();i++)
{
if(i)
printf((s.size()-i)%==?",%c":"%c",s[i]);
else
printf("%c",s[i]);
}
cout<<endl;
}
}
return ;
}

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

  1. 【HDOJ】1314 Numerically Speaking

    学了几天的Java了,终于独立A了一道大数计算.感觉还得练Java啊. import java.util.Scanner; import java.math.BigInteger; import ja ...

  2. Python 迭代器&生成器,装饰器,递归,算法基础:二分查找、二维数组转换,正则表达式,作业:计算器开发

    本节大纲 迭代器&生成器 装饰器  基本装饰器 多参数装饰器 递归 算法基础:二分查找.二维数组转换 正则表达式 常用模块学习 作业:计算器开发 实现加减乘除及拓号优先级解析 用户输入 1 - ...

  3. Python中如何将二维列表转换成一维列表

    已知:a = [(4,2,3), (5, 9, 1), (7,8,9)]希望将二维列表转换成一维列表:["4,2,3", "5, 9, 1", "7, ...

  4. 剑指offer26:将二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。

    1 题目描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 2 思路和方法 在二叉搜索树中,每个结点都有两个分别指向其左.右子树的 ...

  5. HDU 5047 Sawtooth(大数优化+递推公式)

    http://acm.hdu.edu.cn/showproblem.php?pid=5047 题目大意: 给n条样子像“m”的折线,求它们能把二维平面分成的面最多是多少. 解题思路: 我们发现直线1条 ...

  6. HDU 1212 Big Number 大数模小数

    http://acm.hdu.edu.cn/showproblem.php?pid=1212 题目大意: 给你一个长度不超过1000的大数A,还有一个不超过100000的B,让你快速求A % B. 什 ...

  7. hdu 3999 The order of a Tree (二叉搜索树)

    /****************************************************************** 题目: The order of a Tree(hdu 3999 ...

  8. HDU 4819 Mosaic(13年长春现场 二维线段树)

    HDU 4819 Mosaic 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4819 题意:给定一个n*n的矩阵,每次给定一个子矩阵区域(x,y,l) ...

  9. Buy the Ticket HDU 1133 递推+大数

    题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1133 题目大意: 有m+n个人去买电影票,每张电影票50元,  m个人是只有50元一张的,  n个人 ...

随机推荐

  1. 【Qt开发】【Linux开发】调试记录:QFontDatabase::loadFromCache Error

    最近做嵌入式的Qt界面,在移植成功后遇到了一个问题:QFontDatabase::loadFromCache: Font path doesn't match.后面跟着便是两个路径. 解决方案就是对比 ...

  2. [LeetCode] 1092. Shortest Common Supersequence

    LeetCode刷题记录 传送门 Description Given two strings str1 and str2, return the shortest string that has bo ...

  3. [Web 前端] 023 js 的流程控制、循环和元素的获取、操作

    1. Javascript 流程控制 用于"基于不同条件执行不同的动作"的场合 1.1 if 语句 三种形式 // 第一种 if... // 第二种 if... else ... ...

  4. C++中构造函数的手动和自动调用方式

    1,对象的构造通过构造函数来完成,和类名相同且没有返回值,这个时候只有参   数一个特性,构造函数可以自定义参数,这个参数一般而言就是对类进行初始  化来使用的:带有参数的构造函数的意义在于可以使得每 ...

  5. C++ 统计输入的句子有多少英文字母

    // ConsoleApplication1.cpp: 定义控制台应用程序的入口点.//#include "stdafx.h"#include <iostream>#i ...

  6. MySQL总结(3)

    ORDER BY 与 GROUP BY ORDER BY GROUP BY 排序产生的输出 分组行.但输出可能不是分组的顺序 不一定需要 如果与聚集函数一起使用列(或表达式)则必须使用 任意列都可以使 ...

  7. SpringMVC源码解析

    一:springmvc运行过程: 1. dispatcherServlet 通过 HandlerMapping 找到controller2. controller经过后台逻辑处理得到结果集modela ...

  8. 简洁的Asp.net菜单控件

    http://www.cnblogs.com/ruinet/archive/2009/11/10/1599984.html asp.net自带的菜单控件采用的table和javascript,导致生成 ...

  9. Android真机测试时无法连接服务器

    之前服务器的通信一直是在模拟机上实现的,今天用在真机上却不成功.百度之后发现是安卓9以后禁止使用HTTP直接访问服务器.记录一下以后使用. 参考博文:https://blog.csdn.net/don ...

  10. C++ 中头文件<bits/stdc++.h>的优缺点

    在编程竞赛中,我们常见一个头文件: #include <bits/stdc++.h> 发现它是部分C++中支持的一个几乎万能的头文件,包含所有的可用到的C++库函数,如<istrea ...