为什么作业提交网站的输出与Visual Studio不同
Why is the output of the homework submitting website different from Visual Studio?
所以我创建了一个程序来找出五个数字的最大公约数。
当我使用visual studio时,输出很好,但在线系统上的输出变得疯狂。
就像在程序中输入12、24、8、36、100一样,输出将是32758。
有人能回答我吗?
#include<iostream>
using namespace std;
const int LEN = 5;
int x = 0;
// TODO 1: Complete the function declaration of GCD(), takes 2 interger as parameter.
int GCD(int n1, int n2) {
for (int i = 1; i <= n1 && i <= n2; i++) {
if (n1 % i == 0 && n2 % i == 0) {
x = i;
}
}
return x;
}
// TODO 2: Complete the function implementation, return the GCD of 2 given number
int main() {
int Num[LEN];
int answer;
int gcd[4], y, z;
cout << "Enter " << LEN << " numbers:" << endl;
for (int i = 0; i < LEN; i++)
cin >> Num[i];
cout << "GCD:" << endl;
for (int i = 1; i < LEN; i++) {
gcd[i - 1] = GCD(Num[i - 1], Num[i]);
}
for (z = 3; z >= 0; z--) {
if (gcd[z] < gcd[z - 1]) {
y = gcd[z];
gcd[z] = gcd[z - 1];
gcd[z - 1] = y;
}
}
answer = gcd[0];
cout << answer << endl;
return 0;
}
在线系统上的输出
您应该这样计算gcd,而不是每两个连续的数字。
answer = Num[0];
for (int i = 1; i < LEN; i++)
answer = GCD(answer, Num[i]);
cout << answer << endl;
相关文章:
- Visual Studio 2015:Extern "C" 和 "export" 关键字
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 试图在visual studio上用C++创建一个桌面应用程序
- 如何在MS Visual Studio 2019中运行QT UI
- 如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针
- Visual Studio Code - C++ Debugger 無法啟動
- Visual Studio 2019:插入多个C++风格的单行注释
- Visual Studio Code "undefined reference to `WinMain@16'"
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- Visual Studio中的函数声明和函数定义问题
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- Visual Studio mkl_link_tool.exe链接错误
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 不同的Visual Studio版本中缺少.dll
- 用Visual Studio在C++中嵌入Julia
- Visual Studio 2017循环自动向量化问题
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- Visual Studio在尝试读取resource.txt文件时崩溃
- Visual Studio 2017 不允许我创建 C++ 专用模板