Dr. Strange's powers
PROBLEM :
Dr. Strange has developed some new powers since he has started programming. Now if a villan throws a 1 at him, he absorbs it and add it to his already existing binary string s. There are n villans and each of them throws a '1' at him. At each point you have to detemine the resulting string s.
Input:
First line of input contains n denoting the number of enemies. Each of the next n lines contain an integer l and a string s denoting the length of the existing binary string and the string itself.
Output:
For each of the n lines print a string denoting the resulting binary string at each steps.
Constraints:
1<=n<=20
1<=l<=100
Example:
Input:
2
4
1010
5
11100
Output
1011
11101
Explanation:
For test case 1 : 1010+1 = 1011 as he absorbs 1 and add to the existing string
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include <string>
string addOne(string ,int ) ;
int main()
{
int t,no ;
string str ;
cin>>t ;
while(t--)
{
cin>>no ;
cin>>str ;
str=addOne(str,no) ;
cout<<str<<endl ;
}
return 0;
}
string addOne(string str,int no)
{
int rem,i ;
rem=1 ;
string s ;
for(i=no-1;i>=0;i--)
{
if(rem!=0)
{
if(str[i]=='1')
str[i]='0' ;
else
{
str[i]='1' ;
rem=0 ;
break ;
}
}
}
if(rem==1)
{
s='1' ;
s=s+str ;
return s ;
}
return str ;
}
---------------------------------------------------------------------------------
Dr. Strange has developed some new powers since he has started programming. Now if a villan throws a 1 at him, he absorbs it and add it to his already existing binary string s. There are n villans and each of them throws a '1' at him. At each point you have to detemine the resulting string s.
Input:
First line of input contains n denoting the number of enemies. Each of the next n lines contain an integer l and a string s denoting the length of the existing binary string and the string itself.
Output:
For each of the n lines print a string denoting the resulting binary string at each steps.
Constraints:
1<=n<=20
1<=l<=100
Example:
Input:
2
4
1010
5
11100
Output
1011
11101
Explanation:
For test case 1 : 1010+1 = 1011 as he absorbs 1 and add to the existing string
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include <string>
string addOne(string ,int ) ;
int main()
{
int t,no ;
string str ;
cin>>t ;
while(t--)
{
cin>>no ;
cin>>str ;
str=addOne(str,no) ;
cout<<str<<endl ;
}
return 0;
}
string addOne(string str,int no)
{
int rem,i ;
rem=1 ;
string s ;
for(i=no-1;i>=0;i--)
{
if(rem!=0)
{
if(str[i]=='1')
str[i]='0' ;
else
{
str[i]='1' ;
rem=0 ;
break ;
}
}
}
if(rem==1)
{
s='1' ;
s=s+str ;
return s ;
}
return str ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment