c++ - 一元'*'的无效类型参数(有'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++ 中没有 ** 运算符(你从 python3 中带来了 ** 运算符?)代替 **,你可以使用包含在中的 pow 函数数学.hcmath 标头。
double pow(双底,双指数);
(参考:http ://www.cplusplus.com/reference/cmath/pow/ )
而且,第 7 行没有分号
推荐阅读
- immutable.js - ImmutableJS:如何将不透明变量转换为 JSON
- excel - 如果单元格已填充,如何进入下一步?(Excel(VBA)中数据输入的自动单元格移动)
- powerquery - Powerquery - 使用不同的列将同一个表附加到自身
- excel - VBS - 如何获取文件名的今天日期?
- perl - 如何存储和检索 perl 对象
- linux - 记录 Bash 交互,分别保存 STDIN、STDOUT
- html - 仅在图标而不是名称上切换类别导航
- android - 单击 ActionBar 菜单项时带有 Android ActionBar 的弹出对话框
- javascript - 使用 Node.js 将 createWriteStream txt 内容写入全局变量
- r - 将 ifelse() 添加到 Map 函数中