找到最大.任何给定数字的功率值小于另一个给定数字
Finding the max. value of power of any given number which is less than another given number
对于给定值'a'和y',如何找到最大x值,以使x = a^b<y对于b∈N和a> 0。例如,给出y = 14,a = 2,然后x必须为8。换句话说,对于[8.15]中的所有y值,x必须为8。同样,对于[9,26]中y的所有值,x必须为9。
您可以使用基础a使用日志。<cmath>
中不存在这样的功能,但是如果您记得
log (base a, c) = log (base e, c) / log (base e, a)
您可以使用CMATH的日志(自然对数)函数进行操作。
int exponent = log(y)/log(a); //truncates to the floor, just what we need.
int answer = a to the power of exponent
相当明显的算法任务...取y的基础-A对数的整数部分,并将A提高到该力量:
#include <cmath>
int exponent = (int)(log(y) / log(a)); // base-a logarithm of y, truncated to a whole number
int x = (int)pow(a, exponent); // a raised to that power
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 将数字转换为字母(例如:123 转换为一二三)
- C++如何计算用户输入的数字中的偶数位数
- 如何在C++中确定文本文件中的元素是字符还是数字
- 打印数字图案
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 如何检查一个c++字符串中有多少相同的字符/数字
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 找到最大.任何给定数字的功率值小于另一个给定数字
- 计算"power of 2"数字使用的功率的最快方法?
- 计算和存储非常大的数字功率