Friday 15 April 2016

Solution of URI 1557 :: Square Matrix III


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 dg(int x)
{
    int a=x,b=0;
    while(a)
    {
        a/=10;
        b++;
    }
    return b;
}
int main()
{
    int i=0,j=0,N,a,b,c=1,x,y=0,I,d,e=0,f,g,h;
    while(1)
    {
        scanf("%d", &N);
        if(N==0) break;
        else
        {
            int ara[N][N];
            I=N-1;
            for(x=0,c=1,y=0; x<N; x++)
            {
                for(a=y,b=0; a>=0; a--,b++)
                    ara[a][b]=c;
                y++; c*=2;
            }
            for(x=1,y=1; x<N; x++)
            {
                for(a=I,b=y; b<=I; a--,b++)
                    ara[a][b]=c;
                y++; c*=2;
            }
            d=dg(ara[I][I]);
            for(i=0; i<N; i++)
            {
                for(j=0; j<N; j++)
                {
                    if(j==0)
                    {
                        h=ara[i][j];
                        e=dg(h);
                        g=d-e;
                        for(f=1;f<=g; f++)
                            printf(" ");
                        printf("%d",ara[i][j]);
                    }
                    else
                    {
                        h=ara[i][j];
                        e=dg(h);
                        g=d-e;
                        for(f=1;f<=g; f++)
                            printf(" ");
                        printf(" %d",ara[i][j]);
                    }
                }
                printf("\n");
            }
            printf("\n");
        }
    }
    return 0;
}


5 comments:

  1. #include

    int main()
    {
    int n, x[15][15], i, j, t, p=1, q=1, b=0, a;
    while(1){
    scanf("%d", &n);
    a = (2*n) -1;
    for(i=1; i0){
    for(t=1; t<=i+j; t++){
    p=p*2;
    }
    if(j==0){
    printf("%*d", b, p);
    }

    else{
    printf(" %*d", b, p);
    }
    p =1;

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

    //submitting that code i get a presentation error...
    what is the problem..I couldn't understand. plz help me...

    ReplyDelete
  2. #include
    int main()
    {
    while(1)
    {
    int T, i, j, m = 1, n = 1, s = 1;
    scanf("%d", &T);
    //int ara[T][T];

    if(T == 0)
    {
    return;
    }

    for(i = 1; i <= T; i++)
    {

    for(j = 1; j <= T; j++)
    {


    if(T == 1 || T == 2){
    printf("%d ", m);
    }

    else{
    printf(" %d", m);
    }

    m = m * 2;

    }
    m = n * 2;
    n = n * 2;
    printf("\n");
    }
    printf("\n");
    }
    }


    //showing presentation error. please help me to solve this...

    ReplyDelete
  3. #include
    #include
    int main ()
    {
    int contadorLinha,contadorColuna,potencia=0,numero,matriz;
    scanf ("%d", &matriz);

    for (contadorColuna = 0; contadorColuna<matriz;contadorColuna++)
    {
    potencia = contadorColuna;
    for (contadorLinha=0; contadorLinha<matriz; contadorLinha++)
    {
    numero = pow (2, potencia);
    printf ("%d ", numero);
    potencia++;
    }
    printf ("\n");
    }

    return 0;
    }

    ReplyDelete
  4. //showing presentation error. please help me to solve this...


    #include
    int main()
    {
    int i,j,k,l=1,m,n=1;
    for(i=1;i<=5;i++)
    {
    m=1;
    for(j=1;j<=l;j++)
    {
    for(k=1;k<=l;k++)
    {
    printf("%d ",m);
    if(k==2)
    {
    n=m;
    }
    m=m*2;
    }
    m=n;
    printf("\n");
    }
    ++l;
    printf("\n\n");
    }
    }

    ReplyDelete
  5. What is the problem here to submit get presentation error


    #include
    using namespace std;
    int main()
    {
    int x;
    while(cin>>x)
    {
    if(x==0)
    {
    break;
    }
    for(int i=1,n=1;i<=x;i++,n*=2)
    {
    for(int j=1,m=n;j<=x;j++,m*=2)
    {
    cout<<m<<" ";
    }
    cout<<endl;
    }
    cout<<endl;
    }


    }

    ReplyDelete

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