I am unable to understand why I am getting WA.
here's my C++ solution
include
include
include
include
using namespace std;
int main(){
string N;
while(cin>>N){
int len = N.length();
if (len == 1 && N[0] == '1'){
printf("1\n");
continue;
}
string res = "";
int carry = 0;
for (int i = len - 1; i >= 0; i--){
int mult = (((N[i] - '0') * 2) + carry);
carry = mult/10;
res.push_back((mult%10) + '0');
}
if(carry)
res.push_back (carry + '0');
reverse (res.begin(), res.end());
int l = res.length();
carry = 0;
string fres;
int sub = ((res[l - 1] - '0') - 2);
if (sub < 0){
sub = sub + 10;
carry = 1;
fres.push_back (sub + '0');
for (int i = l - 2; i >= 0; i--){
sub = ((res[i] - '0') - carry);
if(sub < 0){
sub = sub + 10;
carry = 1;
}
else
carry = 0;
fres.push_back(sub + '0');
}
}
else{
fres.push_back (sub + '0');
for(int i = l-2; i >= 0; i--)
fres.push_back(res[i]);
}
reverse (fres.begin(), fres.end());
cout << fres<<endl;
N = "";
}
return 0;
}