Tuesday, July 11, 2017

Uva problem solution 541 - Error Correction

problem link :

Discuss: in this problem you just calculate the sum of row if it not even the increase flag.after it you calculate column as one disorder found twice you also found an odd in column summation also.then increase value of cnt. if flag and cnt one then change the bit.if  both zero then it's ok otherwise corrupt.


try yourself before see the code



#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,j,cnt,flag;
    int ara[101][101];
    while(cin >>n&&n!=0)
    {
        cnt=0;flag=0;
        int row,column,rr,cc;
        for(i=0;i<n;i++)
        {
            for(j=0 ;j < n; j++)
            {
                cin >>ara[i][j];
            }
        }
        for(i=0;i<n;i++)
        {
            row=0;
            for(j=0;j<n;j++)
            {
                row+=ara[i][j];
            }
            if(row%2!=0)
            {
                rr=i+1;
                cnt++;
            }

        }
         for(i=0;i<n;i++)
        {
            column=0;
            for(j=0;j<n;j++)
            {
               column+=ara[j][i];
            }
            if(column%2!=0)
            {
                cc=i+1;
                flag++;
            }

        }
        if(cnt==0&&flag==0)
            cout <<"OK"<<endl;
        else if(cnt==1&&flag==1)
            cout <<"Change bit ("<<rr<<','<<cc<<')'<<endl;
        else
            cout <<"Corrupt"<<endl;
    }
    return 0;
}

No comments:

Post a Comment