vendredi 1 octobre 2021

Given a series of positions, he is able to draw the map of the entire garden [closed]

Mr. Jack very recently started gardening. He has a very specific way of planting new flowers. He denotes the position of his flowers with a single number. The number tells him the number of the flower.

Given a series of positions, he is able to draw the map of the entire garden.

For example, the positions 1 3 4 0 2 means his garden is like this:

      • *

* * * *

  • * * *

    • * *

Where the * represents grass and # is a flower.

Write a program takes as input an integer array. The integer array has the positions and you have to print the garden. The number of rows in the garden is the same as the length of the input array.

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int test= scan.nextInt();
        while(test!=0)
        {
            int m = scan.nextInt();
            int i,j,k;
            int[] arr=new int[m]; 
        for(i=0;i<m;i++)
        {
            arr[i]=scan.nextInt();
        }
            String[][] d = new String[m][m];
            for(i=0;i<m;i++)
            {
                for(j=0;j<m;j++)
                {
                    if(i==0)
                        d[i][j]="#";
                    else
                        d[i][j]="*";
                }
                //System.out.println();
            }
             
            for(i=0,j=0;i<m;i++,j++)
            {
                String temp="";
               // for(j=0;j<m;j++)
                {
                    int count=0;
                    while(count<1)
                    {
                        if(arr[i]==0)
                           break;
                    temp=d[arr[i]][j];
                   d[arr[i]][j]="#";
                    d[0][j]="*";
                   count+=1;
                    }
                }
                temp="";
            }
            for(i=0;i<m;i++)
            {
                for(j=0;j<m;j++)
                {
                    System.out.print(d[i][j]);
                    if(j<m-1)
                        System.out.print(" ");
                }
                System.out.println();
            }
    }
    }
}

Aucun commentaire:

Enregistrer un commentaire