Jump to content

[C++] pbInfo


Andreigl
 Share

Recommended Posts

Link https://www.pbinfo.ro/?pagina=probleme&id=618

long long int CMMDC(long long int Min, long long int Max)
{
    long long int Rest;

    while (Max)
    {
        Rest = Min % Max;
        Min = Max;
        Max = Rest;
    }

    return Min;
}

int main()
{
    int N;
    long long int Numar, Min = 39e69, Max = 0, Impartitor;

    if(scanf("%d", &N));

    for(int i=0; i<N; i++)
    {
        if(scanf("%lli", &Numar));

        if(Numar > Max)
            Max = Numar;

        if (Numar < Min)
            Min = Numar;
    }

    do
    {
        Impartitor = CMMDC(Min, Max);
        Min /= Impartitor;
        Max /= Impartitor;
    } while (Impartitor != 1);

    printf("%lli/%lli", Min, Max);
}

 

Edited by Kelton
Link to comment
Share on other sites

long long int Oglinda(long long int Numar)
{
    long long int Oglindit = 0;

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

    return Oglindit;
}

bool Prime(long long int A, long long int B)
{
    while (B != 0)
    {
        int C = A % B;
        A = B;
        B = C;
    }
    
    if (A == 1)
        return true;
    else return false;
}

int main()
{
    int N, Perechi = 0;
    long long int Numar, Oglindit;

    scanf("%d", &N);

    for(int i=0; i<N; i++)
    {
        scanf("%lli", &Numar);
        Oglindit = Oglinda(Numar);
        if (Prime(Numar, Oglindit))
            Perechi++;
    }

    printf("%d", Perechi);
}

 

Link to comment
Share on other sites

Link problemă https://www.pbinfo.ro/?pagina=probleme&id=1192

void Conditia_1(int Array[], int X, ofstream &WriteToFile)
{
    for (int i=0; i<X; i++)
    {
        WriteToFile << Array[i] << " ";
    }
}

void Conditia_2(int Array[], int X, ofstream &WriteToFile)
{
    WriteToFile << "\n";

    for (int i=0; i<X; i++)
    {
        if (i == 0)
        {
            WriteToFile << "0" << " ";
            continue;
        }

        if (i == X)
        {
            WriteToFile << "0" << " ";
            break;
        }

        if ((float)(Array[i-1] + Array[i+1])/2 == Array[i])
        {
            WriteToFile << "1" << " ";
        }
        else
        {
            WriteToFile << "0" << " ";
        }
    }
}

void printArray(int Array[], int X, ofstream &WriteToFile, void (*calledFunc)(int [], int, ofstream &))
{
    return (*calledFunc)(Array, X, WriteToFile);
}

int main()
{
    ifstream ReadFromFile ("arhitectura2.in");
    ofstream WriteToFile ("arhitectura2.out");

    int X, Numar;
    ReadFromFile >> X;

    int Array[X];

    for (int i=0; i<X; i++)
    {
        ReadFromFile >> Numar;
        Array[i] = Numar;
    }

    sort (Array, Array+X, greater<int>());
    (*printArray)(Array, X, WriteToFile, Conditia_1);
    (*printArray)(Array, X, WriteToFile, Conditia_2);

    ReadFromFile.close();
    WriteToFile.close();
}
Edited by Kelton
Link to comment
Share on other sites

Link problemă https://www.pbinfo.ro/?pagina=probleme&id=2979

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

    long long int C, N, Ora, Min, Sec,
        BaietiInScoala = 0, FeteInScoala = 0, arrSize = 0,
        SecundeInTotal = 0, MinuteInTotal, OreInTotal,
        Array[10000], SecundeMax[10000];

    bool Trp = false, Trp_2 = false;

    char Sex, InOut;

    Read >> C; // Conditia
    Read >> N; // Persoanele ce au intrat/iesit

    for (int i=0; i<N; i++)
    {
        Read >> Sex >> InOut;
        Read >> Ora >> Min >> Sec;

        if (Sex == 'b')
        {
            if (InOut == 'i')
                BaietiInScoala++;

            if (InOut == 'e')
                BaietiInScoala--;
        }

        if (Sex == 'f')
        {
            if (InOut == 'i')
                FeteInScoala++;

            if (InOut == 'e')
                FeteInScoala--;
        }

        if (BaietiInScoala == FeteInScoala && BaietiInScoala != 0)
        {
           Array[0] = Sec;
           Array[1] = Min;
           Array[2] = Ora;

           Trp = true;
        }
        else {
            if (Trp == true)
            {
                int OreInTotal = Ora - Array[2];
                OreInTotal *= 3600;

                int Minute_Iesire_Transformare = Min * 60;
                int Minute_Intrare_Transformare = Array[1] * 60;
                MinuteInTotal = (Minute_Iesire_Transformare - Minute_Intrare_Transformare);

                SecundeInTotal += ((Sec - Array[0]) + OreInTotal + MinuteInTotal);
                Trp = false;
            }
        }

        if (BaietiInScoala % 2 != 0 && Sex == 'b')
        {
            //0,1,2
            //3,4,5
            Array[3] = Sec;
            Array[4] = Min;
            Array[5] = Ora;

            Trp_2 = true;
        } else
        {
            if (Trp_2 && Sex == 'b')
            {
                Trp_2 = false;

                int OreInTotal = Ora - Array[5];
                OreInTotal *= 3600;

                int Minute_Iesire_Transformare = Min * 60;
                int Minute_Intrare_Transformare = Array[4] * 60;
                MinuteInTotal = (Minute_Iesire_Transformare - Minute_Intrare_Transformare);

                SecundeMax[arrSize] = ((Sec - Array[3]) + MinuteInTotal + OreInTotal);

                arrSize++;
            }
        }
    }

    sort (SecundeMax, SecundeMax+arrSize, greater <int>());

    if (C == 1)
        Write << BaietiInScoala << " " << FeteInScoala;

    if (C == 2)
        Write << SecundeInTotal;

    if (C == 3)
        Write << SecundeMax[0];

    Read.close();
    Write.close();
}
Edited by Kelton
Link to comment
Share on other sites

Link problemă https://www.pbinfo.ro/?pagina=probleme&id=2745

int NrDivizori(long long int Numar)
{
    int K = 0;

    while (Numar % 2 == 0)
    {
        K++;
        Numar /= 2;
    }

    int Count = (K + 1);

    for(int i=3; (i*i)<=Numar; i+=2)
    {
        int K = 0;

        while(Numar % i == 0)
        {
            K++;
            Numar /= i;
        }

        Count *= (K + 1);
    }

    if (Numar > 1)
        Count *= 2;

    return Count;
}

int main()
{
    int n,t,p;
    long long int array[10], Minim = 39e+64, Maxim = 0, Suma = 0;

    scanf("%d %d %d", &n, &t, &p);

    if (t == 1)
    {
        for(int i=0; i<n; i++)
        {
            scanf("%lli", &array[i]);
            Suma += array[i];

            if (array[i] < Minim and i < p)
                Minim = array[i];
        }

        int SumCif = 0;
        while(Minim != 0)
        {
            SumCif += (Minim % 10);
            Minim /= 10;
        }

        printf("%d\n%lli", SumCif, Suma);
    }
    else
    {
        for(int i=0; i<n; i++)
        {
            scanf("%lli", &array[i]);
            Suma += array[i];

            if(array[i] > Maxim and i < p)
                Maxim = array[i];
        }


        printf("%d\n%lli", NrDivizori(Maxim), Suma);
    }

    return 0;
}
Edited by Kelton
Link to comment
Share on other sites

Link problemă https://www.pbinfo.ro/?pagina=probleme&id=245

int NrDivizori(int Numar)
{
    int K = 0;

    while (Numar % 2 == 0)
    {
        K++;
        Numar /= 2;
    }

    int Count = (K + 1);

    for(int i=3; (i*i)<=Numar; i+=2)
    {
        K = 0;

        while (Numar % i == 0)
        {
            K++;
            Numar /= i;
        }

        Count *= (K + 1);
    }

    if (Numar > 1)
        Count *= 2;

    return Count;
}

int main()
{
    int a, NrMax = 0, Numar = 0;
    scanf("%d", &a);

    for(int i=1; i<=a; i++)
    {
        int ret = NrDivizori(i);
        if(ret > NrMax)
        {
            Numar = 0;
            NrMax = ret;

            if (Numar < i)
                Numar = i;
        }
    }

    printf("%d", Numar);

    return 0;
}
Edited by Kelton
Link to comment
Share on other sites

Link problemă https://www.pbinfo.ro/?pagina=probleme&id=408

long long int Oglindit(long long int Numar)
{
    long long int Oglinda = 0;
    int LastCif;
    
    while (Numar != 0)
    {
        LastCif = Numar % 10;
        Numar /= 10;
        Oglinda = Oglinda * 10 + LastCif;
    }
    
    return Oglinda;
}

int NrDivizori(long long int Oglindit)
{
    int K = 0;
    
    while (Oglindit % 2 == 0)
    {
        K++;
        Oglindit /= 2;
    }
    
    int Count = (K + 1);
    
    for (int i=3; (i*i)<=Oglindit; i+=2)
    {
        K = 0;
        
        while (Oglindit % i == 0)
        {
            K++;
            Oglindit /= i;
        }
        
        Count *= (K + 1);
    }
    
    if (Oglindit > 1)
        Count *= 2;
    
    return Count;
}

int main()
{
    long long int Numar;
    scanf("%lli", &Numar);
    
    long long int ret = Oglindit(Numar);
    printf("%d", NrDivizori(ret));
}
Edited by Kelton
Link to comment
Share on other sites

Link problemă https://www.pbinfo.ro/?pagina=probleme&id=1574

int main()
{
    int a,b;
    long long int suma_1, suma_2;
    suma_1 = suma_2 = 0;
    scanf("%d %d", &a, &b);

    for(int i=1; (i*i)<=a; i++)
    {
        if (a % i == 0)
        {
            suma_1 += i;

            if ((i*i) < a)
                suma_1 += (a/i);
        }
    }

    if (b != (suma_1-a))
    {
        printf("NU SUNT PRIETENE");
        return 0;
    }

    for(int i=1; (i*i)<=b; i++)
    {
        if (b % i == 0)
        {
            suma_2 += i;

            if ((i*i) < b)
                suma_2 += (b/i);
        }
    }

    if (a != (suma_2-b))
    {
        printf("NU SUNT PRIETENE");
        return 0;
    }

    printf("PRIETENE");

    return 0;
}
Edited by Kelton
Link to comment
Share on other sites

Link https://www.pbinfo.ro/?pagina=probleme&id=3017

void binoct(long long int Numar, int Divide)
{
    int array[1005], size = 0;

    while (Numar != 0)
    {
        array[size++] = Numar % Divide;
        Numar /= Divide;
    }

    for (int i=(size-1); i>=0; i--)
        printf("%d", array[i]);
}

void hex(long long int Numar)
{
    char caracters[100];
    int size = 0, Rest;

    while (Numar != 0)
    {
        Rest = Numar % 16;
        Numar /= 16;

        if (Rest <= 9)
            caracters[size++] = (char)(Rest + 48);
        else caracters[size++] = (char)(Rest + 55);
    }

    for (int i=(size-1); i>=0; i--)
        printf("%c", caracters[i]);
}

int main()
{
    char C;
    scanf("%s", &C);

    long long int N;
    scanf("%lli", &N);

    if (C == 'b')
        binoct(N, 2);
    else if (C == 'o')  
        binoct(N, 8);  
    else
        hex(N);
	
    return 0;
}

 

Edited by Kelton
Link to comment
Share on other sites

Link problemă https://www.pbinfo.ro/?pagina=probleme&id=618

int main()
{
    int N;
    scanf("%d", &N);

    int array[N], vector[N];
    for (int i=0; i<N; i++)
    {
        scanf("%d", &array[i]);
        vector[i] = array[i];
    }

    std::sort(array, array+N);
    for (int i=0; i<N; i++)
    {
        for(int j=0; j<N; j++)
            if (array[i] == vector[j])
                printf("%d ", j+1);
    }

    return 0;
}
Edited by Kelton
Link to comment
Share on other sites

Link https://www.pbinfo.ro/?pagina=probleme&id=509

void swap(long long int *x, long long int *y)
{
    long long int temp = *x;
    *x = *y;
    *y = temp;
}

void insertionSort(long long int array[], int size)
{
    for (int i=0; i<size; i++)
    {
        int aux = i;

        while (aux > 0 and array[aux-1] > array[aux])
        {
            swap (&array[aux-1], &array[aux]);
            aux--;
        }
    }
}

int main()
{
    int size;
    long long int array[1005];
    scanf ("%d", &size);

    for (int i=0; i<size; i++)
        scanf("%lli", &array[i]);

    insertionSort(array, size);

    for (int i=0; i<size; i++)
        printf("%d ", array[i]);
}

 

Edited by Kelton
Link to comment
Share on other sites

 

Link https://www.pbinfo.ro/?pagina=probleme&id=945

int main()
{
    long long int Numar, Baza_1, Baza_2, Ultima_Cifra,
        Format = 0, P = 1, aux = 0, K = 0;

    int Vector[100];

    std::cin >> Numar >> Baza_1 >> Baza_2;

    // Trebuie sa convertim numarul din baza B in baza 10
    while (Numar != 0)
    {
        Ultima_Cifra = Numar % 10;
        Numar /= 10;
        Format = Format + Ultima_Cifra * P;
        P *= Baza_1;
    }

    // Convertim din baza 10 in Baza_2
    while (Format != 0)
    {
        Ultima_Cifra = Format % Baza_2;
        Format /= Baza_2;
        Vector[K++] = Ultima_Cifra;
    }

    for (int i=K-1; i>=0; i--)
        std::cout << Vector[i];

    return 0;
}

 

Edited by Kelton
Link to comment
Share on other sites

 

Link https://www.pbinfo.ro/?pagina=probleme&id=2318

int main()
{
    std::ifstream ReadFromFile("conversie_b_10.in");
    std::ofstream WriteToFile("conversie_b_10.out");
    
    long long int Numar, Baza, Rest,
        P = 1, Format = 0;

    ReadFromFile >> Numar >> Baza;

    while (Numar != 0)
    {
        Rest = Numar % 10;
        Numar /= 10;
        Format = Format + Rest * P;
        P *= Baza;
    }

    WriteToFile << Format;
    
    ReadFromFile.close();
    WriteToFile.close();
}

 

Edited by Kelton
Link to comment
Share on other sites

 

Link https://www.pbinfo.ro/?pagina=probleme&id=163

bool isPrim(long long int Numar)
{
    if (Numar == 2)
        return true;

    if (Numar < 2 or Numar % 2 == 0)
        return false;

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

    return true;
}

int main()
{
    int N;
    std::cin >> N;

    long long int Array[N];

    for (int i=0; i<N; i++)
    {
        std::cin >> Array[i];

        if (isPrim(Array[i]))
            Array[i] = -1;

        if (Array[i] != -1)
            std::cout << Array[i] << " ";
    }
}

 

Edited by Kelton
Link to comment
Share on other sites

Link https://www.pbinfo.ro/?pagina=probleme&id=2375

 

bool isPalindrom(long long int Numar)
{
    if (Numar < 10)
        return true;

    long long int Oglinda = 0, aux;
    aux = Numar;

    while (Numar != 0)
    {
        Oglinda = Oglinda * 10 + Numar % 10;
        Numar /= 10;
    }

   	if (aux == Oglinda)
        return true;
    else return false;
}

int main()
{
    std::ifstream Read("palindromsd.in");
    std::ofstream Write("palindromsd.out");

    long long int Numar, Array[1005], Oglindit;
    int size = 0, N = 0;

    bool Palindrom = false;

    while (Read >> Numar)
    {
        if (isPalindrom(Numar))
        {
            Array[size++] = Numar;
            Palindrom = true;
        }
    }

    if (Palindrom == false)
        Write << -1;

    if ((size+1) % 2 == 0)
        for (int i=0; i<=size/2; i++)
            if (i != size/2)
                Write << Array[i] << " " << Array[size-1-i] << " ";
            else Write << Array[i];
    else
        for (int i=0; i<size/2; i++)
            Write << Array[i] << " " << Array[size-1-i] << " ";


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

    return 0;
}

 

Edited by Kelton
Link to comment
Share on other sites

 

Link https://www.pbinfo.ro/?pagina=probleme&id=1550

long long int Factorial(int Numar)
{
    int Multiplier = 1;

    for (int i=Numar; i>1; i--)
        Multiplier *= i;

    return Multiplier;
}

int main()
{
    int N;
    long long int Numar;

    std::cin >> N;
    int Fact = Factorial(N);

    for (int i=0; i<N; i++)
    {
        std::cin >> Numar;

        if (Fact % Numar == 0)
            std::cout << Numar << " ";
    }

    return 0;
}

 

Edited by Kelton
Link to comment
Share on other sites

 

Link https://www.pbinfo.ro/?pagina=probleme&id=249

int main()
{
    std::ifstream Read("pozitiex.in");
    std::ofstream Write("pozitiex.out");
    
    long long int Numar, arr[10005];
    int N, pos = 0;
    
    Read >> Numar >> N;
    
    for (int i=0; i<N; i++)
    {
        Read >> arr[i];
    }
    
    std::sort(arr, arr+N);
    
    for (int i=0; i<N; i++)
    {
        if (arr[i] == Numar)
        {
            pos = i+1;
            break;
        }
    }
    
    if (pos !=0 )
        Write << pos;
    else Write << "NU EXISTA";
    
    Read.close();
    Write.close();
    
    return 0;
}

 

Edited by Kelton
Link to comment
Share on other sites

 

Link https://www.pbinfo.ro/?pagina=probleme&id=238

int main()
{
    std::ifstream Read ("zone.in");
    std::ofstream Write ("zone.out");

    int N, Numar[305], size = 0, ElementPar, ElementImpar, aux;
    Read >> N;
    
    ElementPar = ElementImpar = 0;

    while (Read >> Numar[size++])
        Read >> Numar[size++];

    for (int i=0; i<N; i++)
    {
        if (Numar[i] % 2 == 0)
        {
            ElementPar = i;
            break;
        }
    }

    for (int i=(N*3)-1; i>(N*2)-1; i--)
    {
        if (Numar[i] % 2 != 0)
        {
            ElementImpar = i;
            break;
        }
    }

    
    if (ElementPar != 0 and ElementImpar != 0)
    {
        aux = Numar[ElementPar];
        Numar[ElementPar] = Numar[ElementImpar];
        Numar[ElementImpar] = aux;
    }

    for (int i=0; i<3*N; i++)
        Write << Numar[i] << " ";

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

    return 0;
}

 

Edited by Kelton
Link to comment
Share on other sites

 

Link https://www.pbinfo.ro/?pagina=probleme&id=248

int N;
long long int Numar[10005], PrimulElement;

int main()
{
    std::ifstream Read ("pozitie.in");
    std::ofstream Write ("pozitie.out");

    Read >> N;

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

    PrimulElement = Numar[0];
    std::sort(Numar, Numar+N);

    int i;
    for (i=0; i<N; i++)
    {
        if (Numar[i] == PrimulElement)
            break;
    }

    Write << i+1;

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

    return 0;
}

 

Edited by Kelton
Link to comment
Share on other sites

 

Link https://www.pbinfo.ro/?pagina=probleme&id=237

int main()
{
    std::ifstream Read ("sume.in");
    std::ofstream Write ("sume.out");

    int N, Vector[105], Suma = 0;

    Read >> N;

    for (int i=0; i<N; i++)
    {
        Read >> Vector[i];
        Suma += Vector[i];
    }

    Write << Suma << std::endl;

    for (int i=N-1; i>=1; i--)
    {
        Suma -= Vector[i];
        Write << Suma << std::endl;
    }

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

    return 0;
}

 

Edited by Kelton
Link to comment
Share on other sites

 

Link https://www.pbinfo.ro/?pagina=probleme&id=132

bool PatratPerfect(int Numar)
{
    if ((int)sqrt(Numar) == (float)sqrt(Numar))
        return true;
    else return false;
}

int main()
{
    std::ifstream Read ("elempp.in");
    std::ofstream Write ("elempp.out");

    int N;
    long long int Numar, aux;

    Read >> N;

    for (int i=0; i<N; i++)
    {
        Read >> Numar;
        aux = Numar;
        
        while (Numar != 0)
        {
            if (Numar > 9 and Numar < 100)
                if (PatratPerfect(Numar))
                    Write << aux << " ";

            Numar /= 10;
        }
    }

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

 

Edited by Kelton
Link to comment
Share on other sites

 

Link https://www.pbinfo.ro/?pagina=probleme&id=100

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

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

    return true;
}

int main()
{
    std::ifstream Read ("nrapprime.in");
    std::ofstream Write ("nrapprime.out");

    int N, NrPrime = 0;
    long long int Vector[105];

    Read >> N;

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

        if (Prime(Vector[i]))
            NrPrime++;
    }

    Write << NrPrime;

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

 

Edited by Kelton
Link to comment
Share on other sites

 

Link https://www.pbinfo.ro/?pagina=probleme&id=99

int main()
{
    std::ifstream Read ("nraparitii.in");
    std::ofstream Write ("nraparitii.out");

    int N, Vector[105], Aparitie = 0;

    Read >> N;

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

    for (int i=0; i<N; i++)
    {
        if (Vector[i] == Vector[N-1])
            Aparitie++;
    }

    Write << Aparitie;

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

 

Edited by Kelton
Link to comment
Share on other sites

Link https://www.pbinfo.ro/?pagina=probleme&id=98

int main()
{
    std::ifstream Read ("maxim4.in");
    std::ofstream Write ("maxim4.out");

    int N, Vector[105], Max = 0, Min = 39e+69, Aparitie = 0;

    Read >> N;

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

        if (Vector[i] > Max)
        {
            Max = Vector[i];
            Aparitie = 0;
        }

        if (Vector[i] == Max)
            Aparitie++;
    }

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

    Read.close();
    Write.close();
}
Edited by Kelton
Link to comment
Share on other sites

Link https://www.pbinfo.ro/?pagina=probleme&id=82

int main()
{
    std::ifstream Read ("minmax.in");
    std::ofstream Write ("minmax.out");
    
    int N, Vector[1005], Max = 0, Min = 39e+69;

    Read >> N;

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

        if (Vector[i] > Max)
            Max = Vector[i];

        if (Vector[i] < Min)
            Min = Vector[i];
    }

    Write << Min << " " << Max;
    
    Read.close();
    Write.close();
}
Edited by Kelton
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.