Using Java language to solve this quesiton thanks a lot for

Using Java language to solve this quesiton, thanks a lot for your help!

In the great nation of A the greatest, longest and most important of all books is book B. The importance of that book requires that it be stored

in its entirety in every library in the country. And so it is: due to its massive length, book B is stored in every library as a set of numbered (floppy) disks, and their numbering is identical across all libraries, such that disk n represents the exact same fragment of B across all libraries.

After the greatest hurricane that has ever hit the great nation of A, most or all its libraries ended up with a fair amount of devastation (you

see, the great nation of A was rather smallish in terms of its geographical expanse). Their stored copies of B have been impacted, so that in

many/most/all of the libraries some disks of B got washed away by the hurricane.

The libraries staffs managed to put together inventories of the libraries as they were left after the hurricane, including the disks still available

Of B. Now they are working to restore copies of B as best possible at each library by making copy of disks available in some libraries and

sending them to the libraries missing them.

Your goal is to help the librarians achieve the requirement of having as complete a copy of B at each library by writing a program to restore

B between libraries using as few disk copies as possible.

Input:

------                 

The first line of input will contain a value between 0 and 999999 inclusive, representing the number of libraries in A

.

Next there will be one line of input for each library. Each line will contain a value representing the number of floppies left at

that library, followed by a space and space-separated list of floppy ids currently present at that library. Floppy ids are each an integer

between 1 and 999999, inclusive. Each line will be at most 999 characters long.

Floppy disk ids are not necessarily consecutive. The list of floppy disks will not be in any particular order.

Output:

-------

The program must output an optimal floppy disk copy schedule to ensure that every library has a copy of every floppy disk. Output one line

for every copy instruction.

A copy instruction is of the form , where is the floppy disk id, is the index of the library

the disk will be copied from (1 = the first library), and is the index of the library to copy the disk to.

When there are no more copy instructions, the program must output the word \"done\" on a line by itself.

There is often more than one correct output with minimal number of operations for a given input, and any output that satisfies the

requirements is valid.

Constraints:

------------

The code you submit must take input from stdin and produce output to stdout as specified above. No other output is permitted. You can

assume the input will be valid. In the examples below, the text \"Input:\" and \"Output:\" (or \"One Possible Correct Output:\") are not

part of the output, and neither are the blank lines.

Example 1:

----------

Input:

4

3 1 3 4

3 1 2 3

2 1 3

3 1 4 2

One Possible Correct Output:

2 2 1

4 1 2

2 2 3

4 4 3

3 1 4

done

Example 2:

----------

Input:

2

2 1 2

2 2 1

Output:

done

Example 3:

----------

Input:

3

5 1 3 4 5 7

2 1 3

1 2

One Possible Correct Output:

2 3 2

2 3 1

1 1 3

4 1 2

5 1 3

5 3 2

4 2 3

3 1 3

7 1 2

7 1 3

done

Solution


import java.util.Scanner;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Surya
*/
public class floppy {
  
  
  
    public static void main(String argv[])
    {
       Scanner sc =new Scanner(System.in);//variable declarations
       int n,i,a[]=new int[1000000];
     
       n = sc.nextInt();
       String r = sc.nextLine();
       String s[] = new String[n];
     
       int sa[][]=new int[n][1000000],ss[]=new int[n],j;
     
     
    
       for(i=0;i<n;i++)//taking input
       {
           s[i]=sc.nextLine();
       }
     
       for(i=0;i<n;i++)
       {
           String l[] = s[i].split(\"\\\\s+\");
         
           for(j=0;j<l.length;j++)
           {
              // System.out.println(l[0]);
               if(j!=0)
               {
                        int m = Integer.parseInt(l[j]);
                       sa[i][m]=1;   
                       a[m] = 1;
                     
                
               }
               else
               {
                 
                   ss[i]=Integer.parseInt(l[0]);
               }
           }
     
       }
     
     
       int m=0;
     
       for(i=0;i<1000000;i++)
       {
           if(a[i]==1)m++;
       }
     
       System.out.println(\"One Possible Correct Output:\");
       int k;
       for(i=0;i<n;i++)//caculating and printing
       {
           while(ss[i]!=m)
           {
               for(j=0;j<1000000;j++)
               {
                   if(a[j]==1)
                   {
                       if(sa[i][j]!=1)
                       {
                           System.out.print(j+\" \");
                           for(k=0;k<n;k++)
                           {
                               if(sa[k][j]==1){
                               System.out.print(k+1+\" \"+(i+1));
                               break;  
                               }
                           }
                           System.out.println();
                           sa[i][j]=1;
                           ss[i]++;
                       }
                      
                   }
             
               }
   
         
         
           }
     
       }
     
       System.out.println(\"done\");
  
    }
  
}

ouput:-

run:
4
3 1 3 4
3 1 2 3
2 1 3
3 1 4 2
One Possible Correct Output:
2 2 1
4 1 2
2 1 3
4 1 3
3 1 4
done
BUILD SUCCESSFUL (total time: 5 seconds)

run:
2
2 1 2
2 2 1
One Possible Correct Output:
done
BUILD SUCCESSFUL (total time: 14 seconds)

Using Java language to solve this quesiton, thanks a lot for your help! In the great nation of A the greatest, longest and most important of all books is book B
Using Java language to solve this quesiton, thanks a lot for your help! In the great nation of A the greatest, longest and most important of all books is book B
Using Java language to solve this quesiton, thanks a lot for your help! In the great nation of A the greatest, longest and most important of all books is book B
Using Java language to solve this quesiton, thanks a lot for your help! In the great nation of A the greatest, longest and most important of all books is book B
Using Java language to solve this quesiton, thanks a lot for your help! In the great nation of A the greatest, longest and most important of all books is book B

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site