Renader @ 2021-02-17 23:51:02
测试可以过,第一个也可以过,第一个WA说第一行过短
#include <bits/stdc++.h>
using namespace std;
int string_to_int(string x){
stringstream y;
y << x;
int z;
y >> z;
return z;
}
string int_to_string(int x){
stringstream y;
y << x;
return y.str();
}
string r[55];
int r1[55];
int n;
int s = 0;
char type, memo;
string x, y;
string z;
bool flag = true;
int main()
{
cin >> n;
int count = 0;
for(int i = 0; i < n; i++){
fflush(stdin);
if(flag){
type = getchar();
if(type == 'a' || type == 'b' || type == 'c'){
memo = type;
}
}
switch(memo){
case 'a':
cin >> x >> y;
if(type != 'a'){
flag = true;
x = type + x;
}
z = int_to_string(string_to_int(x)+string_to_int(y));
count += x.length()+y.length()+z.length()+2;
r[s] = x+"+"+y+"="+z;
r1[s++] = count;
count = 0;
break;
case 'b':
cin >> x >> y;
if(type != 'b'){
flag = true;
x = type + x;;
}
z = int_to_string(string_to_int(x)-string_to_int(y));
count += x.length()+y.length()+z.length()+2;
r[s] = x+"-"+y+"="+z;
r1[s++] = count;
count = 0;
break;
case 'c':
cin >> x >> y;
if(type != 'c'){
flag = true;
x = type + x;
}
z = int_to_string(string_to_int(x)*string_to_int(y));
count += x.length()+y.length()+z.length()+2;
r[s] = x+"*"+y+"="+z;
r1[s++] = count;
count = 0;
break;
}
}
for(int i = 0; i < s; i++){
cout << r[i] << endl << r1[i] << endl;
}
return 0;
}
by q779 @ 2021-02-18 00:05:10
有一个函数叫stoi(),还有一个函数叫to_string()您可以看看
by Renader @ 2021-02-18 00:08:05
@q779 好的多谢,我去试试
by q779 @ 2021-02-18 00:10:46
也许substr()和getline()您也可以用到(
by Renader @ 2021-02-18 00:21:47
@q779 我一开始是一直超时,1.2s,改了一部分减少了循环次数,现在是不超时,但是是WA。我把数据下载下来运行输入输出是正确的,不知道您说的这些能不能解决.总之多谢啦
by NusGhy @ 2021-02-18 08:18:14
字符串题建议去洛谷在线ide调
by Renader @ 2021-02-18 12:05:04
终于AC了,给后面遇到相同问题的人一个坑点吧 fflush(stdin);这个清键盘缓存的函数在洛谷不是很管用,用洛谷在线IDE调试可以看出来