Advertisement
rotti321

Test 20 BAC2020

Dec 8th, 2021
952
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. //Test 20 BAC2020
  2. SI ex 2
  3.     r   i
  4. f(3)    0   1   r=0+1+f(2)=0+1+4=5
  5.         2   r=5+2+f(1)=5+2+1=8
  6.         3   r=8+3+f(0)=8+3+0=11
  7.  
  8. f(0) -->0   f(0)=0
  9. f(1)    0   1   r=0+1+f(0)=0+1+0=1
  10. f(1)=1
  11. f(2)    0   1   r=0+1+f(1)=0+1+1=2
  12.         2   r=2+2+f(0)=2+2+0=4
  13. f(3)=11
  14.  
  15. SII ex 3
  16. #include <iostream>
  17. #include <cstring>
  18. using namespace std;
  19.  
  20.  
  21.  
  22. int main()
  23. {
  24.     char a[7][7]={};
  25.     for(int i=0; i<7; i++){
  26.         for(int j=0; j<7; j++){
  27.            if((i >= j && i+j >= 7-1)||(i<=j && i+j <= 7-1)) {
  28.             a[i][j]='a';
  29.            }
  30.            else{
  31.             a[i][j]='b';
  32.            }
  33.            cout<<a[i][j];
  34.         }
  35.         cout<<endl;
  36.     }
  37.  
  38.  
  39.     return 0;
  40. }
  41. ///SIII ex 1
  42.  
  43. #include <iostream>
  44. #include <cstring>
  45. using namespace std;
  46.  
  47.  
  48. int transformareBaza10(int b, int n){
  49.     int k=0;
  50.     int c,x=0,p=1;
  51.     while(n){
  52.         c=n%10;
  53.         n=n/10;
  54.         x=x+c*p;
  55.         p=p*b;
  56.     }
  57.     return x;
  58. }
  59.  
  60. int main()
  61. {
  62.     cout<<transformareBaza10(2,10010);
  63.  
  64.     return 0;
  65. }
  66.  
  67. ///SIII ex 2
  68. #include <iostream>
  69. #include <cstring>
  70. using namespace std;
  71. char s[101];
  72. char aux[101];
  73. char cuv[101];
  74. char *p;
  75. int main() {
  76. cin.getline(s,100);
  77. p=strtok(s," ");
  78. while (p!=NULL)
  79. {
  80.     strcpy(cuv,p);
  81.     if(strchr(p,',')!=NULL)
  82.     {
  83.         int poz=strchr(p,',')-p;
  84.         cuv[poz]='\0';
  85.     }
  86.     strcat(aux,cuv);
  87.     strcat(aux," ");
  88.     p=strtok(NULL," ");
  89. }
  90. cout<<aux;
  91.     return 0;
  92. }
  93.  
  94. ///SIII ex 3
  95. #include <iostream>
  96. #include <fstream>
  97.  
  98. using namespace std;
  99.  
  100. ifstream fin("bac.txt");
  101.  
  102. int main() {
  103.     int x, maxx = -1, last_val, s = 0;
  104.     fin >> x;
  105.     s = x;
  106.     last_val = x;
  107.     while(fin >> x) {
  108.         if(last_val % 2 == x % 2) {
  109.             s = s + x;
  110.         }
  111.         else {
  112.             if(s > maxx) {
  113.                 maxx = s;
  114.             }
  115.             s = x;
  116.         }
  117.         last_val = x;
  118.     }
  119.     if(s > maxx) maxx = s;
  120.     cout << maxx;
  121. }
  122.  
  123. /*
  124. Alg este eficient dpdv al timpului de exec deoarece
  125. are o complexitate O(n), unde n repr nr de elem din fisier
  126. Alg este eficient dpdv al mem utilizate deoarece
  127. am folosit doar patru var intregi simple.
  128.  
  129. Algoritmul foloseste o parcurgere liniara in care
  130. retinem doua elem consecutive last_val si x, in
  131. aceasta ordine. Sunt tratate doua cazuri:
  132.  - cand x si last_val au aceeasi paritate
  133.    atunci crestem valoarea sumei
  134.  - cand x si last_val nu au aceeasi paritate
  135.    atunci actualizam suma maxima existenta pana
  136.    in acel moment dat.
  137.    Se trateaza si cazul in care ultima secv este
  138.    cu suma maxima.
  139.    La sfarsitul alg se afiseaza suma maxima.
  140. */
  141.  
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement