Feiyu successfully found his way out of the maze, but! Feiyu has no combat abilities, he can only go to the shop in Math City to purchase a wooden sword.
But wait! He's not in Math City anymore; he's in Anti-Math City.
Feiyu can only defeat Anti-Math Monsters to escape Anti-Math City!!!
Fei Yu encounters the first monster in Anti-Math City — the Anti-Addition Beast!!!
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The first line indicates the number of monsters (Anti-Addition Beasts). Then repeat the following procedure the number of times indicated in the first line. Input an addition equation. Also, input the answer. | If the answer is correct, output "yes". If the answer is wrong, output "no". |
3 1+1 2 2+4 5 5+8 4 | yes no no |
Thought Process
Use a string to store the addition expression. Run a for loop over the string. Declare an empty string, and concatenate each character to it in each iteration. If the current character is "+", convert the accumulated string into an integer and add it to the integer obtained from string.substr(current position + 1, string.length()).
If the calculated answer matches the input, output "yes"; otherwise, output "no".
Sample Code-ZeroJudge K678: Adding Numbers
#include <iostream>
using namespace std;
int toint(string str)
{
if (str.length() == 1) return int(str[0] - '0');
return stoi(str);
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
for (int i = 0; i<N; i++)
{
string str, tmp = "";
cin >> str;
int sum = 0;
for (int j = 0; j<str.length(); j++)
{
if (str[j] == '+')
{
sum += toint(tmp);
sum += toint(str.substr(j+1, str.length()));
break;
}
tmp += str[j];
}
int ans;
cin >> ans;
if (sum == ans) cout << "yes\n";
else cout << "no\n";
}
}
//ZeroJudge K678
//Dr. SeanXD