亚马逊在线评估编码问题,以找到第n个几何级数
Amazon online assessment coding question to find nth Geometric Progression
给定几何级数的第二项和第三项。找到它的第n项,四舍五入到小数点后3位。
我们必须完成以下功能:
char* nthTerm(double input1, double input2, int input3) {
//your code here
}
input1 = 2nd term and input2 = 3rd term and both are between -2 to 2.
input3 = nth term to find and can be up to 100.
我无法在时间限制内将结果从double转换为char数组。测试是在Mettl平台上进行的,我想我无法使用to_string((、stringstream等。尽管pow((函数运行良好。
e.g input1 = 1, input2 = 2, input3 = 4
output = 4.0
请某人帮助解决这个问题。
我的方法,但得到编译错误和错误的判决:
double r = input2/input1;
double a = input1/r;
double ans = a * (double)pow(r, (double)(input3-1));
char *result = new char[1000];
// from here i tried so many thing like i used to_string,
// setprecision, maps, etc. But getting errors only.
您应该首先将您尝试的内容作为问题的一部分。无论如何,为了将双值转换为char指针,首先可以使用sprintf将其转换为char数组,然后简单地将结果转换为char指示器。下面的示例代码:
#include <stdio.h>
int main(void) {
char charray[200];
double num = 121.14;
sprintf(charray, "%2.3f", num);
char* c = &charray[0];
printf("%s", c);
return 0;
}
相关文章:
- std::condition_variable::wait()如何评估给定的谓词
- 在线编译器中的分段C++没有打印消息
- c++11评估顺序(未定义的行为)
- 在线程之间拆分任务总是值得的吗?
- join() 失败,如果在线程内部调用 io_context.run()
- 如何使用"equal to"以外的评估编写开关语句
- 嵌套 if 中没有返回评估
- 懒惰的参数评估try_emplace?
- 在实践中,在运行时为零的乘法中是否有任何"lazy"评估
- 如何在C++中在线组装?
- 如何改进一堆在已知值范围内评估变量的 else-if 条件?
- C++17:使用 std::optional 来评估枚举是否包含值
- 在线程中读取无符号整数时,c++ 位是否以原子方式切换?
- CNTK:->转发或 ->评估某些电脑上的崩溃,而不是其他电脑上的崩溃
- ConstexPR :GCC比Clang更努力地评估ConstexPR
- C++:Constexpr斐波那契数列评估
- C++能保证论点评估的原子性吗?
- 在编译时评估函数开销的通用方法
- 在线程函数中处理数据向量时进行线程竞速
- 亚马逊在线评估编码问题,以找到第n个几何级数