Kamrul13981

Untitled

Aug 15th, 2021
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. //Name:Md.Kamrul Hasan
  2. //ID:201-15-13981
  3.  
  4. //1.phonebook
  5.  #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. void kamrul_swap(string* a, string* b)
  11. {
  12.     string t = *a;
  13.     *a = *b;
  14.     *b = t;
  15. }
  16.  
  17.  
  18. int kamrul_partition (string arr[], int low, int high)
  19. {
  20.     string pivot = arr[high];  
  21.     int i = (low - 1);  
  22.  
  23.     for (int j = low; j <= high- 1; j++)
  24.     {
  25.        
  26.         if (arr[j] <= pivot)
  27.         {
  28.             i++;    // increment index of smaller element
  29.             kamrul_swap(&arr[i], &arr[j]);
  30.         }
  31.     }
  32.     kamrul_swap(&arr[i + 1], &arr[high]);
  33.     return (i + 1);
  34. }
  35.  
  36.  
  37. void kamrul_quickSort(string arr[], int low, int high)
  38. {
  39.     if (low < high)
  40.     {
  41.  
  42.         int pi = kamrul_partition(arr, low, high);
  43.  
  44.  
  45.         kamrul_quickSort(arr, low, pi - 1);
  46.         kamrul_quickSort(arr, pi + 1, high);
  47.     }
  48. }
  49.  
  50. int kamrul_binarySearch(string arr[], int l, int r, string x)
  51. {
  52.     if (r >= l) {
  53.         int mid = l + (r - l) / 2;
  54.  
  55.  
  56.         if (arr[mid] == x)
  57.             return mid;
  58.  
  59.  
  60.         if (arr[mid] > x)
  61.             return kamrul_binarySearch(arr, l, mid - 1, x);
  62.  
  63.         return kamrul_binarySearch(arr, mid + 1, r, x);
  64.     }
  65.  
  66.     return -1;
  67. }
  68.  
  69. int main()
  70. {
  71.     int n, i;
  72.     cout << "Number of your students: ";
  73.     cin >> n;
  74.  
  75.     string kamrul_name[n], kamrul_phn[n], x;
  76.  
  77.     cout << "\n";
  78.     for(i = 0; i < n; i++) {
  79.         cin.ignore();
  80.         cout << "Name : ";
  81.         cin >> kamrul_name[i];
  82.         cin.ignore();
  83.         cout << "Phone : ";
  84.         cin >>  kamrul_phn[i];
  85.     }
  86.  
  87.     cout << "\nSearch Friend: ";
  88.     cin >> x;
  89.  
  90.     kamrul_quickSort(kamrul_name, 0, n-1);
  91.  
  92.     cout << "Contact List\n";
  93.     for(i = 0; i < n; i++) {
  94.         cout << "Name : ";
  95.         cout << kamrul_name[i] << endl;
  96.         cout << "Phone : ";
  97.         cout << kamrul_phn[i] << endl;
  98.     }
  99.  
  100.     cout << "\n";
  101.     int r = kamrul_binarySearch(kamrul_name, 0, n - 1, x);
  102.  
  103.     if(r == -1) {
  104.         cout << "Contact Details not Found\n";
  105.     }
  106.     else {
  107.         cout << "Contact Details Found\n";
  108.         cout << "Name : ";
  109.         cout << kamrul_name[r] << endl;
  110.         cout << "Phone : ";
  111.         cout <<  kamrul_phn[r] << endl;
  112.     }
  113.  
  114.     return 0;
  115. }
  116.  
  117.  
  118.  
  119. //2.knapsak
  120. #include <bits/stdc++.h>
  121.  
  122. using namespace std;
  123.  
  124.  
  125. struct Item
  126. {
  127.     int value, weight;
  128.  
  129.     Item(int value, int weight)
  130.         : value(value), weight(weight){}
  131. };
  132.  
  133. bool kamrul_cmp(struct Item a,struct Item b)
  134. {
  135.     double r1 = (double)a.value / a.weight;
  136.     double r2 = (double)b.value / b.weight;
  137.     return r1 > r2;
  138. }
  139.  
  140.  
  141. double kamrul_Knapsack(struct Item kamrul_arr[],
  142.                           int N, int size)
  143. {
  144.  
  145.     sort(kamrul_arr, kamrul_arr + size, kamrul_cmp);
  146.  
  147.  
  148.     int curWeight = 0;
  149.  
  150.     double finalvalue = 0.0;
  151.  
  152.     for (int i = 0; i < size; i++)
  153.     {
  154.  
  155.  
  156.         if (curWeight + kamrul_arr[i].weight <= N)
  157.         {
  158.             curWeight += kamrul_arr[i].weight;
  159.             finalvalue += kamrul_arr[i].value;
  160.         }
  161.  
  162.  
  163.         else
  164.         {
  165.             int remain = N - curWeight;
  166.             finalvalue += kamrul_arr[i].value
  167.                           * ((double)remain
  168.                              / kamrul_arr[i].weight);
  169.  
  170.             break;
  171.         }
  172.     }
  173.  
  174.     return finalvalue;
  175. }
  176.  
  177.  
  178. int main()
  179. {
  180.  
  181.     int N = 15;
  182.  
  183.  
  184.     Item kamrul_arr[] = { { 5, 10 },
  185.         { 3, 4 },
  186.         { 5, 3 },
  187.         { 7, 6 },
  188.         {6,2}
  189.     };
  190.  
  191.     int size = sizeof(kamrul_arr) / sizeof(kamrul_arr[0]);
  192.  
  193.  
  194.     cout << "Maximum profit earned = "
  195.          << kamrul_Knapsack(kamrul_arr, N, size);
  196.     return 0;
  197. }
  198.  
Advertisement
Add Comment
Please, Sign In to add comment