Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by minhtienbukai501 ( 5 years ago )
#include <iostream>
#include<string>
using namespace std;

bool KiemTraLonHon(string a, string b)
{
	int leng_a = a.length();
	int leng_b = b.length();

	if (leng_a > leng_b)
		return true;
	else
		return false;

	if (a[0] == '-')
	{
		return false;
	}
	else if (b[0] == '-')
	{
		return true;
	}

	if (leng_a > leng_b)
	{
		b.insert(0, leng_a - leng_b, '0');
	}
	else
	{
		a.insert(0, leng_b - leng_a, '0');
	}

	int leng = a.length();
	for (int i = 0; i < leng; i++)
	{
		if (a[i] > b[i])
			return true;
	}


	return false;
}

string PhepTru(string a, string b)
{
	int leng_a = a.length();
	int leng_b = b.length();
	bool check = KiemTraLonHon(a, b);
	
	if (leng_a > leng_b)
	{
		b.insert(0, leng_a - leng_b, '0');
	}
	else
	{
		a.insert(0, leng_b - leng_a, '0');
	}

	int Nho = 0;
	string Hieu;
	
	if (check == true)
	{
		int leng = a.length();
		for (int i = leng - 1; i >= 0; i--)
		{
			int tru;
			if (a[i] < (b[i] + Nho))
			{
				tru = 10 + (a[i] - 48) - (b[i] - 48 + Nho);
				Nho = 1;
			}
			else
			{
				tru = (a[i] - 48) - (b[i] - 48 + Nho);
				Nho = 0;
			}

			Hieu.insert(0, 1, tru + 48);
		}
	}
	else if (check == false)
	{
		int leng = a.length();
		for (int i = leng - 1; i >= 0; i--)
		{
			int tru;
			if (b[i] < (a[i] + Nho))
			{
				tru = 10 + (b[i] - 48) - (a[i] - 48 + Nho);
				Nho = 1;
			}
			else
			{
				tru = (b[i] - 48) - (a[i] - 48 + Nho);
				Nho = 0;
			}

			Hieu.insert(0, 1, tru + 48);
		}

		Hieu.insert(0, 1, '-');
	}

	return Hieu;
}

string TimUocChung(string a, string b)
{
	
	while (a != b)
	{
		bool check = KiemTraLonHon(a, b);

		if (check == true)
		{
			a = PhepTru(a, b);
			cout << "\n a = " << a; 
		}
		else
		{
			b = PhepTru(a, b);
			cout << "\nb = " << b;
		}
	}

	return a;
}

int main()
{
	string a;
	getline(cin, a);
	string b;
	getline(cin, b);

	string Hieu = PhepTru(a, b);
	cout << "\nHieu = " << Hieu;
	
	string k = TimUocChung(a, b);
	cout << "\n k = " << k;

	
	
	return 0;
}

 

Revise this Paste

Your Name: Code Language: