caomuyilin @ 2022-06-23 19:26:44
#include <bits/stdc++.h>
using namespace std;
string s;
char c;
int cal(char a, int b, int c)
{
if (a == '+') return b + c;
if (a == '-') return b - c;
if (a == '*') return b * c;
return b + c;
}
int getlen(int a)
{
int len = 0, flag = 0;
if (a < 0)
{
flag = 1;
a = -a;
}
while (a)
{
a /= 10;
len++;
}
return len + flag;
}
void process(string s)
{
int a = 0, b = 0, i = 0;
int len = s.length(), ans=0;
string temp;
if (s[0] >= 'a' && s[0] <= 'z')
{
c = s[0];
if (c == 'a') c = '+';
else if (c == 'b') c = '-';
else c = '*';
i = 2;
}
while (s[i] != ' ' && i < len) { a = a * 10 + s[i] - '0'; i++;}
i++;
while (s[i] != '\0' && i < len) { b = b * 10 + s[i] - '0'; i++;}
cout << a << c << b << "=" << cal(c, a, b) << endl;
ans = getlen(cal(c, a, b))+getlen(a)+getlen(b)+2;
cout << ans << endl;
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(NULL);
// freopen("1.in", "r", stdin);
// freopen("test.out", "w", stdout);
int n;
cin >> n;
getline(cin, s);
for (int i = 1; i <= n; i++)
{
getline(cin, s);
process(s);
}
return 0;
}
代码样例下载验证都是没问题的,就是提交无法通过,有大佬帮忙看下
by quliannanyishou @ 2022-06-23 21:20:51
@caomuyilin 这好像是落谷在线ide的问题,好像会读入末尾的换行符,输入成这样(见代码)。 而且你代码有问题当getlen里的a==0时返回的长度是0,应该是1,也帮你改了
#include <bits/stdc++.h>
using namespace std;
string s;
char c;
int cal(char a, int b, int c)
{
if (a == '+') return b + c;
if (a == '-') return b - c;
if (a == '*') return b * c;
return b + c;
}
int getlen(int a)
{
int len = 0, flag = 0;
if(a==0)
{
return 1;
}
if (a < 0)
{
flag = 1;
a = -a;
}
while (a)
{
a /= 10;
len++;
}
return len + flag;
}
void process(string s)
{
int a = 0, b = 0, i = 0;
int len = s.length(), ans=0;
string temp;
if (s[0] >= 'a' && s[0] <= 'z')
{
c = s[0];
if (c == 'a') c = '+';
else if (c == 'b') c = '-';
else c = '*';
i = 2;
}
while (s[i] != ' ' && i < len) { a = a * 10 + s[i] - '0'; i++;}
i++;
while (s[i] != '\0') { b = b * 10 + s[i] - '0'; i++;}
cout <<a<<c<< b << "=" << cal(c, a, b) << endl;
ans = getlen(cal(c, a, b))+getlen(a)+getlen(b)+2;
cout << ans << endl;
}
int main()
{
int n;
string k,l;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin>>s;
if(s[0] >= 'a' && s[0] <= 'z')
{
cin>>k>>l;
s=s+" "+k+" "+l;
}
else
{
cin>>k;
s=s+" "+k;
}
process(s);
}
return 0;
}
by quliannanyishou @ 2022-06-23 21:45:50
@caomuyilin 额,过了怎么不说一声,整的我挺尴尬
by caomuyilin @ 2022-06-23 23:22:18
@quliannanyishou 谢谢大哥