how do I approach chewbacca and Number problem
Chewbacca and Number
hello @s.kumar
read a number as string (say s) .
if left most character is β9β
- then leave the leftmost character as it is and for other characters simpl replace s[i] with min(s[i],β9β-s[i])
else:
- for each i replace s[i] with min(s[i],β9β-s[i])
print s
#include<bits/stdc++.h>
using namespace std;
int main() {
string n;
cin>>n;
int start=0;
if(n[0]=='9')
start=1;
for(int i=start;i<n.length();i++){
char c='9'-n[i]+'0';
n[i]=min(n[i],c );
}
cout<<n;
return 0;
}
why is β0β added in the following statement,
char c=β9β-n[i]+β0β;
to convert int to char we add β0β to the number.
here we did that for the same purpose
but both β9β and s[i] are char only, isnβt it?
β¦
no, that will be int
β9β is char, right?
4 is int,
then why β9β-(4+β0β) is giving correct o/p
it will give 5 which is int and not char.
u again need to to add β0β to convert 5 in to β5β
bro,
n[i]=min(n[i],c); //here n[i] on the right side holds the int value, and c holds char, is it possible to compare char and int?
β¦
n[i] is char and c is also char.
why is n[i]=min(n[i], β9β-n[i]+β0β) statement not working
for(int i=start;i<n.length();i++){
//char c='9'-n[i]+'0';
n[i]=min(n[i],'9'-n[i]+'0' );
}
compiler is treating this -> β9β-n[i]+β0β as an int .
use char(β9β-n[i]+β0β)
or write ur own min function
I hope Iβve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.