26进制(A到Z表示1到26,例27:AA,2019:BYQ)
解析:n-1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
A B C D E F G H I J K L M N O P Q R S T
20 21 22 23 24 25
U P W X Y Z
java代码如下:
import java.util.Scanner;
public class Demo {
public static int solve(int n, int r,char c[]){
int i=-1;
while (n > 0){
i++;
int t = n%r;
n = (n - 1) / r; //不是26进制
if (t == 0)
t = 26;
c[i]= (char)('A' + t - 1);//java中强制转化;
}
return i;
}
public static void main(String[] agrs) {
Scanner input=new Scanner(System.in);
int n=input.nextInt();
char [] c=new char[100];
int m = solve(n, 26,c);
for (int i =m; i>=0; i--)
System.out.print(c[i]);
}
}
》》》》》》》》》》
C++语言代码如下:
#include <cstdio>
#include <iostream>
using namespace std; ///作用:命名空间
string solve(int n, int r)
{
string ret;
while (n > 0){
int t = n%r;
n = (n - 1) / r; //不是26进制
if (t == 0)
t = 26;
ret += 'A' + t - 1;
}
return ret;
}
int main()
{
int n;
scanf_s("%d", &n);
string ans = solve(n, 26);
for (int i = ans.length() - 1; i >= 0; i--)
printf("%c", ans[i]);
return 0;
}