📄 pku1312.cpp
字号:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 100
#define MOD 1000
char s[SIZE];
int v[SIZE];
int v2[SIZE];
int vL, sL, vL2;
int MUL;
void v_mul()
{
int i;
for (i = 0; i < vL; i++)
v[i] *= MUL;
for (i = 0; i < vL; i++)
{
v[i + 1] += v[i] / MOD;
v[i] %= MOD;
}
if (v[vL])
vL++;
}
void v_add(int x)
{
int i;
v[0] += x;
while (i < vL)
{
if (v[i] >= MOD)
{
v[i + 1] += v[i] / MOD;
v[i] %= MOD;
}
else
{
break;
}
}
if (v[vL])
vL++;
}
int v_minus()
{
int i;
if (v[0] == 0 && vL <= 0)
return 0;
v[0]--;
i = 0;
while (i < vL)
{
if (v[i] < 0)
v[i] += MOD, v[i + 1]--;
else
break;
i++;
}
if (v[vL - 1] == 0)
vL--;
return 1;
}
char v_getmod()
{
int i, ans;
for (i = vL - 1; i > 0; i--)
v[i - 1] += (v[i] % 26) * MOD, v[i] /= 26;
ans = v[0] % 26;
v[0] /= 26;
if (v[vL - 1] == 0)
vL--;
return ans + 'a';
}
void calc1()
{
int i, j;
char t;
vL = 0;
sL = strlen(s);
memset(v, 0, sizeof(v));
MUL = 10;
for (i = 0; i < sL; i++)
{
v_mul();
v_add(s[i] - '0');
}
memset(s, 0, sizeof(s));
i = 0;
memcpy(v2, v, sizeof(v));
vL2 = vL;
while (1)
{
if (!v_minus())
break;
s[i++] = v_getmod();
}
i--;
j = 0;
while (j < i)
t = s[i], s[i] = s[j], s[j] = t, j++,i--;
}
void calc2()
{
int i;
vL = 0;
sL = strlen(s);
MUL = 26;
memset(v, 0, sizeof(v));
for (i = 0; i < sL; i++)
{
v_mul();
v_add(s[i] - 'a' + 1);
}
memcpy(v2, v, sizeof(v));
vL2 = vL;
}
void output()
{
int i;
printf("%-22s", s);
printf("%d", v2[vL2 - 1]);
for (i = vL2 - 2; i >= 0; i--)
printf(",%03d", v2[i]);
printf("\n");
}
int main()
{
while (EOF != scanf("%s", s))
{
if (!strcmp(s, "*"))
break;
if (isdigit(s[0]))
calc1();
else
calc2();
output();
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -