我试图找到最大的共同因素.
我编写了一个糟糕的(操作密集型)算法,将较低的值减1,检查使用%来查看它是否均匀地除以分子和分母,如果是,则退出程序.但是,我的while循环不使用和运算符,因此一旦分子可被整除,它就会停止,即使它不是正确的答案.
我使用的数字是54和42,正确的GCD(最大公分母)是6.
#heres a simple algorithm to find the greatest common denominator:
iterations = 0; #used to calculate number of times while loop is executed
u = 54; v= 42; d = v-1; #u is the numerator, v is the denominator, d is the number decremented by one
while ((v % d !=0) & (u % d != 0)): #while both numerator AND denominator cannot be evenly divided by the decremented number
d -= 1 #decrement the number by …Run Code Online (Sandbox Code Playgroud) 刚刚学习R,我认为在unix终端中以批处理模式使用它而不是在R终端中写入会很棒.
所以我决定写test.r
x <- 2
print(x)
Run Code Online (Sandbox Code Playgroud)
然后在终端我做了
R CMD BATCH test.r
Run Code Online (Sandbox Code Playgroud)
它运行,但输出test.r.Rout文件.我可以通过运行R CMD BATCH test.r out.txt来输出它来说出一个文本文件.
问题是,是否可以将输出打印到终端?
应该是直截了当的,但是当我编译C++ Hello World代码时,会返回一堆未定义的符号错误.
// my first program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
下面是我如何编译:打开终端,cd到目录,gcc hello.cpp
然后我得到了错误.有什么想法吗?我觉得我可能已经打破了某些......或者我只是遗漏了一些非常明显的东西.任何帮助是极大的赞赏!
继承人的错误:
Undefined symbols for architecture x86_64:
"std::cout", referenced from:
_main in ccfUAf5i.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
_main in ccfUAf5i.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccfUAf5i.o
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in ccfUAf5i.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)