acm关于poj的有关问题
acm关于poj的问题
若将这个交上去会出现编译错误
Main.cpp
F:\temp\9798484.66862\Main.cpp(19) : error C2668: 'pow' : ambiguous call to overloaded function
math.h(575): could be 'long double pow(long double,int)'
math.h(527): or 'float pow(float,int)'
math.h(489): or 'double pow(double,int)'
while trying to match the argument list '(int, int)'
这是我的代码
#include <stdio.h>
#include<math.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i,t,j,n;
char ch[40];
int sum[5];
scanf("%d",&n);
while(n--)
{
scanf("%s",ch);
memset(sum,0,sizeof(sum));
t=0;
for (i=1;i<=4;i++)
{
for (j=t;j<=t+7;j++)
{
if(ch[j]=='1') sum[i]=sum[i]+pow(2,7+t-j);
}
t+=8;
}
printf("%d.%d.%d.%d\n",sum[1],sum[2],sum[3],sum[4]);
}
return 0;
}
若把将颜色的地方改为 if(ch[j]=='1') sum[i]=sum[i]+pow((float)2,7+t-j);
就对了 这是为什么
------解决方案--------------------
因为,pow这个函数的参数只能是下面几种情况
long double pow(long double,int)
float pow(float,int)'
double pow(double,int)'
即第一个参数必须是float、double、long double中的一个,而你pow(2,7+t-j);第一个参数是2是个int型,找不到匹配的函数。。。如果改成 pow(2.0,7+t-j);也是对的
若将这个交上去会出现编译错误
Main.cpp
F:\temp\9798484.66862\Main.cpp(19) : error C2668: 'pow' : ambiguous call to overloaded function
math.h(575): could be 'long double pow(long double,int)'
math.h(527): or 'float pow(float,int)'
math.h(489): or 'double pow(double,int)'
while trying to match the argument list '(int, int)'
这是我的代码
#include <stdio.h>
#include<math.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i,t,j,n;
char ch[40];
int sum[5];
scanf("%d",&n);
while(n--)
{
scanf("%s",ch);
memset(sum,0,sizeof(sum));
t=0;
for (i=1;i<=4;i++)
{
for (j=t;j<=t+7;j++)
{
if(ch[j]=='1') sum[i]=sum[i]+pow(2,7+t-j);
}
t+=8;
}
printf("%d.%d.%d.%d\n",sum[1],sum[2],sum[3],sum[4]);
}
return 0;
}
若把将颜色的地方改为 if(ch[j]=='1') sum[i]=sum[i]+pow((float)2,7+t-j);
就对了 这是为什么
------解决方案--------------------
因为,pow这个函数的参数只能是下面几种情况
long double pow(long double,int)
float pow(float,int)'
double pow(double,int)'
即第一个参数必须是float、double、long double中的一个,而你pow(2,7+t-j);第一个参数是2是个int型,找不到匹配的函数。。。如果改成 pow(2.0,7+t-j);也是对的