Coding | Mcqs | Multiple choice questions | Informative | Computer Science | Engineering | Aptitude | Quants | Verbal

INFEED

CTS & Wipro Automata Test 14 - 19.09.2021

 Count the Ships

Count the Ships: A sea is represented as an N*N matrix where # represents a part of a ship and – represents water. All the ships are surrounded by water. Series of # which are connected together forms a ship. The # can be connected to another # in any of the surrounding 8 cells to form a ship. The program must print the number of ships in the given map.


Boundary Condition(s):

1 <= N <= 100


Input Format:

The first line contains N.

The next N lines contain N characters each.


Output Format:

The first line contains the count of the ship.


Example Input/Output 1:

Input:

6

------

-###--

-###--

------

-####-

-####-

Output:

2


Example Input/Output 2:

Input:

8

--#-----

--#-----

--#-----

-----#--

------#-

--#----#

#####---

--#-----

Output:

3


C CODE

#include<stdio.h>
#include<stdlib.h>
void ship(int num,char arr[num][num+1],int c1,int c2){
    arr[c1][c2]='-';
    for(int ele=c1-1;ele<=c1+1;ele++){
        for(int foo=c2-1;foo<=c2+1;foo++){
            if(ele<0&&ele>=num&&foo>=num&&foo<0)
            continue;
            if(arr[ele][foo]=='#')
            ship(num,arr,ele,foo);
        }
    }
}
int main(){
    int num,ctr=0;
    scanf("%d",&num);
    char arr[num][num+1];
    for(int ele=0;ele<num;ele++)
    scanf("%s",arr[ele]);
    for(int ele=0;ele<num;ele++){
        for(int foo=0;foo<num;foo++){
            if(arr[ele][foo]=='#'){
                ctr++;
                ship(num,arr,ele,foo);
            }
        }
    }
printf("%d",ctr);
}

Maximum by Single Digit Replacement

Maximum by Single Digit Replacement: Two integers M and N are passed as the input to the program. The program must print the maximum value of M obtained by replacing exactly one digit in M by a digit from N.

Boundary Condition(s):
1 <= Number of digits in M <= 100
1 <= Number of digits in N <= 10

Input Format:
The first line contains M and N separated by space(s).

Output Format:
The first line contains the maximum value of M.

Example Input/Output 1:
Input:
56120 21

Output:
56220

Explanation:
The maximum value is obtained by replacing 1 in 56120 by 2.
Any other replacements would give smaller values.

Example Input/Output 2:
Input:
895496223 5

Output:
895596223

Java Code:



import java.util.*;
public class Hello {
    public static void main(String[] args)
    {
Scanner sc=new Scanner(System.in);
String f=sc.next();
String s=sc.next();
char ff[]=f.toCharArray();
char ss[]=s.toCharArray();
int maxi=0;
for(int i=0;i<ss.length;i++)
{
    if(Integer.parseInt(String.valueOf(ss[i]))>maxi)
    {
        maxi=Integer.parseInt(String.valueOf(ss[i]));
    }
}
int fl=0;
for(int i=0;i<ff.length;i++)
{
    if(maxi>Integer.parseInt(String.valueOf(ff[i])))
    {
        ff[i]=(char)(maxi+'0');
        break;
    }
}
System.out.println(String.valueOf(ff));
}
}

Post a Comment

Previous Post Next Post