Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <ctype.h>
- #include <string>
- #include <iostream>
- #include <stdlib.h> /* srand, rand */
- #include <time.h> /* time */
- using namespace std;
- bool isSquare(double n);
- bool isPrime(double n);
- int gcd(int a, int b);
- int lcm(int a, int b, int gcd);
- //1. kiem tra so nguyen
- int checkType(double n){
- bool isInt = ((int)(n) == n);
- //Kiem tra sau do luu ket qua vao isN
- bool isN[3] = {isInt, isPrime(n), isSquare(n)};
- string numType[3] = {"nguyen", "nguyen to", "chinh phuong"};
- for(int i = 0; i < 3; i++){
- string neg = (isN[i]) ? "" : " khong phai";
- //them phu dinh vao cau neu N khong phai la so nguyen
- cout << n << neg + " la so " + numType[i] << "\n";
- }
- }
- //2. kiem tra uoc boi
- int checkCommon(int a, int b){
- int GCD = gcd(a, b);
- printf("\nUoc so lon nhat cua 2 so la: %i", GCD);
- printf("\nBoi so nhat cua 2 so la: %i", lcm(a, b, GCD));
- }
- //3. tinh tien karaoke
- int karaokeProfit(int start, int end){
- //tra ve 0 neu khung gio khong hop le
- if(start < 12 || end > 23) return 0;
- const int price = 50000;
- int total = 0;
- int time = end - start;
- //neu time > 4, giam so tien di 30% voi nhung tieng tiep theo
- if(time >= 4) total = 150000 + (((time - 3)*50000)*0.7);
- else total = time * price;
- //giam them neu bat dau tu 14-17h;
- if(start >= 14 && start <= 17){
- total = (total * 0.9);
- }
- return total;
- }
- //4. tinh tien dien
- void getTienDien(int kw){
- int tong = 0;
- //luu tru so kw su dung va bang gia
- //neu kw > 400, for loop se dung kw de tinh tong
- int kwLookup[7] = {0, 50, 100, 200, 300, 400, kw};
- int giaLookup[6] = {1678, 1734, 2014, 2536, 2834, 2927};
- for (int i = 0; i < 6; i++){
- //kiem tra xem kw trong bac nao. VD: 100 < kw < 200 => bac 2. Cong vao tong dua vao bac.
- if(kw >= kwLookup[i + 1]){
- tong += (kwLookup[i+1] - kwLookup[i]) * giaLookup[i];
- }
- //neu kw < kwLookup => da tim thay bac. Tinh not tong va end for.
- else{
- tong += (kw - kwLookup[i]) * giaLookup[i];
- break;
- }
- }
- printf("\nSo tien phai dong la: %i", tong);
- }
- //5. doi tien
- void getChange(int amount){
- //left la so tien con phai tra.
- int left = amount;
- const int changeLookup[] = {500,200,100,50,20,10,5,2,1};
- //lay do dai cua mang change
- const int length = sizeof(changeLookup)/sizeof(changeLookup[0]);
- printf("\nMenh gia tien doi ra la: ");
- for(int i = 0; left > 0; i++){
- //chi tra ve menh gia nho hon left
- if(left > changeLookup[i]){
- //in ra man hinh tren cung dong so tien phai tra
- printf("%i ", changeLookup[i]);
- left -= changeLookup[i];
- i--;
- }
- //khi left == thi 1 ket thuc chuong trinh
- else if (left == 1){
- printf("1\n");
- return;
- }
- }
- }
- //6. vay tien
- void loanWInterest(int total){
- int current;
- float interest;
- for(int i = 1; i <= 12; i++){
- //so tien con phai tra
- current = total - (total/12) * i;
- //lai xuat 5% cua thang hien tai
- interest = current * 0.05;
- printf("\nSo tien phai tra ky han %i la: %i ", i, (total/12) + (int)interest);
- }
- }
- //7. vay theo phan tram
- void loanUpfront(int percent){
- //gia cua xe,
- int current, total = 500000000;
- float interest;
- //tong so tien phai tra lan dau
- int firstTime = (total / 100) * (100 - percent);
- printf("\nSo tien phai tra lan dau: %i", firstTime);
- total -= firstTime;
- for(int i = 1; i <= 12; i++){
- //so tien con phai tra
- current = total - (total/12) * i;
- //tra them 7.2% lai xuat theo thang
- interest = current * 0.072;
- printf("\nSo tien phai tra ky han %i la: %i", i, total / 12 + (int)interest);
- }
- }
- void studentClassify(float score, string name){
- if(score < 0 || score > 10){
- printf("\nDiem khong hop le");
- return;
- }
- string hocLuc = "";
- if(score >= 9) hocLuc = "xuat sac";
- else if(score >= 8) hocLuc = "gioi";
- else if(score >= 6.5) hocLuc = "kha";
- else if(score >= 5) hocLuc = "trung binh";
- else if(score >= 3.5) hocLuc = "yeu";
- else hocLuc = "kem";
- }
- void lottery(int n){
- // khoi tao seed ngau nhien:
- srand (time(NULL));
- int win = rand() % 15 + 1;
- }
- bool isSquare(double n){
- //lay can bac 2 cua n nhan voi chinh no, tra ve true neu ket qua = n
- int n2 = sqrt(n);
- if (n2 * n2 == n) return true;
- return false;
- }
- bool isPrime(double n)
- {
- if(n < 2) return false;
- // Tim so chia dc cho n tu 2 den n-1. Neu tim dc thi` tra ve false.
- for (int i = 2; i < n; i++){
- if((int)n % i == 0) return false;
- }
- return true;
- }
- //dung recursion de tim boi so
- int gcd(int a, int b)
- {
- if (a == 0) return b;
- return gcd(b % a, a);
- }
- //dung boi so de tim uoc so
- int lcm(int a, int b, int gcd){
- return (a * b) / gcd;
- }
Add Comment
Please, Sign In to add comment