相关疑难解决方法(0)

round()for C++中的float

我需要一个简单的浮点舍入函数,因此:

double round(double);

round(0.1) = 0
round(-0.1) = 0
round(-0.9) = -1
Run Code Online (Sandbox Code Playgroud)

我能找到ceil()floor()在math.h中-但不是round().

它是以另一个名称存在于标准C++库中,还是缺少?

c++ floating-point rounding

227
推荐指数
11
解决办法
36万
查看次数

确定平方根是否为整数

在我的程序中,我试图找到数字600851475143的最大素数因子.我已经制作了一个for循环,它确定了该数字的所有因子并将它们存储在向量数组中.我遇到的问题是我不知道如何确定因子是否可以是平方根并且给出整数而不是小数.到目前为止我的代码是:

#include <iostream>
#include <vector>
#include <math.h>

using namespace std;
vector <int> factors;

int main()
{
    double num = 600851475143;
    for (int i=1; i<=num; i++)
    {
        if (fmod(num,i)==0)
        {
            factors.push_back(i);
        }
    }

     for (int i=0; i<factors.size(); i++)
     {
         if (sqrt(factor[i]))                      // ??? 
     }
}
Run Code Online (Sandbox Code Playgroud)

有人可以通过我的if语句告诉我如何确定一个数字是否可以平方根?

c++ arrays math loops sqrt

3
推荐指数
1
解决办法
7381
查看次数

标签 统计

c++ ×2

arrays ×1

floating-point ×1

loops ×1

math ×1

rounding ×1

sqrt ×1