Problem Statement
|-O-| |-O-| |-O-| |-O-| |-O-|
Above you see a tiny fraction of our glorious imperial fleet, the Twin Ion Engine starfighters (known as "TIE fighters" for short). Each TIE fighter consists of two solar array wings ('|'), two wing braces ('-') and one central cabin ('O').
Even though our TIE fighters are clearly superior to the X-wing fighters flown by those pesky rebels, occasionally some TIE fighter will receive some accidental combat damage. Whenever a damaged TIE fighter is recovered, we disasemble it and salvage the parts that are still in working condition. It is now up to you to use these salvaged parts to assemble as many complete TIE fighters as possible.
You are given the string salvagedParts. Each character of salvagedParts represents one piece of machinery that has been salvaged. As mentioned above, the characters '|', '-', and 'O' (uppercase oh) represent the parts you need to build a TIE fighter. There may also be other characters in salvagedParts. These represent machinery that isn't used in a TIE fighter.
Class: TIEFighterAssembly
Method: assemble
Parameters: string
Returns: int
Method signature: int assemble(string salvagedParts)
(be sure your method is public)
Constraints:
salvagedParts will contain between 1 and 50 characters, inclusive.
Each character of salvagedParts will be one of "|-O=+()*" (quotes for clarity).
Examples
"|-O-|" Returns: 1 One fully functional TIE fighter.
My code to this is
class TIEFighterAssembly
{
public:
int assemble(string salvagedParts)
{
int a=0,b=0,c=0;
for(int i=0;i<salvagedParts.length();i++)
{
if(salvagedParts[i]=='-')a++;
else if(salvagedParts[i]=='|')b++;
else if(salvagedParts[i]=='O')c++;
}
int sum=0;
while(c)
{
if(c>0&&a>=2&&b>=2)
{
sum++;
c--;
a-=2;
b-=2;
}
else c=0;
}
return sum;
}
};
I got this error
Your code did not compile:
errors compiling:
In file included from top level:3:0:
TIEFighterAssembly.cc:4:15: error: 'string' has not been declared
int assemble(string salvagedParts)
^
TIEFighterAssembly.cc: In member function 'int TIEFighterAssembly::assemble(int)':
TIEFighterAssembly.cc:7:31: error: request for member 'length' in 'salvagedParts', which is of non-class type 'int'
for(int i=0;i<salvagedParts.length();i++)
^
TIEFighterAssembly.cc:9:22: error: invalid types 'int[int]' for array subscript
if(salvagedParts[i]=='-')a++;
^
TIEFighterAssembly.cc:10:27: error: invalid types 'int[int]' for array subscript
else if(salvagedParts[i]=='|')b++;
^
TIEFighterAssembly.cc:11:27: error: invalid types 'int[int]' for array subscript
else if(salvagedParts[i]=='O')c++;
^
In file included from top level:10:0:
Your class or method was improperly declared: In function 'int _wrapper::thunk(std::string)':
Your class or method was improperly declared:20034:3: error: no matching function for call to 'TIEFighterAssembly::assemble(std::string&)'
Your class or method was improperly declared:20034:3: note: candidate is:
In file included from top level:3:0:
TIEFighterAssembly.cc:4:6: note: int TIEFighterAssembly::assemble(int)
int assemble(string salvagedParts)
^
TIEFighterAssembly.cc:4:6: note: no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'int'
In file included from top level:10:0:
Your class or method was improperly declared:20037:1: warning: control reaches end of non-void function [-Wreturn-type]
Can anyone help me how to submit a problem in TopCoder