Jump to content

Rezolvare Lista Probleme Incepatori


Kid Koder

Recommended Posts

Am scris rezolvarile celor 3 probleme din lista pe care am dat-o acum ceva timp:

 

       --> Aceasta este lista.

 

Problema 1 : interviu

#include <fstream>

using namespace std;

ifstream fin("interviu.in");
ofstream fout("interviu.out");

int n, i, nr, mn;

int main() {
   fin >> n;

   fin >> nr;
   mn = nr;

   fin >> nr;
   mn = max(nr, mn);

   fin >> nr;
   mn = max(nr, mn);

   for(i = 4;i < n;i++) {
      fin >> nr;

      if(nr > mn) break;
   }

   fout << i << 'n';

   fin.close();
   fout.close();

   return 0;
}

Problema 2 : vintage

#include <fstream>
#include <algorithm>

using namespace std;

ifstream fin("vintage.in");
ofstream fout("vintage.out");

struct masina {
   int cost, val, k;
};

masina v[1001];

int i, n, s, vf, k;

bool cmp(masina a, masina b);

int main() {
   fin >> n >> k;

   for(i = 1;i <= n;i++) {
      fin >> v[i].cost >> v[i].val;
      v[i].k = i;
   }

   sort(v + 1, v + 1 + n, cmp);

   for(i = 1;i <= k;i++) {
      s += v[i].cost;
      vf += v[i].val;
   }

   fout << vf << ' ' << s << 'n';

   for(i = 1;i <= k;i++) {
      fout << v[i].k << ' ';
   }

   fout << 'n';

   fin.close();
   fout.close();

   return 0;
}

bool cmp(masina a, masina b) {
   if(a.val > b.val) return true;
   else if(a.val == b.val && a.cost < b.cost) return true;
   return false;
}

Problema 3 : prieten

#include <fstream>

using namespace std;

ifstream fin("prieten.in");
ofstream fout("prieten.out");

long long n, k, s, i;

int main() {
   fin >> n >> k;

   for(i = 1;i <= k;i++) {
      fout << i * n + i << ' ';
   }

   fout << 'n';

   fin >> s;

   s *= 2;

   for(i = 1;i <= 100000;i++) {
      if((i - 1) * i * (i + 1) == s) {
         fout << i << 'n';
         break;
      }
   }

   fin.close();
   fout.close();

   return 0;
}

Daca iti iese un program din prima, inseamna ca ceva e gresit...

Link to comment
Share on other sites

  • 4 months later...

La a 3-a problema, de unde ai stiut ca de doua ori suma prietenilor, este egala cu produsul numarului, predecesorului si succesorului sau?

E vreo formula sau ceva?

Punctul unu l-am facut singur, dar la b a trebuit sa ma uit ca nu aveam nicio idee.

Si de ce cauti pana la 100000? i<=s ar fi de ajuns. Numarul ala doar nu or sa fie mai mare decat 2*suma prietenilor sai, din moment ce prietenii sunt mai mari decat numarul respectiv.

Am vazut ca ai pus break, totusi :D

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.