洛谷P1957 口算练习题
这道题难度是入门 真的是没啥难度的感觉 ?
题目链接
迅速写完提交,80分,手动滑稽。
才发现真的是很容易踩雷啊
运算数是非负整数,也就是说,可能为0!!!虽然运算数是非负整数,可是没说结果是非负整数哦,所以样例第三个的-18的长度是3呢.事实证明不能太相信自己对字符串转数字类题目的掌握啊,这个模板记下了!
#include <iostream>
using namespace std;
int length(int a)
{
int t=0;
if(a<0||a==0) t++;
while(a)
{
t=t+1;
a=a/10;
}
return t;
}
int zhuan(string s)
{
int t=0,i=0;
if(s[0]=='-') i=1;
//如果运算数有负数的话会需要
for(;i<s.length();i++)
{
t=t*10+s[i]-'0';
}
if(s[0]=='-') t=t*(-1);
return t;
}
int main()
{
int n,a,b,sum;
cin>>n;
string s;
char ch;
for(int i=0;i<n;i++)
{
cin>>s;
if(s=="a") {ch='+';cin>>s;}
if(s=="b"){ch='-';cin>>s;}
if(s=="c"){ch='*';cin>>s;}
a=zhuan(s);
cin>>s;
b=zhuan(s);
if(ch=='+') {sum=a+b;}
if(ch=='-'){sum=a-b;}
if(ch=='*'){sum=a*b;}
cout<<a<<ch<<b<<'='<<sum<<endl;
cout<<length(a)+length(b)+length(sum)+2<<endl;
}
}