64,652
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <cmath>
using namespace std;
int titleToNumber(string s) {
int n = 0, len = s.length();
for(int i = 0; i < s.length(); ++ i) {
char c = s[i];
int chara = c - 'A' + 1;
cout<<"chara = " << chara << endl;
n += chara * pow(26, len - 1 - i);
cout<<"pow = " << pow(26.0, len - 1 - i) << endl;
cout<<"n = " << n << endl;
cout<<endl;
}
return n;
}
int main() {
cout<<titleToNumber("AAB");
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
double titleToNumber(string s) {
double n = 0, len = s.length();
for(int i = 0; i < s.length(); ++ i) {
char c = s[i];
double chara = c - 'A' + 1;
double a = len - 1 - i;
n += chara * pow(26.0, a);
}
return n;
}
int main() {
cout<<titleToNumber("AAB");
return 0;
}
用double的话是OK的,问题还是上一楼回答的,两个编译器内部是如何走的……
#include <stdio.h>
#include <math.h>
int main() {
int n=0,m=3;
n=pow(26,2);
printf("pow(26,2)=%d\n",n);
n=pow(26.0,2.0);
printf("pow(26.0,2)=%d\n",n);
n=pow(26,m-1);
printf("pow(26,int(2))=%d\n",n);
n=pow(26.0,m-1);
printf("pow(26.0,int(2))=%d\n",n);
return 0;
}
在c++结果不同 是函数匹配的问题么
int m=3; void A(int* x){*x=pow(26,m-1);}
void B(float *y){*y=pow(26,m-1); }
(A) (B)
pushl %ebp pushl %ebp
movl %esp, %ebp movl %esp, %ebp
subl $40, %esp subl $40, %esp
movl _m, %eax movl _m, %eax
subl $1, %eax subl $1, %eax
movl %eax, -20(%ebp) movl %eax, -16(%ebp)
fildl -20(%ebp) fildl -16(%ebp)
fstpl 8(%esp) fstpl 8(%esp)
fldl LC0 fldl LC0
fstpl (%esp) fstpl (%esp)
call _pow call _pow
fnstcw -10(%ebp) fstps -12(%ebp)
movzwl -10(%ebp), %eax flds -12(%ebp)
movb $12, %ah movl 8(%ebp), %eax
movw %ax, -12(%ebp) fstps (%eax)
fldcw -12(%ebp) nop
fistpl -16(%ebp) leave
fldcw -10(%ebp) ret
movl -16(%ebp), %edx
movl 8(%ebp), %eax
movl %edx, (%eax)
nop
leave
ret
看汇编代码的话 好像函数调用是一样的 就是转换的问题了