思路:除基取余(参考十进制转二进制)
关于网上大多数提供的代码如下:
#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)
#include<iostream>
using namespace std;
int main()
{
char ch[26];
char ans[5];
int index = 0;
int n =2019;
for (int i = 0; i < 26; i++)
{
ch[i] = 'A' + i;
}
while (n)
{
int a=n % 26;
n=n / 26;
if (a == 0)
{
a += 26;
}
ans[index++] = ch[a - 1];
}
for (int j = index-1; j >= 0; j--)
{
cout << ans[j] << endl;
}
system("pause");
return 0;
}
最后输出的答案为BYQ且是正确答案;但是本人好奇将题目中52代入得出结果却为BZ,而题目给出参考为AZ。
然后我对此修改了一下,代码如下:
#include <stdio.h>
int main()
{
char ch[26];
char ans[5];
int index = 0, n = 2019;
for (int i = 0; i < 26; i++)
{
ch[i] = 'A' + i;
}
cout:
while (n)
{
int t = n % 26;
if (n % 26 == 0 && n / 26 != 0)
{
ans[index++] = 'Z';
n = n / 26-1;
goto cout;
}
n = n / 26;
ans[index++] = ch[t - 1];
}
for (int i = index - 1; i >= 0; i--)
{
printf("%c", ans[i]);
}
return 0;
}
此时得出2019输出仍为BYQ,而52输出符合题目中AZ,且符合题中其它案例;
如有不足,希望大佬多多指导。谢谢!