Friday 15 April 2016

Solution of URI 1534 :: Array 123


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()
{
    /*freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);*/
    int z;
    while(scanf("%d",&z)!=EOF)
    {
        int ara[z][z];
        int a,b,c,d,e,f,g,h;
        for(a=0; a<z; a++)
        {
            for(b=0; b<z; b++)
                ara[a][b]=3;
        }
        for(a=0; a<z; a++)
            ara[a][a]=1;
        d=z-1;
        for(a=0,b=d; a<z;a++,b--)
            ara[a][b]=2;
        for(a=0; a<z; a++)
        {
            for(b=0; b<z; b++)
                printf("%d",ara[a][b]);
            printf("\n");
        }
    }
    return 0;
}


3 comments:

  1. Jesus christ men, I'd really like to know why you create so much variables, I only needed 2 (one of them being the array) to get the job done. Could you explain why you choose to do it this way? here's my code:

    #include
    #include

    using namespace std;

    int main(){
    int m[70][70]={},n;

    while (scanf("%d",&n)!=EOF){
    for (int i=0;i<n;i++){
    for (int j=0;j<n;j++){
    if (i==j) m[i][j]=1;
    else m[i][j]=3;
    if (i+j==n-1) m[i][j]=2;
    printf("%d",m[i][j]);
    if (j==n-1) cout << endl;
    }
    }
    }

    return 0;
    }

    ReplyDelete
  2. Easier and clear solution:

    https://pastebin.com/Tap9YKiS

    ReplyDelete
  3. #include

    int main() {

    int i,j,k,l,m,n;
    while(scanf("%d",&n)!=EOF){
    l=1,m=n;
    for(i=1;i<=n;i++){
    for(j=1;j<=n;j++){
    if(m==j){
    printf("2");
    }
    else if(l==j){
    printf("1");
    }
    else {
    printf("3");
    }
    }
    printf("\n");
    l++;m--;
    }
    }

    return 0;
    }

    ReplyDelete

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