- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
假设我有一个数字,我可以找到构成该数字的所有质因数。例如,6000 是 2^4 * 3 * 5^3。
如果我有一个不能很好分解的数字(给定一个可接受的素数列表),我如何才能找到下一个最接近的数字?例如,给定数字 5917,与素数列表 2、3、5、7 相乘的最接近的数是多少?在这个例子中是 6000。
我有一些可以蛮力找到答案的东西,但必须有一个更优雅的解决方案。
const UInt32 num = 5917;
const CVector<UInt32> primes = { 2, 3, 5, 7 };
const size_t size = primes.size();
UInt32 x = num;
while (x < num * 2)
{
const UInt32 y = x;
for(size_t i = 0; i < size && x > 1; ++i)
{
while(x % primes[i] == 0)
{
x /= primes[i];
}
}
if (x == 1)
{
cout << "Found " << y << endl;
break;
}
else
{
x = y + 1;
}
}
编辑
我创建了一个测试,使用蛮力法和提供的 3 种方法作为答案,并得到了一些令人惊讶的结果。所有 4 个版本都产生了正确答案(感谢您的贡献),但是,蛮力法似乎是最快的,快了一个数量级。我尝试了几个不同的系统、编译器和架构,它们都产生了基本一致的结果。
测试代码可以在这里找到:http://ideone.com/HAgDsF .请随时提出建议。
最佳答案
我建议采用以下解决方案。我假设素数是从小到大排列的。我还使用了方便的 vector
和 int
类型。
vector<int> primes = { 2, 3, 5, 7 };
int num = 5917;
// initialize bestCandidate as a power of some prime greater than num
int bestCandidate = 1;
while (bestCandidate < num) bestCandidate *= primes[0];
set<int> s;
s.insert(1);
while (s.size()) {
int current = *s.begin();
s.erase(s.begin());
for (auto p : primes) { // generate new candidates
int newCandidate = current * p;
if (newCandidate < num) {
// new lower candidates should be stored.
if (s.find(newCandidate) == s.end())
s.insert(newCandidate);
}
else {
if (newCandidate < bestCandidate) bestCandidate = newCandidate;
break; // further iterations will generate only larger numbers
}
}
}
cout << bestCandidate;
接下来我想对建议的解决方案进行分析。让我用 np 作为素数; n 作为一个数字来找到最接近的结果; minP 作为列表中的最小素数。
我的解决方案生成所有低于 n 的可能值。新值是从旧值中产生的。每个值仅一次用作生成源。如果新值超过 n,则它被视为有效候选值。如果列表包含所有小于 n 的素数,算法仍然可以执行良好。我不知道该算法的时间复杂度公式,但它是低于 n 的有效候选数乘以前一个因子的对数。日志来自set
数据结构操作。如果 n 可以足够小以分配大小为 n 的数组来标记哪些值已经生成,我们可以去掉对数因子,一个简单的列表可以保存生成源值的集合。
您的初始解有 O(n(np + logminP(n)))。您检查每个数字是否有效,然后从 n 到 2n 支付 np + logminP(n) 每张支票。
Recursive solution by @anatolyg在多次“访问”某些有效数字方面存在很大缺陷,效率非常低。可以通过引入指示该号码已经“访问过”的标志来修复它。例如 12 = 2*2*3
将从 6 = 2*3
和 4 = 2*2
访问。次要缺陷是多次上下文切换和支持每次调用的状态。该解决方案有一个全局变量,使全局命名空间困惑,这可以通过添加函数参数来解决。
Solution by @dasblinkenlight缺乏效率,因为已经“使用过”的候选人被用来生成新的候选人,产生集合中已经存在的数字。虽然我借用了 set 的想法。
基于@גלעד ברקן's answer我创建了一个 c++
解决方案,因为没有 log
因素,它确实似乎渐进地更有效。但是,我拒绝使用 double
对数,并使用整数解决方案。这个想法很简单。我们有一个低于 num
的产品列表。每个产品都是从第一个 primesUsed
素数生成的。然后我们尝试使用下一个素数生成新产品。这种方法保证生成独特产品:
vector<int> primes = { 2, 3, 5, 7, 11, 17, 23 };
int num = 100005917;
int bestCandidate = INT_MAX;
list<pair<int, int> > ls;
ls.push_back(make_pair(1, 0));
while (ls.size()) {
long long currentProd = ls.front().first;
int primesUsed = ls.front().second;
ls.pop_front();
int currentPrime = primes[primesUsed];
while (currentProd < num) {
if(primesUsed < primes.size() - 1)
ls.push_back(make_pair(currentProd, primesUsed + 1));
currentProd *= currentPrime;
}
bestCandidate = min((long long)bestCandidate, currentProd);
}
cout << bestCandidate;
关于c++ - 找到最接近给定素数列表的因素的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36208887/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!