logo by user c1

basement
community

search

wall of shame

software What do you need help with?

joined feb 18, 2023

avatar

joined feb 18, 2023

Ask any question about software that you need some help with!

P.S. you can also answer questions too

edited 3/3/2023, 9:32 pm

joined dec 5, 2022

avatar

testing one two

joined dec 5, 2022

Hi coderguy.

I'm teaching myself C++ by solving problems over at Kattis (https://open.kattis.com/) and I'm slamming my head against one problem in particular right now. The issue is that when I submit my code it doesn't solve all the test cases and I can't see where in my code I am screwing up.

The problem: https://open.kattis.com/problems/bluetooth - in short Harold Bluetooth has bad teeth and you need to figure out how/if he can chew an apple.

My approach is to use a bunch of if/else statements, first to determine how many usable teeth Harold has in each side of the mouth, and finally to determine if he can use that side of the mouth to chew.

I was wondering if you could take a look at it and suggest where I'm screwing up.

My code:

#include <iostream> #include <string> using namespace std; int main(){ string tooth; char status; int n, lower_left = 8, upper_left = 8, lower_right = 8, upper_right = 8; cin >> n; for (int i = 0 ; i < n ; i++){ cin >> tooth >> status; if (tooth[0] == '-' && status == 'm'){lower_left -= 1;} else if (tooth[0] == '-' && status == 'b'){lower_left = 0;} else if (tooth[0] == '+' && status == 'm'){upper_left -= 1;} else if (tooth[0] == '+' && status == 'b'){upper_left = 0;} else if (tooth[1] == '-' && status == 'm'){lower_right -= 1;} else if (tooth[1] == '-' && status == 'b'){lower_right = 0;} else if (tooth[1] == '+' && status == 'm'){upper_right -= 1;} else if (tooth[1] == '+' && status == 'b'){upper_right = 0;} } if (lower_left < 1 || upper_left < 1){cout << 1 << endl;} else if(lower_right < 1 || upper_right < 1){cout << 0 << endl;} else {cout << 2 << endl;} return 0; }

posted 3/1/2023, 3:46 pm

joined dec 5, 2022

avatar

testing one two

joined dec 5, 2022

I had an epiphany while waiting in the checkout lane: it's the last bit that isn't rigorously defined. In short it doesn't check for cases where all teeth are missing, which is why I only got my problem partially solved. After rewriting I get 100% solved.

New code stuff:

if ((lower_left <= 0 || upper_left <= 0) && (lower_right <= 0 || upper_right <= 0)){ cout << 2 << endl; } else if ((lower_left <= 0 || upper_left <= 0) && (lower_right > 0 || upper_right > 0)){ cout << 1 << endl; } else if((lower_right <= 0 || upper_right <= 0) && (lower_left > 0 || upper_left > 0)){ cout << 0 << endl; } else { cout << 2 << endl; }

posted 3/3/2023, 11:46 am

joined feb 18, 2023

avatar

joined feb 18, 2023

quoting thatbirdguy:

I had an epiphany while waiting in the checkout lane: it's the last bit that isn't rigorously defined. In short it doesn't check for cases where all teeth are missing, which is why I only got my problem partially solved. After rewriting I get 100% solved.

Good job in solving the problem! Those IF statements look like a great solution to me!

edited 3/3/2023, 9:34 pm

software What do you need help with?