我们如何找到 N sqrt(m) 并检查答案是否是 C++ 中的整数
how can we find n sqrt(m) and check whether the answer is an integer in c++
这是我尝试过的方法,所以如果您有任何建议,请提供帮助。使用模板可以解决这个问题吗
#include <iostream>
`#include<math.h>
using namespace std;
int main()
{
long double m=0;
long double n=0,ans=0;//is this wrong
int t;
cin>>t;
for(int i=0;i<t;i++)//here t is testcases
{
cin>>n>>m;
ans=pow(m,1.0/n);
if(floor(ans)==ans)//this is to check if ans is int
cout<<ans<<"n";
else
cout<<"-1"<<"n";
}
return 0;
}
long double m;
long double value = sqrt(m);
if(floor(value) == (value)) {
cout << "Sqrt of given number is integer n";
}
还有一个小建议。在 14 行的编程中 ans=pow(m,1.0/n);
.使用 powl()
而不是 pow
,因为变量的类型为 long double
编辑:正如Potatoswatter在此答案的评论部分中所建议的那样,C++
过载pow()
因此无需详细说明powl()
#include <iostream>
#include <cmath>
using namespace std;
int main() {
long double m; // No need to assign zero
long double n,ans; // No need to assign zero
int t;
cin >> t;
for(int i = 0; i < t; i++) {
cin >> n >> m;
ans = powl(m,(1.0/n));
if(floor(ans) == ans)//this is to check if ans is int
cout << ans <<"n";
else
cout << "-1" << "n";
}
return 0;
}
相关文章:
- C++LDAP检查用户是否是特定组的成员
- 检查某些类型是否是模板类 std::optional 的实例化
- 将错误返回给调用方而不是立即在 C++ 中抛出错误是否是一种好的做法
- 如何检查模板专用化是否是基本模板的子类?
- 如何检查变量是否是C++中的地图?
- 为什么C++程序在太大时返回代码而不是答案?
- C++ Chrono 确定一天是否是周末?
- 将相同共享指针的副本存储在不同的向量中是否是一种好的做法?
- 使用类在C++中存储和列出变量/方法是否是一种好的做法
- 代码在 CodeSignal 中工作不正确。不确定这是否是我的代码缺陷
- 如果 C 函数仍然可以间接执行(通过回调函数),那么将它声明为静态函数是否是一种不好的做法?
- MFC 中的窗口消息管理:添加基类调用是否是强制性的?
- 检查 n2 是否是 n1 的倍数后结果错误,但根本没有错误
- C ++:检查它是否是类中的数字
- 传递给放置 new 的指针是否是指向其对象表示形式的非 UB 指针?
- 使用 SET(C++) 检查两个给定字符串是否是字谜时出现运行时错误
- 从 std::string 到 std::array<char,size> 的 memcopy 额外数据是否是一种未定义的行为?
- 有没有办法检查发送到变量的值是否是正确的类型,而它已经在该变量下?
- 如何检查一个模板是否是另一个模板的类成员
- 我们如何找到 N sqrt(m) 并检查答案是否是 C++ 中的整数