Anachronos

Untitled

Jun 16th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.64 KB | None | 0 0
  1. #include <math.h>
  2. #include <ctype.h>
  3. #include <string>
  4. #include <iostream>
  5. #include <stdlib.h>     /* srand, rand */
  6. #include <time.h>       /* time */
  7. using namespace std;
  8.  
  9. bool isSquare(double n);
  10. bool isPrime(double n);
  11.  
  12. int gcd(int a, int b);
  13. int lcm(int a, int b, int gcd);
  14.  
  15. //1. kiem tra so nguyen
  16. int checkType(double n){
  17.     bool isInt = ((int)(n) == n);
  18.     //Kiem tra sau do luu ket qua vao isN
  19.     bool isN[3] = {isInt, isPrime(n), isSquare(n)};
  20.     string numType[3] = {"nguyen", "nguyen to", "chinh phuong"};
  21.  
  22.     for(int i = 0; i < 3; i++){
  23.         string neg = (isN[i]) ? "" : " khong phai";
  24.         //them phu dinh vao cau neu N khong phai la so nguyen
  25.         cout << n << neg + " la so " + numType[i]  << "\n";
  26.     }
  27.    
  28. }
  29.  
  30. //2. kiem tra uoc boi
  31. int checkCommon(int a, int b){
  32.     int GCD = gcd(a, b);
  33.     printf("\nUoc so lon nhat cua 2 so la: %i", GCD);
  34.     printf("\nBoi so nhat cua 2 so la: %i", lcm(a, b, GCD));
  35. }
  36.  
  37. //3. tinh tien karaoke
  38. int karaokeProfit(int start, int end){
  39.     //tra ve 0 neu khung gio khong hop le
  40.     if(start < 12 || end > 23) return 0;
  41.    
  42.     const int price = 50000;
  43.     int total = 0;
  44.     int time = end - start;
  45.    
  46.     //neu time > 4, giam so tien di 30% voi nhung tieng tiep theo
  47.     if(time >= 4) total = 150000 + (((time - 3)*50000)*0.7);
  48.     else total = time * price;
  49.    
  50.     //giam them neu bat dau tu 14-17h;
  51.     if(start >= 14 && start <= 17){
  52.         total = (total * 0.9);
  53.     }
  54.    
  55.     return total;
  56. }
  57.  
  58. //4. tinh tien dien
  59. void getTienDien(int kw){
  60.     int tong = 0;
  61.     //luu tru so kw su dung va bang gia
  62.     //neu kw > 400, for loop se dung kw de tinh tong
  63.     int kwLookup[7] = {0, 50, 100, 200, 300, 400, kw};
  64.     int giaLookup[6] = {1678, 1734, 2014, 2536, 2834, 2927};
  65.    
  66.     for (int i = 0; i < 6; i++){
  67.         //kiem tra xem kw trong bac nao. VD: 100 < kw < 200 => bac 2. Cong vao tong dua vao bac.
  68.         if(kw >= kwLookup[i + 1]){
  69.             tong += (kwLookup[i+1] - kwLookup[i]) * giaLookup[i];
  70.         }
  71.         //neu kw < kwLookup => da tim thay bac. Tinh not tong va end for.
  72.         else{
  73.             tong += (kw - kwLookup[i]) * giaLookup[i];
  74.             break;
  75.         }
  76.     }
  77.     printf("\nSo tien phai dong la: %i", tong);
  78. }
  79.  
  80.  
  81. //5. doi tien
  82. void getChange(int amount){
  83.     //left la so tien con phai tra.
  84.     int left = amount;
  85.     const int changeLookup[] = {500,200,100,50,20,10,5,2,1};
  86.     //lay do dai cua mang change
  87.     const int length = sizeof(changeLookup)/sizeof(changeLookup[0]);
  88.    
  89.     printf("\nMenh gia tien doi ra la: ");
  90.     for(int i = 0; left > 0; i++){
  91.         //chi tra ve menh gia nho hon left
  92.         if(left > changeLookup[i]){
  93.             //in ra man hinh tren cung dong so tien phai tra
  94.             printf("%i ", changeLookup[i]);
  95.             left -= changeLookup[i];
  96.             i--;
  97.         }
  98.         //khi left == thi 1 ket thuc chuong trinh
  99.         else if (left == 1){
  100.             printf("1\n");
  101.             return;
  102.         }
  103.        
  104.     }
  105. }
  106.  
  107.  
  108. //6. vay tien
  109. void loanWInterest(int total){
  110.     int current;
  111.     float interest;
  112.    
  113.     for(int i = 1; i <= 12; i++){
  114.         //so tien con phai tra
  115.         current = total - (total/12) * i;
  116.         //lai xuat 5% cua thang hien tai
  117.         interest = current * 0.05;
  118.        
  119.         printf("\nSo tien phai tra ky han %i la: %i ", i, (total/12) + (int)interest);
  120.     }
  121. }
  122.  
  123. //7. vay theo phan tram
  124. void loanUpfront(int percent){
  125.     //gia cua xe,
  126.     int current, total = 500000000;
  127.     float interest;
  128.  
  129.     //tong so tien phai tra lan dau
  130.     int firstTime = (total / 100) * (100 - percent);
  131.     printf("\nSo tien phai tra lan dau: %i", firstTime);
  132.    
  133.     total -= firstTime;
  134.     for(int i = 1; i <= 12; i++){
  135.         //so tien con phai tra
  136.         current = total - (total/12) * i;
  137.         //tra them 7.2% lai xuat theo thang
  138.         interest = current * 0.072;
  139.         printf("\nSo tien phai tra ky han %i la: %i", i, total / 12 + (int)interest);
  140.     }
  141. }
  142.  
  143. void studentClassify(float score, string name){
  144.     if(score < 0 || score > 10){
  145.         printf("\nDiem khong hop le");
  146.         return;
  147.     }
  148.     string hocLuc = "";
  149.     if(score >= 9) hocLuc = "xuat sac";
  150.     else if(score >= 8) hocLuc = "gioi";
  151.     else if(score >= 6.5) hocLuc = "kha";
  152.     else if(score >= 5) hocLuc = "trung binh";
  153.     else if(score >= 3.5) hocLuc = "yeu";
  154.     else hocLuc = "kem";
  155. }
  156.  
  157.  
  158. void lottery(int n){
  159.     // khoi tao seed ngau nhien:
  160.     srand (time(NULL));
  161.     int win = rand() % 15 + 1;
  162. }
  163.  
  164. bool isSquare(double n){
  165.     //lay can bac 2 cua n nhan voi chinh no, tra ve true neu ket qua = n
  166.     int n2 = sqrt(n);
  167.     if (n2 * n2 == n) return true;
  168.     return false;
  169. }
  170.  
  171. bool isPrime(double n)
  172. {
  173.     if(n < 2) return false;
  174.     // Tim so chia dc cho n tu 2 den n-1. Neu tim dc thi` tra ve false.
  175.     for (int i = 2; i < n; i++){
  176.         if((int)n % i == 0) return false;
  177.     }
  178.     return true;
  179. }
  180.  
  181. //dung recursion de tim boi so
  182. int gcd(int a, int b)
  183. {
  184.     if (a == 0) return b;
  185.     return gcd(b % a, a);
  186. }
  187. //dung boi so de tim uoc so
  188. int lcm(int a, int b, int gcd){
  189.     return (a * b) / gcd;
  190. }
  191.  
  192.    
Add Comment
Please, Sign In to add comment