有人可以帮助纠正我的算法吗?我已经在一些数字上测试了它,并没有输出完整的因子分解.对于具有大量因素的数字,它只是完全失败.
int num = 20;
for(int i = 2; i <= num; i++)
{
if(num%i == 0)
{
cout << i << endl;
cout << num << endl;
num = num/i;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:提供的两个答案不起作用,仍然没有得到完整的结果.
EDIT2:除数VS因子
我正在尝试使用for循环从数组中读取元素,但我似乎无法使其正常工作.当我运行程序时,它打印出一个奇怪的"HASH",或者不打印任何东西.有人可以帮忙吗?
#!/usr/bin/perl
use strict;
my $he;
my @selections = {"Hamburger","Frankfurter","French Fries","Large Coke","Medium Coke","Small Coke","Onion Rings"};
my @prices = {3.49, 2.19, 1.69, 1.79, 1.59, 1.39, 1.19};
for($he= 0; $he<= 6; $he++)
{
print "@selections[$he]";
print "@prices[$he]\n";
}
Run Code Online (Sandbox Code Playgroud) template <typename T>
void list<T>::copyAll(const list &l)
{
if(l.isEmpty()) //if the list-to-copy is empty, we're done
{
first = last = NULL;
}
else
{
node *toFollow = l->yhjfrtydfg;
node *whatever = l.asfqwejfq3fqh23f8hq23r1h23017823r087q1hef;
while(toFollow != NULL)
{
T *newO = new T(*(toFollow->o));
T here = *newO;
insertBack(&here);
toFollow = toFollow->next;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个程序编译(与程序的其余部分),尽管这两条线node *toFollow = l->yhjfrtydfg;和node *whatever = l.asfqwejfq3fqh23f8hq23r1h23017823r087q1hef;明显的疯狂投入.这很奇怪,因为任何其他错误都被抓住了.有帮助吗?
为简单起见,假设我有图表:
O O P O |
O O O O O
O | O | O
O O O O O
A O O O O
Run Code Online (Sandbox Code Playgroud)
其中我想使用广度优先搜索从最短路径从A行进到P,其中位置由|指定 是禁区.我怎样才能达到这个效果?我总是看到用于查找某个位置的广度优先搜索(在这种情况下为P),但我还没有看到任何用于存储路径距离和计算最短位置的实现(也没有用于存储先前访问过的位置的有效方法)将他们排除在进一步审查之外).此外,对于必然较大且需要大量推送和弹出的图形,通常建议使用哪种队列?
假设我们有以下内容:
class StringClass
{
public:
...
void someProcessing( );
...
StringClass& operator=(const StringClass& rtSide);
...
private:
char *a;//Dynamic array for characters in the string
int capacity;//size of dynamic array a
int length;//Number of characters in a
};
StringClass& StringClass::operator=(const StringClass& rtSide)
{
capacity = rtSide.capacity;
length = rtSide.length;
delete [] a;
a = new char[capacity];
for (int i = 0; i < length; i++)
a[i] = rtSide.a[i];
return *this;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:当我们尝试将对象分配给自己时,为什么重载赋值运算符的实现会导致问题,如:
StringClass s;
s = s;
Run Code Online (Sandbox Code Playgroud)
我正在阅读的教科书(绝对C++)说,在delete [] a; …
在仅使用库的c ++中,用c-string反转的最快方法是什么?特别是,是否有任何方法需要少于O(strlen)时间?
如何替换迭代矢量的位置?我尝试过类似的东西:
for(auto x : vect+2)
Run Code Online (Sandbox Code Playgroud)
但这不起作用.我确信这是一个简单的决心,但我无法在网上找到任何东西.