Home > Back-end >  Online judge keeps telling i have wrong answer
Online judge keeps telling i have wrong answer

Time:11-01

This is the problem:

Lili is organizing a competition at her school. There are N × N people in a room-sized N × N and each one of them belongs to a team. The team is numbered between 1 to N. In the room, people wear a shirt with numbers between 0 to N indicating team number they come from. People that wears number 0 is spectator.

Lili wants to know whether each team consists of at least N members and are in the room. Help Lili to count how many incomplete teams in the room.

Input: Input consists of one integer N , number of team participating in this competition followed by N lines consisting of N integers Aij with value between 0 and N inclusive, each representing the numbers in the people’s shirts.

Constraints:

1 ≤ N ≤ 100

Output:

Output the number of incomplete teams in the room.

Sample input 1:

2
1 0
2 2

Output 1:

1

Sample input 2:

3
3 0 2
2 0 2
1 3 3

Output 2:

1

This is my current code but it keeps saying wrong answer

#include <stdio.h>

int main() {
  int n, array[101][101], i, j, count = 0, k, x = 0;

  scanf("%d", &n);

  for (i = 0; i < n; i  ) {
    for (j = 0; j < n; j  ) {
      scanf("%d", &array[i][j]);
    }
  }

  for (k = 0; k < n; k  ) {
    for (i = 0; i < n; i  ) {
      for (j = 0; j < n; j  ) {
        if (array[i][j] == k   1) {
          count  = 1;
        }
      }
    }

    if (count != n) {
      x  = 1;
    }
    count = 0;
  }

  printf("%d\n", x);
  return 0;
}

CodePudding user response:

First of all you do not need a 2D array of size NxN, but a single 1D array of size N (or just N-1) to count how many people are in each team.

And as you need a dynamic size array (N is only known at run time), you can simply use an allocated array of integers. Because I would bet a coin that you problem comes when N > 101...

Algo in pseudo-code:

read N
alloc an array teams of size N-1 and initialize it with 0
loop N * N times
| read a shirt number i
| if i > 0
| | increase teams[i - 1]
let incomplete be a 0 value integer
loop N-1 times
| if teams[i] < N
| | increase incomplete
output incomplete

CodePudding user response:

At first glance your solution seems correct excepting for two flaws:

  1. count != n expression should be count < n

  2. It might be too slow. @Serge Ballesta solution is O(N^2) but yours is O(N^3)

a 101x101 array is big enough, you could even use a 100x100 array instead. You can also allocate a NxN array using malloc, which would make your code more flexible but also a bit more complex.

  •  Tags:  
  • c
  • Related