TachibanaMarika @ 2024-01-04 18:04:14
#include<iostream>
#include<string>
using namespace std;
int get(int n) {
if (n == 0)return 1;
int t = n;
int i = 0;
while (n != 0) {
n /= 10;
i++;
}
if (t > 0)
return i;
else
return i+1 ;
}
int main()
{
int i = 0;
string t = "\0";
getline(cin, t);
for (int m = 0; t[m] != '\0'; m++) {
i = i * 10 + t[m] - '0';
}
char sign = 'a';
int sum = 0;
int a1 = 0;
string* s = new string[i];
for (int n = 0; n < i; n++) {
getline(cin, s[n]);
}
for (int n = 0; n < i; n++) {
int count = 0;
int k = 0;
sum = a1 = 0;
if (s[n][0] >= 'a' && s[n][0] <= 'c') {
sign = s[n][0];
}
if (sign == 'a') {
for (int m = 0; s[n][m] != '\0'; m++) {
if (s[n][m] <= '9' && s[n][m] >= '0') {
a1 = a1 * 10 + s[n][m] - '0';
k = m;
}
else {
if (s[n][k] <= '9' && s[n][k] >= '0') {
cout << a1 << "+";
count += get(a1);
}
sum += a1;
a1 = 0;
}
}
sum += a1;
count += get(a1) + get(sum);
cout << a1 << "=" << sum << endl << count +2 << endl;
}
else if (sign == 'b') {
int k = 0;
for (int m = 0; s[n][m] != '\0'; m++) {
if (s[n][m] <= '9' && s[n][m] >= '0') {
sum = sum * 10 + s[n][m] - '0';
}
if (m >= 1) {
if (s[n][m] == ' ' && s[n][m - 1] != 'b') {
k = m;
break;
}
}
k = m;
}
cout << sum << "-";
count += get(sum);
while (s[n][++k] != '\0') {
a1 = a1 * 10 + s[n][k] - '0';
}
sum -= a1;
count += get(a1) + get(sum);
cout << a1 << "=" << sum << endl << count + 2 << endl;
}
else {
sum = 1;
for (int m = 0; s[n][m] != '\0'; m++) {
if (s[n][m] <= '9' && s[n][m] >= '0') {
a1 = a1 * 10 + s[n][m] - '0';
k = m;
}
else {
if (s[n][k] <= '9' && s[n][k] >= '0') {
cout << a1 << "*";
count += get(a1);
sum *= a1;
}
a1 = 0;
}
}
sum *= a1;
count += get(a1) + get(sum);
cout << a1 << "=" << sum << endl << count + 2 << endl;
}
}
return 0;
}
明明试了好几组都可以,也下了第一组测试数据也可以,但1,3,4测试点RE其余都是WA
by GXZJQ @ 2024-01-04 18:34:37
#include<bits/stdc++.h>
using namespace std;
char yunsuanfu;
int n, num1, num2;
char s[100], b[10];
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> b;
if (b[0] >= 'a' && b[0] <= 'z') {
yunsuanfu = b[0];
cin >> num1 >> num2;
} else {
sscanf(b, "%d", &num1);
cin >> num2;
}
memset(s, 0, sizeof(s));
if (yunsuanfu == 'a') {
sprintf(s, "%d+%d=%d", num1, num2, num1 + num2);
} else if (yunsuanfu == 'b') {
sprintf(s, "%d-%d=%d", num1, num2, num1 - num2);
} else if (yunsuanfu == 'c') {
sprintf(s, "%d*%d=%d", num1, num2, num1 * num2);
}
cout << s << endl << strlen(s) << endl;
}
return 0;
}
@TachibanaMarika