1. 程式人生 > 其它 >Excel表格的列A~Z字母與數值之間的轉換

Excel表格的列A~Z字母與數值之間的轉換

技術標籤:其他Excel進位制轉換

題目描述:

Excel表格的第1列為A,
Excel表格的第2列為B,

Excel表格的第26列為Z,

Excel表格的第28列為AB,

Excel表格的第19007列為ABCA。

這裡我整理了一下Excel表格的列A~Z與數值之間的轉換關係。

字母轉換為數值:

直接按位乘權值。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long
ll; const int N=1010; ll a[N]; char s[N]; int main() { scanf("%s",s); int len=strlen(s); a[0]=1; for(int i=1;i<N;i++) a[i]=a[i-1]*26; ll ans=0; for(int i=len-1,j=0;i>=0;i--,j++) ans+=(s[i]-'A'+1)*a[j]; cout<<ans<<endl; return 0; }

在這裡插入圖片描述

數值轉換為字母:

在10進位制當中,10佔兩位,
而Excel中26進位制的Z只佔了一位。

下面程式碼中while中的n- -是靈魂所在。

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1010;
int ans[N];
int main()
{
	int n,cnt=0;
	cin>>n;
	while(n)
	{
		n--;
		ans[cnt++]=n%26;
		n/=26;
	}
	for(int i=cnt-1;i>=0;i--)
		printf("%c"
,ans[i]+'A'); return 0; }

在這裡插入圖片描述