Jump to content

[C++] pbInfo


Andreigl
 Share

Recommended Posts

În acest topic o să postez diferite probleme de pe site-ul pbinfo, probleme făcute de mine. Problemele nu sunt explicate, ci doar am pus sursa.

Link direct: https://www.pbinfo.ro/

 

----------------------------------------------------------------------------

 

Pentru a accesa această problema, apasă aici.

 

Rezolvarea:

	int N, M;
	int array[100][100];
	bool loop = true, found = false;
	std::cin >> N >> M;

	for (int c = 0; c < N; c++)
		for (int l = 0; l < M; l++)
			std::cin >> array[c][l];

	for (int l = 0; l < M; l++)
	{
		for (int c = 0; c < N; c++)
		{
			if (array[0][l] != array[c][l])
			{
				loop = false;
				break;
			}
		}

		if (loop)
		{
			printf("%d ", array[0][l]);
			found = true;
		}
		loop = true;
	}

	if (found == false)
		printf("nu exista");

 

Edited by shanker'
update
Link to comment
Share on other sites

Pentru a accesa această problemă, apasă aici.

 

 

struct index
{
	int C, L;
} index;

int main()
{
	int N, M, max = 0;
	int array[100][100];
	std::cin >> N >> M;

	for (int c = 0; c < N; c++)
		for (int l = 0; l < M; l++)
			std::cin >> array[c][l];

	for (int i = 0; i < N * M; i++)
	{
		for (int c = 0; c < N; c++)
			for (int l = 0; l < M; l++)
				if (array[c][l] > max)
				{
					max = array[c][l];
					index.C = c;
					index.L = l;
				}

		array[index.C][index.L] = -1;

		for (int j = 0; j < N; j++)
			for (int k = 0; k < M; k++)
				if (array[j][k] == max)
				{
					printf("%d", array[j][k]);
					return 0;
				}

		max = -1;
	}

	printf("IMPOSIBIL");
}

 

Link to comment
Share on other sites

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

 

Varianta mea:

#include <iostream>
#include <algorithm>

int main()
{
	int N, M, i = 0, lastElement = -1;
	int array[100][100], elemente[10000];
	std::cin >> N >> M;

	for (int c = 0; c < N; c++)
		for (int l = 0; l < M; l++)
		{
			std::cin >> array[c][l];
			if (c == 0 || l == 0 || c == N - 1 || l == M - 1)
				elemente[i++] = array[c][l];
		}

	std::sort(elemente, elemente + i);
	for (int j = 0; j < i; j++)
	{
		if (elemente[j] != lastElement)
		{
			lastElement = elemente[j];
			printf("%d ", elemente[j]);
		}
	}
}

 

Varianta autorului:

int n,m,a[101][101] , v[500] , k;

int main()
{
	cin >> n >> m;
	for(int i = 1 ; i <= n ; ++i)
		for(int j = 1 ; j <= m ; ++j)
			cin >> a[i][j];
	
	k = 0;
	
	for(int i = 1 ; i <= n ; i ++ )
		for(int j =1 ; j <=m ; j ++)
			if(i == 1 || i == n || j == 1 || j == m)
			{
				bool gasit = false;
				for(int p = 1 ; p <= k && ! gasit ; p ++)
					if(a[i][j] == v[p])
						gasit = true;
				if(! gasit)
				{
					int p = k + 1;
					for(p = k + 1 ; p > 1 && v[p-1] > a[i][j] ; p --)
						v[p] = v[p-1];
					v[p] = a[i][j];
					k ++;
				}
			}
	for(int i = 1; i <= k ; i ++)
		cout << v[i] << " ";
	return 0;
}

 

Edited by shanker'
Link to comment
Share on other sites

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

 

Varianta mea:

int main()
{
	int N, M;
	int array[100][100];
	std::cin >> N >> M;

	for (int c = 0; c < N; c++)
		for (int l = 0; l < M; l++)
			std::cin >> array[c][l];

	int X = array[N - 1][M - 1];

	for (int c = 0; c < N; c++)
		for (int l = 0; l < M; l++)
			if (c == 0 || l == 0 || c == N - 1 || l == M - 1)
				array[c][l] = X;

	for (int c = 0; c < N; c++)
	{
		for (int l = 0; l < M; l++)
			printf("%d ", array[c][l]);
		printf("\n");
	}
}

 

Varianta autorului:

int main()
{
    int n , m ,a[51][51];
    cin >> n >> m;
    for(int i = 1 ; i <= n ; i ++)
        for(int j = 1 ; j <= m ; j ++)
            cin >> a[i][j];
    
    int x = a[n][m];
    for(int i = 1 ; i <= n ; i ++)
        for(int j = 1 ; j <= m ; j ++)
            if(i == 1 || i == n || j == 1 || j == m)
                a[i][j] = x;
    for(int i = 1 ; i <= n ; i ++)
    {
        for(int j = 1 ; j <= m ; j ++)
            cout << a[i][j] << " ";
        cout << "\n";
    }
    return 0;
}

 

Link to comment
Share on other sites

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

 

Varianta mea:

int main()
{
	int N, M, X;
	int array[100][100];
	bool Result = false;
	std::cin >> N >> M;

	for (int c = 0; c < N; c++)
		for (int l = 0; l < M; l++)
			std::cin >> array[c][l];

	std::cin >> X;

	/* ----------------------- (de sus) */
	for (int l = 0; l < M; l++)
		if (array[0][l] == X)
		{
			Result = true;
			break;
		}

	/*
		(din stanga)
		|
		|
		|
		|
		|
		|
	*/
	if (Result != true)
		for(int c=0; c<N; c++)
			if (array[c][0] == X)
			{
				Result = true;
				break;
			}

	/* ----------------------- (de jos) */
	if (Result != true)
		for (int c = 0; c < N - 1; c++)
			if (array[N - 1][c] == X)
			{
				Result = true;
				break;
			}

	/*
		(din dreapta)
		|
		|
		|
		|
		|
		|
	*/
	if(Result != true)
		for (int c = 0; c < N - 1; c++)
			if (array[c][M - 1] == X)
			{
				Result = true;
				break;
			}

	if (Result)
		printf("DA");
	else printf("NU");
}

 

Varianta autorului:

int main()
{
    int n , m , x , A[101][101];
    cin >> n >> m;
    for(int i = 1; i <= n ; i ++)
        for(int j = 1; j <= m ; j ++)
            cin >> A[i][j];
    cin >> x;
    
    int gasit = 0;
    for(int i = 1; i <= n ; i ++)
        for(int j = 1; j <= m ; j ++)
            if(i == 1 || i == n || j == 1 || j == m)
                if(A[i][j] == x)
                    gasit = 1;
    
    if(gasit)
        cout << "DA\n";
    else
        cout << "NU\n";
    return 0;
}

 

Link to comment
Share on other sites

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

 

Varianta mea:

int main()
{
	int N, M, result = 0;
	int array[100][100];
	bool pas = true;
	std::cin >> N >> M;

	for (int c = 0; c < N; c++)
		for (int l = 0; l < M; l++)
			std::cin >> array[c][l];

	for (int c = 0; c < N; c++)
	{
		for (int l = 0; l < M; l++)
		{
			if (array[c][l] != array[c][0])
			{
				pas = false;
				break;
			}
		}

		if (pas == true)
			result++;

		pas = true;	
	}

	printf("%d", result);
}

 

Varianta autorului:

int n,m,a[101][101];

int main()
{
	cin >> n >> m;
	for(int i = 1 ; i <= n ; ++i)
		for(int j = 1 ; j <= m ; ++j)
			cin >> a[i][j];
	
	int cnt = 0;
	for(int i = 1 ; i <= n ; i ++)
	{
		bool ok = true;
		for(int j = 1 ; j <= m ; ++j)
			if(a[i][j] != a[i][1])
				ok = false;
		if(ok)
			cnt ++;
	}
	
	cout << cnt;
	
	return 0;
}

 

Edited by shanker'
Link to comment
Share on other sites

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

 

int main()
{
	int N, M, min1, min2, coloana1, coloana2;
	int array[100][100];
	std::cin >> N >> M;

	min1 = min2 = 2147483647;
	for (int c = 0; c < N; c++)
		for (int l = 0; l < M; l++)
			std::cin >> array[c][l];

	for (int c = 0; c < N; c++)
	{
		if (array[c][0] < min1)
		{
			min1 = array[c][0];
			coloana1 = c;
		}

		if (array[c][M - 1] < min2)
		{
			min2 = array[c][M - 1];
			coloana2 = c;
		}
	}

	array[coloana1][0] = min2;
	array[coloana2][M - 1] = min1;
	
	for (int c = 0; c < N; c++)
	{
		for (int l = 0; l < M; l++)
			printf("%d ", array[c][l]);
		printf("\n");
	}
}

 

Link to comment
Share on other sites

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

 

int main()
{
	int N, M, max = 0, size = 0, lastAp = -1;
	int array[100][100], index[100][100], numere[100];
	std::cin >> N >> M;

	for (int c = 0; c < N; c++)
		for (int l = 0; l < M; l++)
		{
			std::cin >> array[c][l];
			index[c][l] = 0;
		}

	for (int j = 0; j < N; j++)
		for (int k = 0; k < M; k++)
			for (int c = 0; c < N; c++)
				for (int l = 0; l < M; l++)
					if (array[j][k] == array[c][l])
						index[c][l]++;

	for (int c = 0; c < N; c++)
		for (int l = 0; l < M; l++)
			if (index[c][l] > max)
				max = index[c][l];

	for (int c = 0; c < N; c++)
		for (int l = 0; l < M; l++)
			if (index[c][l] == max)
			{
				numere[size] = array[c][l];
				size++;
			}
	
	std::sort(numere, numere + size);
	for (int i = 0; i < size; i++)
		if (numere[i] != lastAp)
		{
			lastAp = numere[i];
			printf("%d ", numere[i]);
		}
	
	
}

 

Link to comment
Share on other sites

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

 

int main()
{
	int N, M, max = 0, max_number = 0;
	int array[100][100], index[100][100];
	std::cin >> N >> M;

	for (int c = 0; c < N; c++)
		for (int l = 0; l < M; l++)
		{
			std::cin >> array[c][l];
			index[c][l] = 0;

			for (int j = 0; j < N; j++)
				for (int k = 0; k < M; k++)
					if (array[c][l] == array[j][k])
					{
						index[c][l]++;
						if (index[c][l] > max)
							max = index[c][l];
					}
		}

	for (int c = 0; c < N; c++)
		for (int l = 0; l < M; l++)
			if (index[c][l] == max)
				if (array[c][l] > max_number)
					max_number = array[c][l];

	printf("%d", max_number);
}

 

Link to comment
Share on other sites

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

 

int main()
{
	int N, M, array[100][100], suma[100];
	long long int lowest_line = 2147483647;

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

	for (int c = 0; c < N; c++)
		for (int l = 0; l < M; l++)
			scanf("%d", &array[c][l]);

	for (int c = 0; c < N; c++)
		suma[c] = 0;

	for (int c = 0; c < N; c++)
		for (int l = 0; l < M; l++)
			suma[c] += array[c][l];

	for (int i = 0; i < N; i++)
	{
		int index = 0;

		for (int c = 0; c < N; c++)
			if (suma[c] != -1 and suma[c] < lowest_line)
			{
				lowest_line = suma[c];
				index = c;
			}
		suma[index] = -1;
		lowest_line = 2147483647;
		
		for (int l = 0; l < M; l++)
			printf("%d ", array[index][l]);

		printf("\n");
	}
}

 

Link to comment
Share on other sites

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

 

int main()
{
	int N, M, array[100][100], copy[100][1];
	scanf("%d %d", &N, &M);

	for (int c = 0; c < N; c++)
		for (int l = 0; l < M; l++)
			scanf("%d", &array[c][l]);

	for (int c = 0; c < N; c++)
		copy[c][0] = array[c][0];

	for (int c = 0; c < N; c++)
		for (int l = 0; l < M; l++)
			array[c][l] = array[c][l + 1];

	for (int c = 0; c < N; c++)
		array[c][M - 1] = copy[c][0];

	for (int c = 0; c < N; c++)
	{
		for (int l = 0; l < M; l++)
			printf("%d ", array[c][l]);
		printf("\n");
	}
}

 

 

Link to comment
Share on other sites

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

 

int main()
{
	std::ifstream readFile("smartphone.in");
	std::ofstream writeFile("smartphone.out");

	int Conditie, Telefoane, contor;
	long long int Performanta[10000];

	readFile >> Conditie >> Telefoane;
	for (contor = 0; contor < Telefoane; contor++)
		readFile >> Performanta[contor];

	std::sort(Performanta, Performanta + contor, std::greater<int>());
	if (Conditie == 1)
		writeFile << Performanta[0];
	else writeFile << Performanta[1];
}

 

Edited by shanker'
Link to comment
Share on other sites

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

 

#include <fstream>
#include <algorithm>

int main()
{
	std::ifstream fileIn("minimdoua.in");
	std::ofstream fileOut("minimdoua.out");

	int N, Numar[250], read, Contor = 0;
	fileIn >> N;

	for (int i = 0; i < N; i++)
	{
		fileIn >> read;
		if (read > 9)
		{		
			int Cif_1 = read % 10;
			int Cif_2 = (read / 10) % 10;
				
			if (Cif_1 == Cif_2)
			{
				Numar[Contor] = read;
				Contor++;
			}
		}
	}

	std::sort(Numar, Numar + Contor);
	if (Contor > 1)
		fileOut << Numar[0] << " " << Numar[1];
	else fileOut << "numere insuficiente";
}

 

Link to comment
Share on other sites

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

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

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

    return true;
}


int main()
{
    int N, M, Numere = 0, Contor = 0, Line = 1;
    if(scanf("%d %d", &N, &M));

    long long int Numar, array[M];

    for(int i=1; i<=N; i++)
    {
        for(int j=1; j<=M; j++)
        {
            if(scanf("%lld", &Numar))
            {
                if(i % 2 == 0)
                    if(Prim(Numar))
                        Numere++;
            }
        }
    }

    printf("%d", Numere);
}

 

Link: https://www.pbinfo.ro/probleme/666/nrprime

Edited by shanker'
Link to comment
Share on other sites

int main()
{
    int N, M, Contor = 0;
    if(scanf("%d %d", &N, &M));

    long long int Numar, Suma = 0, array[M], Max = 0;

    for(int i=0; i<(N*M); i++)
    {
        if(scanf("%lld", &Numar))
        {
            Contor++;
            array[Contor] = Numar;

            if(Contor == M)
            {
                for(int j=1; j<=M; j++)
                    if(array[j] > Max)
                        Max = array[j];

                for(int j=1; j<=M; j++)
                    Suma += array[j];

                Suma -= Max;
                printf("%lld ", Suma);

                Contor = 0;
                Suma = 0;
                Max = 0;
            }
        }
    }
}

Link: https://www.pbinfo.ro/probleme/659/sumalinii1

Edited by shanker'
Link to comment
Share on other sites

int main()
{
    std::ifstream ReadFromFile("unice1.in");
    std::ofstream WriteToFile("unice1.out");
    
    int N, array[1005], Unice = 0, K = 0;
    ReadFromFile >> N;
    
    for(int i=0; i<N; i++)
        ReadFromFile >> array[i];
        
    for(int i=0; i<N; i++)
    {
        K = 0;

        for(int j=0; j<N; j++)
        {
            if( array[i] == array[j])
                K++;
        }

        if(K == 1)
            Unice++;
    }

    WriteToFile << Unice;
    
    return 0;
}

 

Link: https://www.pbinfo.ro/probleme/270/unice1

Edited by shanker'
Link to comment
Share on other sites

int main()
{
    long long int N;
    long long int _aux = 0, P = 1;
    int Baza, Rest;

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

    while(N)
    {
        Rest = N % Baza;
        N /= Baza;
        _aux = _aux + Rest * P;
        P *= 10;
    }

    int cif_max = 0;
    while(_aux)
    {
        if(_aux % 10 > cif_max)
            cif_max = _aux % 10;

        _aux /= 10;
    }

    printf("%d", cif_max);

    return 0;
}

Link: https://www.pbinfo.ro/probleme/426/bazab

Edited by shanker'
Link to comment
Share on other sites

long long int base_convertor(long long int Numar, int Baza)
{
    long long int base = 0, P = 1;
    int Rest;

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

    return base;
}

int main()
{
    int Baza,Cifre,_Cifra;
    if(scanf("%d %d", &Baza, &Cifre));

    long long int Numar = 0;
    for(int contor=0; contor<Cifre; contor++)
    {
        if(scanf("%d", &_Cifra));

        Numar = Numar * 10 + _Cifra;
    }

    printf("%lld", base_convertor(Numar, Baza));
}

Link: https://www.pbinfo.ro/probleme/428/transfb

Edited by shanker'
Link to comment
Share on other sites

bool Prime(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, Aparitie = 0;
    long long int Numar, max = 0;

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

    for(int i=0; i<N; i++)
    {
        if(scanf("%lld", &Numar))
        {
            if(Prime(Numar) and Numar > max)
            {
                max = Numar;
                Aparitie = 0;
            }

            if(Numar == max)
                Aparitie++;
        }
    }

    printf("%lld %d", max, Aparitie);

    return 0;
}

 

Link: https://www.pbinfo.ro/probleme/436/primmaxim

Edited by shanker'
Link to comment
Share on other sites

// #include <climits>

bool Prime(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, _Numere_Prime = 0;
    long long int Numar, min=LLONG_MAX, max=0;

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

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

            	if(Numar > max)
                	max = Numar;

            	if (min > Numar)
                	min = Numar;
            }
        }
    }

    printf("%d %lld %lld", _Numere_Prime, min, max);

    return 0;
}

Link: https://www.pbinfo.ro/probleme/1408/numere10

Edited by shanker'
Link to comment
Share on other sites

bool Prime(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 A, B, Perechi = 0;
    if(scanf("%d %d", &A, &B));

    if(A > B)
    {
        for(int i=B; i<=A; i++)
            if(Prime(i))
                Perechi++;
    }
    else
        for(int i=A; i<=B; i++)
            if(Prime(i))
                Perechi++;

    printf("%d", Perechi);

    return 0;
}

Link: https://www.pbinfo.ro/probleme/44/primeinterval

Edited by shanker'
Link to comment
Share on other sites

bool Prime(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()
{
    long long int Numar;
    if(scanf("%lld", &Numar));

    for(;;)
    {
        Numar++;
        if(Prime(Numar))
            break;
    }

    printf("%lld", Numar);

    return 0;
}

Link: https://www.pbinfo.ro/probleme/375/urmatorul-prim

Edited by shanker'
Link to comment
Share on other sites

long long int CMMDC(long long int A, long long int B)
{
    long long int Rest;

    while (B != 0)
    {
        Rest = A % B;
        A = B;
        B = Rest;
    }

    return A;
}

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

    long long int _Numar[1005];
    for(int i=0; i<N; i++)
        if(scanf("%lld", &_Numar[i]));

    long long int Impartitor;
    if (N >= 2)
    {
        Impartitor = CMMDC(_Numar[0], _Numar[1]);
        for(int i=2; i<N; i++)
            Impartitor = CMMDC(Impartitor, _Numar[i]);
    }

    printf("%lld", Impartitor);
}

Link: https://www.pbinfo.ro/probleme/305/cmmdcn

Edited by shanker'
Link to comment
Share on other sites

int CMMDC(int A, int B)
{
    int Rest;

    while (B != 0)
    {
        Rest = A % B;
        A = B;
        B = Rest;
    }

    return A;
}

int main()
{
    int _N1, _N2, _N3, _N4;
    int _aux_1, _aux_2, _aux_3, _aux_4;
    if(scanf("%d %d %d %d", &_N1, &_N2, &_N3, &_N4));

    _aux_1 = _N1;
    _aux_2 = _N2;
    _aux_3 = _N3;
    _aux_4 = _N4;

    if (_N2 != _N4)
    {
        _N1 *= _N4;
        _N3 *= _N2;
        _N2 *= _N4;
    }

    _N1 += _N3;

    int Impartitor;
    do
    {
        Impartitor = CMMDC(_N1, _N2);
        _N1 /= Impartitor;
        _N2 /= Impartitor;
    } while(Impartitor != 1);

    printf("%d %d", _N1, _N2);

    _aux_1 *= _aux_3;
    _aux_2 *= _aux_4;
    do
    {
        Impartitor = CMMDC(_aux_1, _aux_2);
        _aux_1 /= Impartitor;
        _aux_2 /= Impartitor;
    } while(Impartitor != 1);

    printf("\n%d %d", _aux_1, _aux_2);
}

Link: https://www.pbinfo.ro/probleme/390/spfractii

Edited by shanker'
Link to comment
Share on other sites

long long int CMMDC(long long int A, long long int B)
{
    long long int Rest;

    while (B != 0)
    {
        Rest = A % B;
        A = B;
        B = Rest;
    }

    return A;
}

int main()
{
    long long int Numar, aux;
    int Cifre = 0;

    if(scanf("%lld", &Numar))
        aux = Numar;

    while(Numar != 0)
    {
        Cifre++;
        Numar /= 10;

    }

    int _number_1[15], _number_2[2];
    long long int _format_1, _format_2;
    _format_1 = _format_2 = 0;

    for(int i=0; i<(Cifre/2); i++)
    {
        _number_1[i] = aux % 10;
        aux /= 10;
    }

    for(int i=(Cifre/2)-1; i>=0; i--)
        _format_1 = _format_1 * 10 + _number_1[i];

    if((Cifre % 2) != 0)
    {
        aux /= 10;
        printf("%lli", CMMDC(_format_1, aux));
    }
    else printf("%lli", CMMDC(_format_1, aux));

    return 0;
}

Link: https://www.pbinfo.ro/probleme/410/cmmdc2

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.