coj 1140 序号互换

进制转换,注意边界(WA 5次)。

测试数据:26 27 52 160 Z FD

结果:  Z AA AZ FD 26 160

# include <stdio.h>

# include <ctype.h>

# include <memory.h>

 

char a[15];

char s[15];

 

int main()

{

    int T, i, n;



    scanf("%d", &T);

    while (T--)

    {

        scanf("%s", a);

        if (isdigit(a[0]))

        {

            i = 0;

            n = atoi(a);

            while  (n > 0)

            {

                s[i] = (n+25) % 26 + 'A'; 

                ++i;

                if (n % 26) n /= 26;

                else {n /= 26; --n;}

            }

            while (i > 0) putchar(s[--i]);

            putchar('\n');

            memset(s, 0, sizeof(s));

        }

        else

        {

            i = n = 0;

            while (a[i] != '\0')

            {

                n = 26*n + a[i]-'A'+1;

                ++i;

            }

            printf("%d\n", n);

        }

         

    }

     

    return 0;

}

 

你可能感兴趣的:(OJ)