欢迎光临
我们一直在努力

东华OJ-基础题-87-弟弟的作业(C++)

  • 问题描述 你的弟弟刚做完了“100以内数的加减法”这部分的作业,请你帮他检查一下。每道题目(包括弟弟的答案)的格式为a+b=c或者a-b=c,其中a和b是作业中给出的,均为不超过100的非负整数;c是弟弟算出的答案,可能是不超过200的非负整数,也可能是单个字符"?",表示他不会算。
  • 输入说明 输入文件包含不超过100行,以文件结束符结尾。每行包含一道题目,格式保证符合上述规定,且不包含任何空白字符。输入的所有整数均不含前导0。
  • 输出说明 输出仅一行,包含一个非负整数,即弟弟答对的题目数量。
  • 输入范例

55+12=67
158=7
10035=?
8350=33
43=6
81+5=21

  • 输出范例

3

感想:用find()找到符号位置,用substr()剪取所需片段,stoi()将字符串转为int ,to_string()将int转为string 代码如下:

#include <bits/stdc++.h>
using namespace std;

int main() {
string str;
int cnt = 0;
while(getline(cin,str)) {
int a,b,res;
auto add = str.find('+');
auto deduct = str.find('-');
auto eq = str.find('=');
if(add != string::npos) {
a = stoi(str.substr(0,add));
b = stoi(str.substr(add+1,eqadd1));
res = a+b;
}
if(deduct != string:: npos) {
a = stoi(str.substr(0,deduct));
b = stoi(str.substr(deduct+1,eqdeduct1));
res = ab;
}

string ans = str.substr(eq+1,str.size()eq1);
if( to_string(res) == ans)
++cnt;
}

cout<<cnt;
return 0;
}

赞(0)
未经允许不得转载:171主机测评 » 东华OJ-基础题-87-弟弟的作业(C++)
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址