### Motaz_Husain's blog

By Motaz_Husain, 2 months ago,

I Had A Problem solving the "lucky?" problem. And I Need just a little bit of help to solve it.

--> this is the link to the problem : https://codeforces.com/contest/1676/problem/A

--> and this is my source code :

# include

using namespace std;

int main(){ string num; int testCases,sum1,sum2;

cin>>testCases;

for(int k=0;k<=testCases-1;k++){

cin>>num;

for(int i=0;i<=2;i++){
sum1=sum1+num[i];
}//end for i - first 3-digits -

for(int j=3;j<=5;j++){
sum2=sum2+num[j];
}//end for j - Second 3-digits -

if(sum1==sum2){
cout<<"YES\n";
}//end if - sum1==sum2 -
else {
cout<<"NO\n";
}//end else

}//end for k-test cases-

return 0;

}//end main

So If Anybody can help I Would be super appreciative.

• +4

 » 2 months ago, # |   +9 Initialise sum1=sum2=0
 » 2 months ago, # |   0 As the constraints are very limited you can just compare the sum for first 3 to the last three as s[0]+ .. +s[2] to s[3]+ .. + s[5], and have your answer. :)
•  » » 2 months ago, # ^ |   0 Just want to point out: Of course you are right, also with your approach you don't need to initialise sum1 and sum2 and probably I would've done the same in a contest. But I really like, that Motaz_Husain used a for loop! This is very good form in my opinion and especially useful for someone who is starting into CP.
 » 2 months ago, # | ← Rev. 2 →   +12 To help you helping yourself: find out about the debugging tools of your programming environment or use some temporary debug outputs. By inspecting sum1 and sum2 you would've found, that there are invalid values stored in them. Edit: Also please use the formating options CF provides.
•  » » 2 months ago, # ^ |   0 can you tell me what is CF exactly ?
•  » » » 2 months ago, # ^ |   0 Codeforces. The platform you are posting on.
•  » » » » 2 months ago, # ^ |   0 alright, Thanks man I appreciate your advice.
 » 2 months ago, # | ← Rev. 2 →   -8 Keep it simple. string s; cin >> s; cout << (s[0]+s[1]+s[2]==s[3]+s[4]+s[5] ? "YES" : "NO") << '\n'; 
»
2 months ago, # |
-11

# include

using namespace std;

int main() { int t; cin>>t; while(t--){ string s; cin>>s;

int sum=0;
int sum1=0;
int x=s.length()/2;
for(int i=0;i<x;i++){
sum=sum+(s[i]-'0');
}
for(int i=x;i<s.length();i++){
sum1=sum1+(s[i]-'0');
}

if(sum==sum1){
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
}

} See the above code do some dry run figure out the mistake in your code by yourself. It will make you a good cute man

 » 2 months ago, # | ← Rev. 2 →   +8 i think if u use % and / will be better for u how to do itthree time{ sum1+=n%10 n/=10 } three time{ sum2+=n%10 n/=10 } and compere try it in paper it's easy idea
 » 2 months ago, # |   0 while (t--) {str s; cin >> s;if (((int)s[0] + (int)s[1] + (int)s[2]) == ((int)s[3] + (int)s[4] + (int)s[5])) cout << "YES" << endl;else cout << "NO" << endl;}