#include<bits/stdc++.h>
using namespace std;
void multiply(string);
int main()
{
int i,j,k;
string str;
cin>>str;
while(str!=""){
if(str=="1"){
cout<<"1"<<endl;
cin>>str;
continue;
}
for(i=str.length()-1;i>=0;i--){
if(str[i]=='0'){
str[i]='9';
continue;
}
if(str[i]!='0'){
str[i]=((str[i]-'0')-1)+'0';
break;
}
}
multiply(str);
cin>>str;
}
return 0;
}
void multiply(string str)
{
int i,temp,carry=0;
string ans="";
for(i=str.length()-1;i>=0;i--){
temp=(str[i]-'0')*2+carry;
ans.push_back(temp%10+'0');
carry=temp/10;
}
if(carry!=0)
ans.push_back(carry+'0');
reverse(ans.begin(),ans.end());
cout<<ans<<endl;
}
Can anyone help me in finding out why my code is showing tle.Is there any way better than this?
https://www.spoj.com/problems/BISHOPS/