Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files BruteForce vs Euclid #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions GCD_BruteForce_UCLN.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//Tinh UCLN bang phuong phap Brute Force
#include<iostream>
using namespace std;

int gcd(int a, int b){
int result = min(a,b); // gan result la so nho nhat trong hai so a va b
while(result > 0){
if(a % result == 0 && b % result == 0) { //neu a va b cung chia het cho result thi dung lai
break;
}
result--; // neu khong se giam result cho den khi ca a va b deu chia het cho result
}
return result;
}

int main(){
int a,b;
cout<<"Nhap hai so nguyen a va b: ";
cin>> a >> b;
cout <<"Tim uoc chung lon nhat cua " << a << " va " << b << " la " << gcd(a,b);

return 0;
}

23 changes: 23 additions & 0 deletions GCD_Euclid_UCLN.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include<bits/stdc++.h>
using namespace std;


//H�m t�nh uoc chung lon nhat (GCD) su dung thuat toan Euclid
int gcd(int a, int b){
while (b != 0 ){ //vong lap tinh GCD theo thuat toan Euclid cho den khi bang 0.
int temp = b; //luu giu gia tri cua a vao bien tam thoi temp.
b = a % b; // gan gia tri a % b vao b.
a = temp; // gan gia tri ban dau cua b vao a.
}
return a;
}

int main(){
int a,b;
cout << "Nhap hai so nguyen duong: ";
cin >> a >> b;
int UCLN = gcd(a,b);
cout << "Uoc chung lon nhat cua " << a << " va " << b << " la " << UCLN;

return 0;
}
8 changes: 8 additions & 0 deletions HelloWorld.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include<iostream>

using namespace std;

int main() {
cout << "Xin chao!";
return 0;
}