"5"怎么了?纸上一切都很好
What's wrong with "5"? Everything is just fine on the paper
这是我们应该用C 解决的问题:
GCD ( 2m , 2n ) = 2 * GCD( m , n )
GCD ( 2m , 2n+1 ) = GCD ( m , 2n+1 )
GCD ( 2m+1, 2n+1 ) = GCD ( n-m , 2m+1 ) (m<n)
GCD ( m , m ) = m
这是我写的功能:
int GCD(int n1, int n2)
{
bool n1Zoj, n2Zoj;
n1Zoj = (n1%2 == 0);
n2Zoj = (n2%2 == 0);
if(n1Zoj && n2Zoj)
return 2 * GCD(n1/2, n2/2);
if(n1Zoj && !n2Zoj)
return GCD(n1/2, n2);
if(!n1Zoj && !n2Zoj)
return GCD((n2-n1)/2, n1);
if(n1 == n2)
return n1;
}
(*" zoj"表示我的语言(波斯语))
当我将5作为第二个参数传递时,程序崩溃并打印以下消息:
Segmentation fault (core dumped)
退出代码是139
。我正在使用使用G 作为编译器的Ubuntu上的Code ::块。
更新:程序以5,10,15,20,25崩溃,...
更新:我认为函数的正确形式是:
int GCD(int n1, int n2)
{
if (n1 > n2)
std::swap(n1, n2);
//std::cout<<"GCD is called with params: "<<n1<<" & "<<n2<<std::endl;
bool n1Zoj, n2Zoj;
n1Zoj = (n1%2 == 0);
n2Zoj = (n2%2 == 0);
if(n1 == n2)
return n1;
if(n1Zoj && n2Zoj)
return 2 * GCD(n1/2, n2/2);
if(n1Zoj && !n2Zoj)
return GCD(n1/2, n2);
if(!n1Zoj && n2Zoj)
return GCD(n2/2, n1);
if(!n1Zoj && !n2Zoj)
return GCD((n2-n1)/2, n1);
}
当(n1Zoj && n2Zoj)
评估为true时,您该怎么办?您致电
return 2 * GCD(n1, n2);
使用调用函数>完全相同的参数,导致无限递归,吹出的堆栈和堆栈溢出(分割故障)。
protip-学会调试 - 我无法强调非常重要的这是如何。
相关文章:
- 不断收到相同的错误...怎么了?C++
- 我编写了代码将十进制分数转换为其二进制等效数.它编译得很好,但在执行时挂起
- 很好的语法来获取对向量/数组数据的大小引用?
- 我认为我的代码很好,但它在 cin a 之后停止并且没有进一步?
- ICC 中的 -O2 搞砸了汇编程序,ICC 中的 -O1 和 GCC / Clang 中的所有优化都很好
- 是否很好地定义了强制转换为仅由 char[] 组成的结构并从该数组读取?
- 使用 stl 迭代器封装向量是否很好?如果是?怎么可能呢?
- C++20 是否为"overflow"的有符号整数很好地定义了左移?
- "5"怎么了?纸上一切都很好
- 我在哪里可以找到arm-linux-gneabihf-ld的手册页,更不用说写得很好的手册页了,其中包括-rpath和-
- WiFi上的原生C++webrtc客户端降低了视频质量,但对来自chrome的呼叫很好
- 为什么我的.exe文件崩溃了,但在调试和发布版本中工作得很好
- 不能很好地与send()和recv()一起使用,短字符串的第一个字节丢失了.c++
- 第一次填充 QTableWidget 时,一切都很好,但是当我重新填充它时,它明显变慢了
- 当我为调试Lua编译程序时,运行得很好,但为什么我要为发布版编译它,我得到了一个c0000005错误
- 很好地表示了字节数组及其大小
- 没有安装VS2012, c / c++ dll缺少依赖项.有了它,它工作得很好
- visual studio 2010 - ReadFile在VS c++ IDE中工作得很好,但在独立时就失败了
- 代码在Visual Studio中可以很好地编译,但在使用g++的Unix终端中就不行了.我在多维向量处得到一个错误.为
- GLSL编译器是否得到了很好的优化?