c字符数组转整型【c语言复习1】
//对A=1,B=2...Z=26,AA=27,AA=28,ZZ...
//将字符串转为相应整数
//c语言实现
#include <stdio.h>
int countA(char a[])//普通方法
{
int all=0;
int i=0;
while(a[i]!='\0')
{
all = all * 26 + a[i]-'A' +1 ;
i++;
}
return all;
}
int countB(char a[])//指针方法
{
int all=0;
char *p=a;
while(*p!='\0')
{
all = all * 26 + *p-'A' +1 ;
p++;
}
return all;
}
int main()
{
char s[10];
char t;
while(1)
{
int i=0;
while((t=getchar())!='\n')
{
if(t==EOF)
return 0 ;
s[i]=t;
i++;
}
s[i]='\0'; //字符数组最后要加上!
int c = countA(s);
int d = countB(s);
printf("%d\t%d\n",c,d);
}
return 0;
}
(2013腾讯广研创新班笔试题目)
我相信,会有一个公正而深刻的认识来为我们总结的:那时,我们这一代独有的奋斗、思索、烙印和选择才会显露其意义。 ——《北方的河》

浙公网安备 33010602011771号