#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<math.h>
using namespace std;
int fun(char s[])
{
int i = 0;
int list = 0;
while (s[i]!='\0')
{
i++;
}
int j = 0;
while (i>0)
list += (s[--i] - 64)*pow(26, j++);
return list;
}
int main()
{
cout << fun("ABA");
system("pause");
}
上面代码是不严谨的,没有对传入的字符串判空,没有判断如果不是大写字母怎么办。
一直对递归函数的改写不太熟悉,顺便试试递归求N次方;
int POW(int x, int y)
{
if (y == 0)
return 1;
return x*POW(x, y - 1);
}
可以把上面的pow替换,结果一样。