Jump to content

[C++] pbInfo


Andreigl
 Share

Recommended Posts

Link: https://www.pbinfo.ro/solutii/user/neauzitul/problema/244/cifreord

 

#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

int Array[100000], arrSize = 0;


int main()
{
    ifstream Read("cifreord.in");
    ofstream Write("cifreord.out");

    int N, Count = 0;
    Read >> N;

    for (int i=0; i<N; i++)
        Read >> Array[i];

    sort(Array, Array+N);

    for (int i=0; i<N; i++)
    {
        if (Count == 20)
        {
            Write << endl;
            Count = 0;
        }

        Write << Array[i] << " ";
        Count++;
    }

    Read.close();
    Write.close();
}

 

Link to comment
Share on other sites

#include <iostream>
#include <fstream>

using namespace std;

bool VerifPrim(int Numar)
{
    if (Numar < 2 or Numar % 2 == 0)
        return false;

    if (Numar == 2)
        return true;

    for (int i=3; (i*i)<=Numar; i+=2)
    {
        if (Numar % i == 0)
            return false;
    }

    return true;
}

int main()
{
    ifstream Read("ciffrecv.in");
    ofstream Write("ciffrecv.out");

    int N, Max = 0, Aparitie = 0;

    while (Read >> N)
    {
        if (VerifPrim(N))
        {
            if (N > Max)
                Max = N, Aparitie = 1;
            else
                if (N == Max)
                    Aparitie++;
        }
    }

    Write << Max << " " << Aparitie;

    Read.close();
    Write.close();
}

 

Link: https://www.pbinfo.ro/probleme/187/ciffrecv

Link to comment
Share on other sites

#include <iostream>

using namespace std;

int Vector[1005];

int main()
{
    int N, Numar, FirstNumber, SecondNumber;
    
    cin >> N;

    FirstNumber = SecondNumber = 0;

    for (int i=1; i<=N; i++)
    {
        cin >> Numar;

        if (Numar < 1000)
            Vector[Numar] = 1;
    }

    for (int i=999; i>=100; i--)
    {
        if (Vector[i] != 1 && FirstNumber == 0)
            FirstNumber = i;
        else
        {
            if (Vector[i] != 1 && SecondNumber == 0)
                SecondNumber = i;
        }

        if (SecondNumber != 0 and FirstNumber != 0)
            break;
    }

    if (SecondNumber == 0 or FirstNumber == 0)
        cout << "NU EXISTA";
    else
    cout << SecondNumber << " " << FirstNumber;
}

 

Link: https://www.pbinfo.ro/probleme/525/numere1

Link to comment
Share on other sites

Varianta autorului: 

 

#include <fstream>
using namespace std;
int x,i,v[10020];
int main()
{
    ifstream f("numere8.in");
    ofstream g("numere8.out");
    while(f>>x)
        if (x<=9999) v[x]=1;
        
    for(i=9999; i>=1; i--)
        if (v[i]==0) g<<i<<" ";
}

Link: https://www.pbinfo.ro/probleme/1005/numere8

 

#include <iostream>

using namespace std;

int main()
{
    int X;
    cin >> X;

    int Numar[X], Min = 39e+69, Max = 0;

    for (int i=0; i<X; i++)
        cin >> Numar[i];

    for (int i=0; i<X; i++)
    {
        if (Min > Numar[i])
            Min = Numar[i];

        if (Numar[i] > Max)
            Max = Numar[i];
    }

    cout << Min << " " << Max;
}

Link: https://www.pbinfo.ro/probleme/486/minmax0

#include <iostream>

using namespace std;

int main()
{
    int X;
    cin >> X;

    long long int Numar[X];

    for (int i=0; i<X; i++)
        cin >> Numar[i];

    for (int i=0, j=X-1; i<=j; i++, j--)
    {
        cout << Numar[i] << " ";

        if (i<j)
            cout << Numar[j] << " ";
    }
}

Link: https://www.pbinfo.ro/probleme/489/afisare1

Edited by shanker'
Link to comment
Share on other sites

17 hours ago, shanker' said:

 


#include <iostream>

using namespace std;

int main()
{
    int X;
    cin >> X;

    int Numar[X], Min = 39e+69, Max = 0;

    for (int i=0; i<X; i++)
        cin >> Numar[i];

    for (int i=0; i<X; i++)
    {
        if (Min > Numar[i])
            Min = Numar[i];

        if (Numar[i] > Max)
            Max = Numar[i];
    }

    cout << Min << " " << Max;
}

Link: https://www.pbinfo.ro/probleme/486/minmax0

Critici (constructive):

  • Cand rezolvi o problema de informatica, te uiti la restrictii: "elementele vectorului vor fi mai mici decât 1000000". Deci la tine min ar trebui sa porneasca de la 1000000 (mai mic decat ceva inseamna mai mic strict, nu si egal), nu de la INT_MAX (oricum nu este un procedeu bun sa hardcodezi asa valoarea, vezi exemplul meu cum ar trebui scris)
  • "int Numar[X]" -> nu toate compilatoarele accepta aceasta sintaxa, ti-as sugera sa citesti despre alocarea dinamica sau despre vectori (in exemplul meu voi folosi vector), oricum, din nou, enuntul problemei iti sare in ajutor: "0 < n < 1000" deci poti scrie "int Numar[1001]" lejer. (Se da asa in enunt pentru ca aceasta problema este conceputa pentru clase gimnaziale (5-6), stiu ca pe site scrie clasa a 9-a dar copii care se apuca de programare de tineri o fac mult mai repede).

Voi folosi 2 exemple, sa vezi doua moduri diferite de rezolvare:

  1. nu salvezi numerele intr-un tablou unidimensional (nu ai de ce, consuma timp si e mai eficient sa rezolvi problema direct din citire)
  2. aloci dinamic un tablou unidimensional pentru cate elemente ai nevoie si aplici algoritmi deja existenti pe el

Varianta 1:

#include <algorithm> // min, max
#include <limits> // numeric_limits
#include <iostream> // cin, cout

int main() {
    int current_max = std::numeric_limits<int>::min(); // INT_MIN
    int current_min = std::numeric_limits<int>::max(); // INT_MAX
    
    int n; std::cin >> n;
    int current_item;
    
    while (n--) {
        std::cin >> current_item;
        current_max = std::max(current_max, current_item);
        current_min = std::min(current_min, current_item);
    }
    
    std::cout << current_min << " " << current_max << "\n";
    
    return 0;
}

Varianta 2:

#include <algorithm> // min_element, max_element
#include <iostream> // cin, cout
#include <vector> // vector

int main() {
    int n; std::cin >> n;
    std::vector<int> input(n); // alocare dinamica in constructor (c.f. clase)
    
    for (auto& elem : input) { // c++'s foreach
        std::cin >> elem;
    }
    
    std::cout << *std::min_element(input.begin(), input.end()) << " ";
    std::cout << *std::max_element(input.begin(), input.end()) << "\n";
    
    return 0;
}

Concluzie: Keep the code clean and easy to read & understand.

Edited by Vesca
Link to comment
Share on other sites

Link: https://www.pbinfo.ro/probleme/88/palindrom

#include <fstream>
#include <cctype>
#include <string.h>

bool Palindrom(std::string myString)
{
    std::string Palindrom_Check;

    for (int contor = myString.length() - 1; contor >= 0; contor--)
    {
        myString[contor] = std::tolower(myString[contor]);
        Palindrom_Check += myString[contor];
    }

    if (!strcmp(myString.c_str(), Palindrom_Check.c_str()))
        return true;
    else
        return false;
}

int main()
{
    std::ifstream file_In("palindrom.in");
    std::ofstream file_Out("palindrom.out");

    int N;
    file_In >> N;

    std::string read_File;
    for (int contor = 0; contor < N; contor++)
    {
        file_In >> read_File;
        
        if (Palindrom(read_File))
            file_Out << 1 << "\n";
        else
            file_Out << 0 << "\n";
    }

    return 0;
}

 

Link: https://www.pbinfo.ro/probleme/82/minmax

#include <fstream>

int main()
{
    std::ifstream file_in("minmax.in");
    std::ofstream file_out("minmax.out");

    int N; file_in >> N;
    int Maxim, Minim, Numar;
    Maxim = 0, Minim = 1000000;

    for (int contor = 0; contor < N; contor++)
    {
        file_in >> Numar;
        if (Numar > Maxim)
            Maxim = Numar;
        if (Minim > Numar)
            Minim = Numar;
    }

    file_out << Minim << " " << Maxim;
}
Edited by shanker'
Link to comment
Share on other sites

https://www.pbinfo.ro/probleme/3270/suma-divizorilor-pari

 

#include <iostream>

int main()
{
    int N; std::cin >> N;
    long long int Suma = 0;

    for (int i = 1; i * i <= N; i++)
    {
        if (N % i == 0)
        {
            if(!(i & 1))
                Suma += i;

            if (i * i < N && !(N/i & 1))
                Suma += N / i;
        }
    }

    std::cout << Suma;
    return 0;
}
Link to comment
Share on other sites

https://www.pbinfo.ro/solutii/user/neauzitul/problema/3272/sumdivogl

 

Method 1 by @Vesca:

 

#include <algorithm>
#include <iostream>
#include <string>

int main()
{
    int N; std::cin >> N;
    long long int sum = 0;
    int current_num;

    while (std::cin >> current_num) {
        std::string str_num = std::to_string(current_num);
        std::reverse(str_num.begin(), str_num.end());
        current_num = std::stoi(str_num);

        int div;
        for (div = 1; div * div < current_num; ++div) {
            sum += (div + current_num / div) * (current_num % div == 0);
        }
        sum += div * (div * div == current_num);
    }

    std::cout << sum << "\n";

    return 0;
}

 

Method 2:

#include <iostream>

int Oglindit(int Numar)
{
    int Oglindit = 0;
    while (Numar)
    {
        Oglindit = Numar % 10 + Oglindit * 10;
        Numar /= 10;
    }

    return Oglindit;
}

long long int Divizori(int Numar)
{
    long long int Suma = 0;
    for (int i = 1; i * i <= Numar; i++)
    {
        if (Numar % i == 0)
        {
            Suma += i;

            if (i * i < Numar)
                Suma += Numar / i;
        }
    }

    return Suma;
}

int main()
{
    int N, Numar;
    long long int Suma = 0;
    std::cin >> N;

    for (; N > 0; N--)
    {
        std::cin >> Numar;
        Suma += Divizori(Oglindit(Numar));
    }

    std::cout << Suma;
}
Edited by shanker'
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • 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.