题目
题目链接:
ZOJ1217 Numerically Speaking https://zoj.pintia.cn/problem-sets/91827364500/problems/91827364771
测试样例
输入样例
29697684282993
transcendental
28011622636823854456520
computationally
zzzzzzzzzzzzzzzzzzzz
*
输出样例
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 <algorithm>
#include <cmath>
#include <vector>
using namespace std;
inline string convert(string str);
inline string change_num(string num);
template <typename T>
inline T contrast(T s);
int main()
{
string tmp, num, str;
while (cin >> tmp)
{
if (tmp[0] == '*')
break;
if ('0' <= tmp[0] && tmp[0] <= '9')
{
num = tmp;
str = convert(num);
}
else
{
str = tmp;
num = convert(str);
}
num = change_num(num);
cout << str;
for (int i = 0; i < 22 - str.length(); i++)
cout << " ";
cout << num << endl;
}
return 0;
}
template <typename T>
inline T contrast(T s)
{
T res;
//s是数字
if ('0' <= s && s <= '0' + 26)
res = s - '0' + 'a' - 1;
else//是字符
res = s - 'a' + '1';
return res;
}
//数字转字符,字符转数字(进制转换)
inline string convert(string str)
{
string res;
vector<long long>quotient;//保存商
for (int i = 0; i < str.length(); i++)
quotient.push_back(str[i]);
if ('0' <= str[0] && str[0] <= '9')//是数字
{
int flag1 = true;
int remainder = 0, point_remainder = 0;
while (1)
{
int flag = true;
for (int i = 0; i < quotient.size(); i++)
if (quotient[i] != '0')
flag = false;
if (flag)
break;
//取模操作
point_remainder = 0;
for (int i = 0; i < quotient.size(); i++)
{
//printf("B, quotient[%d] = %lld\n", i, quotient[i]);
//cout << "point_remainder = " << point_remainder << endl;
quotient[i] = quotient[i] + 10 * point_remainder - '0';//加上上一位的余数乘以10
//printf("A, quotient[%d] = %lld\n", i, quotient[i]);
point_remainder = quotient[i] % 26;
quotient[i] = quotient[i] / 26 + '0';
}
if (flag1 && !point_remainder)
point_remainder += 26;
flag1 = false;
remainder = point_remainder + '0';//得到最终余数
//printf("remainder = %d\n", remainder - '0');
char bit = contrast(remainder);
res = res + bit;
//cout << "num, res = " << res << endl;
//printf("bit = %d\n", bit - 'a');
}
reverse(res.begin(), res.end());//翻转字符串
return res;
}
else
{
int remainder = 0, point_remainder = 0;
while (1)
{
int flag = true;
for (int i = 0; i < quotient.size(); i++)
if (quotient[i] != ('a' - 1))
flag = false;
if (flag)
break;
//取模操作
point_remainder = 0;
for (int i = 0; i < quotient.size(); i++)
{
//printf("B, quotient[%d] = %lld\n", i, quotient[i]);
//cout << "point_remainder = " << point_remainder << endl;
quotient[i] = quotient[i] + 26 * point_remainder - ('a' - 1);//加上上一位的余数乘以26
//printf("A, quotient[%d] = %lld\n", i, quotient[i]);
point_remainder = quotient[i] % 10;
quotient[i] = quotient[i] / 10 + ('a' - 1);
}
remainder = point_remainder + ('a' - 1);//得到最终余数
//printf("remainder = %d\n", remainder - ('a' - 1));
char bit = contrast(remainder);
res = res + bit;
//cout << "str, res = " << res << endl;
//printf("bit = %d\n", bit - '0');
}
reverse(res.begin(), res.end());//翻转字符串
return res;
}
return res;
}
//加逗号
inline string change_num(string num)
{
string res;
while (num[0] == '0')
num = num.substr(1, num.length());
//cout << "num = " << num << ", num.length() = " << num.length()<<endl;
for (int i = 0; i < num.size(); i++)
{
res += num[i];
if (((i + 1) % 3 == (num.size()) % 3) && i != num.size() - 1)
res += ',';
}
return res;
}