quarta-feira, 24 de outubro de 2018

URI PROBLEMA 1514 - Competição SOLUÇÃO EM C

URI Online Judge | 1514

Competição

Por Cristhian Bonilha, UTFPR BR Brazil
Timelimit: 1
A maioria dos programadores que chegam a escrever competições com exercícios de programação concordam em quatro características que toda competição deve alcançar. Embora nem todas sejam sempre alcançadas, quanto mais melhor. As características são as seguintes:
  1. Ninguém resolveu todos os problemas.
  2. Todo problema foi resolvido por pelo menos uma pessoa (não necessariamente a mesma).
  3. Não há nenhum problema resolvido por todos.
  4. Todos resolveram ao menos um problema (não necessariamente o mesmo).
Rafael organizou uma competição alguns dias atrás, e está preocupado com quantas dessas características ele conseguiu alcançar com a competição.
Dadas as informações sobre a competição, com o número de participantes, número de problemas, e qual participante resolveu quais problemas, descubra o número de características que foram alcançadas nesta competição.

Entrada

Haverá diversos casos de teste. Cada caso de teste inicia com dois inteiros N e M (3 ≤ NM ≤ 100), indicando, respectivamente, o número de participantes e o número de problemas.
Em seguida, haverá N linhas com M inteiros cada, onde o inteiro da linha i e coluna j é 1 caso o competidor iresolveu o problema j, ou 0 caso contrário.
O último caso de teste é indicado quando N = M = 0, o qual não deverá ser processado.

Saída

Para cada caso de teste, imprima uma linha contendo um inteiro, representando quantas das características citadas foram alcançadas na competição.


URI Online Judge | 1514

Contest

By Cristhian Bonilha, UTFPR BR Brazil
Timelimit: 1
Most of the programmers who come to write contests with programming exercises agree in four caracteristics that every contest should achieve. Although not all of them are always achieved, more is better. The caracterists are the following:
  1. Nobody solved all the problems.
  2. Every problem was solved by at least one person (not necessarily the same).
  3. There is no problem solved by everyone.
  4. Everyone solved at least one problem (not necessarily the same).
Rafael organized a contest a few days ago, and is worried about how many of these caracteristics he got to achieve with his contest.
Given the information about the contest, with the number of contestants, the number of problems, and which contestant solved which problem, find out the number of caracteristics that were achieved on this contest.

Input

There will be several tests cases. Each test case starts with two integers N and M (3 ≤ NM ≤ 100).
Then, there will be N lines with M integers each, where the integer on the i-th line and on the j-th column is 1 if the i-th contestant solved the j-th problem, or 0 otherwise.
The last test case is indicated when N = M = 0, which should not be processed.

Output

For each test case, print one line with one integer, representing the amount of caracteristics achieved by the given contest.



#include <stdio.h>

#define NUMERO_MAXIMO_DE_PESSSOAS 128
#define NUMERO_MAXIMO_DE_PROBLEMAS 128

#define INDICE_DA_SOMA 101

int ninguemResolveuTodosOsProblemas(int problemas[][NUMERO_MAXIMO_DE_PROBLEMAS], int numeroDeParticipantes, int numeroDeProblemas);
int todoProblemaFoiResolvidoPorPeloMenosUmaPessoa(int problemas[][NUMERO_MAXIMO_DE_PROBLEMAS], int numeroDeParticipantes, int numeroDeProblemas);
int nenhumProblemaFoiResolvidoPorTodos(int problemas[][NUMERO_MAXIMO_DE_PROBLEMAS], int numeroDeParticipantes, int numeroDeProblemas);
int todosResolveramAoMenosUmProblema(int problemas[][NUMERO_MAXIMO_DE_PROBLEMAS], int numeroDeParticipantes, int numeroDeProblemas);

int main (void) {
    int numeroDeParticipantes,
        i,
        numeroDeProblemas,
        j,
        problemas[NUMERO_MAXIMO_DE_PESSSOAS][NUMERO_MAXIMO_DE_PROBLEMAS],
        soma;

    while (scanf("%d %d", &numeroDeParticipantes, &numeroDeProblemas), numeroDeParticipantes != 0 || numeroDeProblemas != 0) {
        for (i = 0; i < numeroDeParticipantes; i++) {
            problemas[i][INDICE_DA_SOMA] = 0;
        }

        for (j = 0; j < numeroDeProblemas; j++) {
            problemas[INDICE_DA_SOMA][j] = 0;
        }

        for (i = 0; i < numeroDeParticipantes; i++) {
            for (j = 0; j < numeroDeProblemas; j++) {
                scanf("%d", &problemas[i][j]);

                problemas[i][INDICE_DA_SOMA] += problemas[i][j];
                problemas[INDICE_DA_SOMA][j] += problemas[i][j];
            }
        }

        soma = ninguemResolveuTodosOsProblemas(problemas, numeroDeParticipantes, numeroDeProblemas)
            + todoProblemaFoiResolvidoPorPeloMenosUmaPessoa(problemas, numeroDeParticipantes, numeroDeProblemas)
            + nenhumProblemaFoiResolvidoPorTodos(problemas, numeroDeParticipantes, numeroDeProblemas)
            + todosResolveramAoMenosUmProblema(problemas, numeroDeParticipantes, numeroDeProblemas);
        printf("%d\n", soma);
    }

    return 0;
}

int ninguemResolveuTodosOsProblemas(int problemas[][NUMERO_MAXIMO_DE_PROBLEMAS], int numeroDeParticipantes, int numeroDeProblemas) {
    int i,
        j,
        resolveuTodosOsProblemas;

    for (i = 0; i < numeroDeParticipantes; i++) {
        resolveuTodosOsProblemas = 1;

        for (j = 0; resolveuTodosOsProblemas && j < numeroDeProblemas; j++) {
            resolveuTodosOsProblemas = problemas[i][j] == 1;
        }

        if (resolveuTodosOsProblemas) {
            return 0;
        }
    }

    return 1;
}

int todoProblemaFoiResolvidoPorPeloMenosUmaPessoa(int problemas[][NUMERO_MAXIMO_DE_PROBLEMAS], int numeroDeParticipantes, int numeroDeProblemas) {
    int j,
        todoProblemaResolvidoPorPeloMenosUmaPessoa = 1;

    for (j = 0; todoProblemaResolvidoPorPeloMenosUmaPessoa && j < numeroDeProblemas; j++) {
        todoProblemaResolvidoPorPeloMenosUmaPessoa = problemas[INDICE_DA_SOMA][j] > 0;
    }

    if (todoProblemaResolvidoPorPeloMenosUmaPessoa) {
        return 1;
    }
    else {
        return 0;
    }
}

int nenhumProblemaFoiResolvidoPorTodos(int problemas[][NUMERO_MAXIMO_DE_PROBLEMAS], int numeroDeParticipantes, int numeroDeProblemas) {
    int j;

    for (j = 0; j < numeroDeProblemas; j++) {
        if (problemas[INDICE_DA_SOMA][j] == numeroDeParticipantes) {
            return 0;
        }
    }

    return 1;
}

int todosResolveramAoMenosUmProblema(int problemas[][NUMERO_MAXIMO_DE_PROBLEMAS], int numeroDeParticipantes, int numeroDeProblemas) {
    int i;

    for (i = 0; i < numeroDeParticipantes; i++) {
        if (problemas[i][INDICE_DA_SOMA] == 0) {
            return 0;
        }
    }

    return 1;
}

Nenhum comentário:

Postar um comentário

URI PROBLEMA 1133 - Resto da Divisão SOLUÇÃO EM C

URI Online Judge | 1133 Resto da Divisão Adaptado por Neilor Tonin, URI   Brasil Timelimit: 1 Escreva um programa que leia 2 valo...