Problem:-
Dhananjay has recently learned about ASCII values.He is very fond of experimenting. With his knowledge of ASCII values and character he has developed a special word and named it Dhananjay's Magical word.
A word which consist of alphabets whose ASCII values is a prime number is an Dhananjay's Magical word. An alphabet is Dhananjay's Magical alphabet if its ASCII value is prime.
Dhananjay's nature is to boast about the things he know or have learnt about. So just to defame his friends he gives few string to his friends and ask them to convert it to Dhananjay's Magical word. None of his friends would like to get insulted. Help them to convert the given strings to Dhananjay's Magical Word.
Rules for converting:
1.Each character should be replaced by the nearest Dhananjay's Magical alphabet.
2.If the character is equidistant with 2 Magical alphabets. The one with lower ASCII value will be considered as its replacement.
Input format:
First line of input contains an integer
Output Format:
For each test case, print Dhananjay's Magical Word in a new line.
Constraints:
1 <= T <= 100
1 <= |S| <= 500
A word which consist of alphabets whose ASCII values is a prime number is an Dhananjay's Magical word. An alphabet is Dhananjay's Magical alphabet if its ASCII value is prime.
Dhananjay's nature is to boast about the things he know or have learnt about. So just to defame his friends he gives few string to his friends and ask them to convert it to Dhananjay's Magical word. None of his friends would like to get insulted. Help them to convert the given strings to Dhananjay's Magical Word.
Rules for converting:
1.Each character should be replaced by the nearest Dhananjay's Magical alphabet.
2.If the character is equidistant with 2 Magical alphabets. The one with lower ASCII value will be considered as its replacement.
Input format:
First line of input contains an integer
T
number of test cases. Each test case contains an integer N
(denoting the length of the string) and a string S
.Output Format:
For each test case, print Dhananjay's Magical Word in a new line.
Constraints:
1 <= T <= 100
1 <= |S| <= 500
Explanation
ASCII values of alphabets in AFREEN are 65, 70, 82, 69 ,69 and 78 respectively which are converted to CGSCCO with ASCII values 67, 71, 83, 67, 67, 79 respectively. All such ASCII values are prime numbers.
Time Limit:0.5 sec(s) for each input file.
Memory Limit:256 MB
Source Limit:1024 KB
Code:-
Solution by using ( C language):-
#include<stdio.h>
int main()
{
int t,n,i,x,j,k=0,l,k1=0,r,r1=0;
char s[500];
scanf("%d",&t);
for(i=1;i<=t;i++)
{
scanf("%d",&n);
scanf("%s",s);
for(j=0;j<=n-1;j++)
{
x=s[j];
while(1)
{
r=x+k;
if(r>122)
break;
for(l=2;l<=r-1;l++)
{
if(r%l==0)
break;
}
if(l==r)
break;
else
k++;
}
while(1)
{
r1=x-k1;
for(l=2;l<=r1-1;l++)
{
if(r1%l==0)
break;
}
if(l==r1)
break;
else
k1++;
}
if(r>122)
s[j]=r1;
else
{
if(k>=k1)
s[j]=r1;
if(k<k1)
s[j]=r;
}
if(x<65)
s[j]=67;
k=0;
k1=0;
}
l=0;
while(l<=n-1)
{
printf("%c",s[l]);
l++;
}
printf("\n");
}
return 0;
}
Recommended Post:-
- Hackerearth Problems:-Data structure:-
- Very Cool numbers | Hacker earth solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
1 Comments
Nice
ReplyDelete