Volver a la lista de problemas
Numerically Speaking
619.c
/* Numerically Speaking */ #include <stdio.h> typedef struct { int len; int num[50]; } big; big number; big letter; void sum(big *a, int n, int base) { int i; for(i=0; i<a->len; i++) { a->num[i] += n; n = (a->num[i] / base); a->num[i] %= base; } while (n) { a->num[a->len] = (base==26 ? n-1 : n); n /= base; a->num[a->len] %= base; a->len++; } } void mul(big *a, int n, int base) { int i; for(i=0; i<a->len; i++) { if (base==26) { a->num[i] = (a->num[i]+1) * n - 1; } else { a->num[i] *= n; } } n=0; for(i=0; i<a->len; i++) { a->num[i] += n; n = (a->num[i] / base); a->num[i] %= base; } while (n) { a->num[a->len] = (base==26 ? n-1 : n); n /= base; a->num[a->len] %= base; a->len++; } } void num2letters() { int i; letter.len = 0; for(i=number.len-1; i>=0; i--) { mul(&letter, 10, 26); sum(&letter, number.num[i], 26); } } void letters2num() { int i; number.len = 0; for(i=letter.len-1; i>=0; i--) { mul(&number, 26, 10); sum(&number, letter.num[i], 10); sum(&number, 1, 10); } } int main(void) { char buf[1024]; while(scanf("%s", buf)==1) { int i; if (!strcmp(buf, "*")) { break; } if (buf[0]>='0' && buf[0]<='9') { number.len = strlen(buf); for(i=0; i<number.len; i++) { number.num[i] = buf[number.len-i-1]-'0'; } num2letters(); } else { letter.len = strlen(buf); for(i=0; i<letter.len; i++) { letter.num[i] = buf[letter.len-i-1]-'a'; } letters2num(); } for(i=letter.len-1; i>=0; i--) { printf("%c", 'a'+letter.num[i]); } printf("%.*s", 22-letter.len, " "); for(i=number.len-1; i>=0; i--) { printf("%c", '0'+number.num[i]); if (i && (i%3==0)) { printf(","); } } printf("\n"); } exit(0); }