Advertisement
Guest User

al k-lea element din intercls nush plm

a guest
Jan 23rd, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4.  
  5. using namespace std;
  6.  
  7.  
  8. ifstream fin("date.in");
  9.  
  10. int a[100], n, b[100], m, k;
  11.  
  12. void ctr() {
  13.  
  14. fin >> n >> m >> k;
  15. for(int i = 0; i < n; ++i)
  16. {
  17. fin >> a[i];
  18. }
  19. for(int i = 0; i < m; ++i)
  20. {
  21. fin >> b[i];
  22. }
  23.  
  24. cout << "n = " << n << '\n';
  25. for(int i = 0; i < n; ++i)
  26. {
  27. cout << a[i] << " ";
  28. }
  29. cout << '\n';
  30. cout << "m = " << m << '\n';
  31. for(int i = 0; i < m; ++i)
  32. {
  33. cout << b[i] << " ";
  34. }
  35.  
  36. cout << '\n';
  37. }
  38.  
  39. void calc(){
  40.  
  41. if(n < m) {
  42. // a < b poti sa le interschimbi cand le citesti sa fie mereu a mai mic, mie mi-e lene acuma
  43.  
  44. int ok = 0;
  45. int temp, poz = n/2;
  46. int rasp = 0;
  47. while(ok == 0 && poz >= 0 && poz < n) {
  48. temp = a[poz-1];
  49. cout << "verif poz " << poz << ": " << b[k - poz -1] << " < " << temp << " < " << b[k - poz] << '\n';
  50.  
  51. if((b[k - poz -1] <= temp) && (temp < b[k - poz])) {
  52. ok = 1;
  53. rasp = temp;
  54. }
  55.  
  56. else
  57. if(b[k - poz -1] > temp) poz++;
  58. else if(temp > b[k - poz]) poz--;
  59. }
  60.  
  61. cout << "raspuns = " << rasp;
  62. }
  63.  
  64. }
  65.  
  66.  
  67.  
  68. int main()
  69. {
  70. ctr();
  71. calc();
  72. return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement