Advertisement
Cosmin3105

Reducere

Nov 1st, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6.  
  7. struct Reduc
  8. {
  9.     char raspuns[10];
  10.     int k;
  11.     int nrMax;
  12. };
  13.  
  14. Reduc reducere(int a[10000], int b[10000], int n, int m)
  15. {
  16.     Reduc rezult;
  17.  
  18.     int ok = 1;
  19.     int s = 0;
  20.     int i = 1, j = 1;
  21.     int k ,nrMax = 0, nr = 0;
  22.     while(i <= n && j <= m){
  23.         s += a[i];
  24.         nr++;
  25.         if(s == b[j]){
  26.             if(nr > nrMax){
  27.                 nrMax = nr;
  28.                 k = j;
  29.             }
  30.             nr = 0;
  31.             j++;
  32.             s = 0;
  33.  
  34.         }
  35.         else if(s > b[j]){
  36.             ok = 0;
  37.             break;
  38.         }
  39.         i++;
  40.     }
  41.  
  42.     if(ok){
  43.         strcpy(rezult.raspuns, "adevarat");
  44.         rezult.k = k;
  45.         rezult.nrMax = nrMax;
  46.     }
  47.     else{
  48.         strcpy(rezult.raspuns, "fals");
  49.         rezult.k = -1;
  50.         rezult.nrMax = -1;
  51.     }
  52.  
  53.     return rezult;
  54. }
  55.  
  56. int main()
  57. {
  58.     int n = 12;
  59.     int a[10000] = {0, 6, 3, 4, 1, 6, 4, 6, 1, 7, 1, 8, 7};
  60.     int m = 4;
  61.     int b[10000] = {0, 13, 7, 18, 16};
  62.  
  63.     cout << reducere(a,b,n,m).raspuns << " " << reducere(a,b,n,m).k << " " << reducere(a,b,n,m).nrMax;
  64.  
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement