Friday 15 April 2016

Solution of URI 1478 :: Square Matrix II


Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.

/**Bismillahir Rahmanir Rahim.**/

#include <stdio.h>
int main()
{
    int a,b,c,d,e,f=1,g=2,h,i=0,j=0,N,I,J;
    while(1)
    {
        scanf("%d", &N);
        if(N==0) break;
        else
        {
            int ara[N][N];
            for(a=0;  a<N; a++)
            {
                for(b=0; b<N; b++)
                    ara[a][b]=1;
            }
            I=N-1; J=N;
            for(e=0,f=1,g=2;e<I; e++)
            {
                for(c=0,d=f; d<N;d++,c++)
                    ara[c][d]=g;
                f++; g++;
            }
            for(e=0,f=1,g=2;e<I; e++)
            {
                for(c=f,d=0; c<N;d++,c++)
                    ara[c][d]=g;
                f++; g++;
            }
            for(i=0; i<N; i++)
            {
                for(j=0; j<N; j++)
                {
                    if(j==0)
                        printf("%3d",ara[i][j]);
                else printf(" %3d",ara[i][j]);
                }
                printf("\n");
            }
            printf("\n");
        }
    }
    return 0;
}


4 comments:

  1. #include
    int main()
    {
    int n,row,col,i,j,x;
    while(1){
    scanf("%d",&n);
    if(n==0){
    break;
    }
    i=0,x=1;
    for(row=1;row<=n;row++){
    for(col=1;col<=n;col++){
    if(row-col==i)j=x;
    if(row-col>=x)j=row-col+1;
    if(col-row>=x)j=col-row+1;
    printf("%3d",j);
    if(col<n)printf(" ");
    else printf("\n");
    }
    }
    printf("\n");
    }
    }

    ReplyDelete
  2. i cant understand your logic.
    Please give me your contact Number.Please!

    ReplyDelete
  3. #include
    int main()
    {
    int i,x,j,k,l;
    for(i=0;ik)l=j-k+1;
    if(k>j)l=k-j+1;
    if(k==1)printf("%3d",l);
    else printf(" %3d",l);
    }

    printf("\n");
    }
    printf("\n");
    l=0;
    }
    return 0;
    }

    ReplyDelete

Note: only a member of this blog may post a comment.