Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ex: n=3
1 2 3
2 3 4
3 4 5
The answer is 1 because only number 3 appear in every row


What I have tried:

#include <iostream>

using namespace std;

void input(int **a,int n)
{
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
            cin>>a[i][j];
    }
}

void output(int **a,int n)
{
    int result=0;
    for(int i=1; i<n; i++)
    {
        bool appear=false;
        for(int j=0; j<n; j++)
        {
            for(int k=0; k<n; k++)
            {
                if(a[i][j]==a[0][k])
                {
                    appear=true;
                    break;
                }
            }
        }
        if(appear)
            result++;
    }
    cout<<result<<endl;
}

int main()
{
    int t, n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        int **a=new int*[n];
        for(int i=0; i<n; i++)
            a[i]=new int[n];
        input(a,n);
        output(a,n);
    }
    return 0;
}
I think my code is really bad because I have no idea
Posted
Updated 10-Mar-22 23:53pm

1 solution

You need to check it by row, and keep a count of each number that you find. So create a second array that is as long as the highest number input + 1 (to allow for zeroes). Then as you traverse each row, for each number that you find use it as an index into the array, and add 1 to the relevant cell. So for your matrix above you would end up with something like:
C++
0, 1, 2, 3, 2, 1 // i.e no zeroes, 1 x 1, 2 x 2, 3 x 3, 2 x 4, 1 x 5.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900