shinkaifan @ 2024-01-10 23:38:24
代码已经满足需求,但全部测试点WA,报错为“行数太少”,似乎是输出控制? 代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int N;
cin>>N;
cin.ignore();
bool sym[3]={0};
for(int i=1;i<=N;i++)
{
bool ischanged = false;
string str;
char str_res[100];
getline(cin,str);
//转化为字符串,判断字符串首位
if(str[0]=='a')
{
sym[0]=1;
sym[1]=0;
sym[2]=0;
ischanged = 1;
}
if(str[0]=='b')
{
sym[0]=0;
sym[1]=1;
sym[2]=0;
ischanged = 1;
}
if(str[0]=='c')
{
sym[0]=0;
sym[1]=0;
sym[2]=1;
ischanged = 1;
}
//再转化为流,便于分割
istringstream iss(str);
int num1,num2,res;
char temp;
if(ischanged)
{
iss>>temp;
}
iss>>num1>>num2;
//分情况输出,转化为字符串便于计算长度
if(sym[0])
{
res = num1+num2;
sprintf(str_res,"%d+%d=%d",num1,num2,res);
string s = str_res;
cout<<str_res<<endl<<s.size();
if(i<N)
cout<<endl;
}
if(sym[1])
{
res = num1-num2;
sprintf(str_res,"%d-%d=%d",num1,num2,res);
string s = str_res;
cout<<str_res<<endl<<s.size();
if(i<N)
cout<<endl;
}
if(sym[2])
{
res = num1*num2;
sprintf(str_res,"%d*%d=%d",num1,num2,res);
string s = str_res;
cout<<str_res<<endl<<s.size();
if(i<N)
cout<<endl;
}
}
return 0;
}