Блог пользователя Motaz_Husain

Автор Motaz_Husain, 18 месяцев назад, По-английски

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

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
  • Проголосовать: не нравится

»
18 месяцев назад, # |
  Проголосовать: нравится +9 Проголосовать: не нравится

Initialise sum1=sum2=0

»
18 месяцев назад, # |
  Проголосовать: нравится 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. :)

  • »
    »
    18 месяцев назад, # ^ |
      Проголосовать: нравится 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.

»
18 месяцев назад, # |
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.

»
18 месяцев назад, # |
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';
»
18 месяцев назад, # |
Rev. 2   Проголосовать: нравится +8 Проголосовать: не нравится

i think if u use % and / will be better for u

how to do it

try it in paper it's easy idea

»
18 месяцев назад, # |
  Проголосовать: нравится 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;

}