首页 > 解决方案 > 一元'*'的无效类型参数(有'int') - 用于实现乘法和指数运算

问题描述

我编写了一个程序,用 5 替换数字中的零,时间复杂度为 logn N 以 10 为底的顺序。但是 cpp 实现会出现一些错误,如代码片段所示。

代码:

int convertFive(int n) {
int i = 0, temp = 0;
do{
    if ((n%10) == 0)
        temp = temp+(5*(10**i));
    else
        temp = temp+(n%10*(10**i))
    i++;
    n = n/10;
}while(n/10 != 0);
return temp;

错误:

prog.cpp:24:33: error: invalid type argument of unary ‘*’ (have ‘int’)
         temp = temp+(5*(10**i));
                             ^
prog.cpp:26:36: error: invalid type argument of unary ‘*’ (have ‘int’)
             temp = temp+(n%10*(10**i))

标签: c++arraysalgorithmdata-structures

解决方案


C++ 中没有 ** 运算符(你从 python3 中带来了 ** 运算符?)代替 **,你可以使用包含在中的 pow 函数数学.hcmath 标头。

double pow(双底,双指数);

(参考:http ://www.cplusplus.com/reference/cmath/pow/ )

而且,第 7 行没有分号


推荐阅读