NWD, algorytm Euklidesa
#include <utility>
#include <stdexcept>
template<typename T>
T _NWD(T a, T b){
if(a<b)
std::swap(a, b);
while(b!=0) {
const T reszta = a%b;
a = b;
b = reszta;
}
return a;
}
template<typename T>
inline T NWD(const T a, const T b) {
if(a<0 or b<0)
throw std::invalid_argument("Co najmniej jeden z argumentow jest ujemny");
return _NWD(a, b);
}
#ifdef DEBUG
#include <iostream>
int main(){
std::clog << __cplusplus << std::endl;
int a, b;
while(std::cin >> a >> b){
std::cout << "NWD(" << a << ", " << b << ")="<< NWD(a, b) << '\n';
}
}
#endif
|
run
| edit
| history
| help
|
0
|
|
|