Witam!
Mam problem z napisaniem programu do obliczania nwd dwóch liczb w zapisie binarnym. Mój program jest za wolny.
kod:
#include <iostream>
#include <string>
std::string odejmij(const std::string, const std::string);
std::string gcd(std::string, std::string);
int main() {
int lt;
std::cin >> lt;
std::string a;
std::string b;
while(lt--)
{
std::cin >> a >> b;
while ((a.size()>1) && (a[0] == '0'))
a=a.substr(1, a.size()-1);
while ((b.size()>1) && (b[0] == '0'))
b=b.substr(1, b.size()-1);
std::cout << gcd(a, b) << "\n";
a.clear();
b.clear();
}
}
std::string odejmij(const std::string a, const std::string b)
{
std::string w;
std::string::const_reverse_iterator ita = a.rbegin();
std::string::const_reverse_iterator itb = b.rbegin();
bool pozyczenie = false;
char c;
while (itb != b.rend())
{
if (pozyczenie)
{
c=(*ita>=*itb+1?*ita-*itb-1:*ita-*itb+9)+'0';
pozyczenie=*ita<*itb+1;
} else
{
c=(*ita>=*itb?*ita-*itb:*ita-*itb+10)+'0';
pozyczenie=*ita<*itb;
}
w=c+w;
ita++;
itb++;
}
if (ita != a.rend())
{
if (pozyczenie)
{
c=*ita-1;
w=c+w;
ita++;
}
while (ita != a.rend()) {
c = *ita;
w = c + w;
ita++;
}
}
while ((w.size() > 1) && (w[0] == '0'))
w=w.substr(1, w.size()-1);
return w;
}
std::string gcd(std::string a, std::string b)
{
std::string c(b);
while ((a.size()>c.size()+1) || ((a.size()==c.size()+1) && (a>=c+'0')))
c+='0';
if ((a.size()>c.size()) || ((a.size()==c.size()) && (a>=c)))
a = odejmij(a, c);
while ((a.size()>b.size()) || ((a.size()==b.size()) && (a>=b)))
a = odejmij(a, b);
return a!="0"?gcd(b, a):b;
}
dziękuję za wszelką pomoc!