Ordering of strings
PROBLEM :
You will be given N number of strings. You have to find the lexicographically smallest string and the lexicographically largest string among these strings.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. The first line of each test case consists of N. In the next line are N space separated strings of lower case Latin letters only.
Output:
Corresponding to each test case, in a new line, print the lexicographically smallest string and the lexicographically largest string among the given strings separated by a single space between them.
Constraints:
1 = T = 100
1 = N = 10
1 = Length of each string = 40
Example:
Input
3
3
a ab abc
3
a a b
3
z xy t
Output
a abc
a b
t z
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
void bubble(string [],int ) ;
int main()
{
int t,no,i ;
string str[100] ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>str[i] ;
bubble(str,no) ;
cout<<str[0]<<" "<<str[no-1] ;
cout<<endl ;
}
return 0;
}
void bubble(string str[],int no)
{
int i,j ;
string temp ;
for(i=0;i<no-1;i++)
{
for(j=0;j<no-i-1;j++)
{
if(str[j].compare(str[j+1])>0)
{
temp=str[j] ;
str[j]=str[j+1] ;
str[j+1]=temp ;
}
}
}
}
---------------------------------------------------------------------------------
You will be given N number of strings. You have to find the lexicographically smallest string and the lexicographically largest string among these strings.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. The first line of each test case consists of N. In the next line are N space separated strings of lower case Latin letters only.
Output:
Corresponding to each test case, in a new line, print the lexicographically smallest string and the lexicographically largest string among the given strings separated by a single space between them.
Constraints:
1 = T = 100
1 = N = 10
1 = Length of each string = 40
Example:
Input
3
3
a ab abc
3
a a b
3
z xy t
Output
a abc
a b
t z
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
void bubble(string [],int ) ;
int main()
{
int t,no,i ;
string str[100] ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>str[i] ;
bubble(str,no) ;
cout<<str[0]<<" "<<str[no-1] ;
cout<<endl ;
}
return 0;
}
void bubble(string str[],int no)
{
int i,j ;
string temp ;
for(i=0;i<no-1;i++)
{
for(j=0;j<no-i-1;j++)
{
if(str[j].compare(str[j+1])>0)
{
temp=str[j] ;
str[j]=str[j+1] ;
str[j+1]=temp ;
}
}
}
}
---------------------------------------------------------------------------------
Comments
Post a Comment