-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathZOJ1272(AC).cpp
137 lines (124 loc) · 2.05 KB
/
ZOJ1272(AC).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int MAXN = 40;
char s[MAXN], s1[MAXN];
int a[MAXN];
int a0[MAXN];
int len;
void reverse(char s[])
{
int i, len;
char ch;
i = 0;
len = strlen(s);
while(i < len - 1 - i){
ch = s[i];
s[i] = s[len - 1 - i];
s[len - 1 - i] = ch;
++i;
}
}
void minus_one(int a[])
{
int i;
--a[0];
for(i = 0; i < MAXN - 1; ++i){
if(a[i] < 0){
a[i] += 10;
--a[i + 1];
}else{
break;
}
}
if(a[MAXN - 1] < 0){
a[MAXN - 1] += 10;
}
}
int main()
{
int i, j, r;
while(scanf("%s", s) == 1 && strcmp(s, "*")){
if(s[0] >= '0' && s[0] <= '9'){
if(strcmp(s, "1") == 0){
printf("%-22s1\n", "a");
continue;
}
//number to word
memset(a, 0, MAXN * sizeof(int));
len = strlen(s);
for(i = 0; i < len; ++i){
a[i] = s[len - 1 - i] - '0';
}
memcpy(a0, a, MAXN * sizeof(int));
len = 0;
while(true){
i = MAXN - 1;
while(i >= 0 && a[i] == 0){
--i;
}
if(i < 0){
break;
}
for(i = MAXN - 1; i > 0; --i){
a[i - 1] += a[i] % 26 * 10;
a[i] = a[i] / 26;
}
r = a[0] % 26;
a[0] = a[0] / 26;
if(r > 0){
s[len++] = 'a' + r - 1;
}else{
s[len++] = 'z';
minus_one(a);
}
}
memcpy(a, a0, MAXN * sizeof(int));
s[len] = 0;
reverse(s);
printf("%-22s", s);
i = MAXN - 1;
while(i >= 0 && a[i] == 0){
--i;
}
while(i >= 0){
printf("%d", a[i]);
if(i > 0 && i % 3 == 0){
printf(",");
}
--i;
}
printf("\n");
}else{
//word to number
memset(a, 0, MAXN * sizeof(int));
for(i = 0; s[i]; ++i){
for(j = 0; j < MAXN; ++j){
a[j] *= 26;
}
a[0] += s[i] - 'a' + 1;
for(j = 0; j < MAXN - 1; ++j){
a[j + 1] += a[j] / 10;
a[j] %= 10;
}
a[j] %= 10;
}
printf("%-22s", s);
i = MAXN - 1;
while(i >= 0 && a[i] == 0){
--i;
}
while(i >= 0){
printf("%d", a[i]);
if(i > 0 && i % 3 == 0){
printf(",");
}
--i;
}
printf("\n");
}
}
return 0;
}