It is showing wrong answer, help me solve this, please.
Question link -DIVISIBLEBY8 Problem - CodeChef
include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t–){
int n,a,b,c;
string m,o,p=“”,j=“”,s;
cin>>n>>m;
s=m;
if(n<=3){
c=stoi(m);
a=c % 8;
b=c-a;
if(b%10==0) b+=8;
cout<<b<<endl;
}
else{
o=s.substr(n-3,3);
c=stoi(o);
if(o[0]==‘0’ && o[1]==‘0’ && o[2]==‘0’) j=“000”;
else if(o[0]==‘0’ && o[1]==‘0’) j=“00”;
else if(o[0]==‘0’) j=“0”;
a=c % 8;
b=c-a;
if(b%10==0) b+=8;
p=to_string(b);
p=j+p;
s[n-3]=p[0];
s[n-2]=p[1];
s[n-1]=p[2];
cout<<s<<endl;
}
}
return 0;
}
@souvik_2004
Plzz refer the following solution for better understanding of the logic and implementation .
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
int n,v;
string s;
cin>>n>>s;
if(n<=3) v=stoi(s);
else v=stoi(s.substr(n-3));
if(v%8==0) cout<<s<<endl;
else{
int ans;
if(n==1) ans=0;
else if(n==2) ans=stoi(s.substr(n-2,1));
else ans=stoi(s.substr(n-3,2));
if(ans%4==0) s.back()='8';
else if(ans%4==1) s.back()='6';
else if(ans%4==2) s.back()='4';
else s.back()='2';
cout<<s<<endl;
}
}
return 0;
}
I asked what was wrong with my logic. You copied and pasted a random answer.
@souvik_2004
I have sent u the solution i came up with …To get more help plzz explain your logic and also send the code in formatted form … so that it can be easily understandable and readable.