木鸟 @ 2020-10-19 22:47:56
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define MAXA 50
char str[MAXA], s1[MAXA], s2[MAXA], s[MAXA];
int main(void)
{
char c;
int n, a1, a2;
scanf("%d", &n);
getchar();
//getchar();
for(int i = 1; i <= n; i++)
{
memset(str, '\0', sizeof(str));
memset(s1, '\0', sizeof(s1));
memset(s2, '\0', sizeof(s2));
memset(s, '\0', sizeof(s));
gets(str);
//getchar();
if(isalpha(str[0]) != 0)
{
int j, k;
c = str[0];
for(j = 2; ; j++)
{
if(str[j] == ' ')
break;
s1[j - 2] = str[j];
}
for(k = j + 1; k < strlen(str); k++)
s2[k - j - 1] = str[k];
}
else
{
int j, k;
for(j = 0; ; j++)
{
if(str[j] == ' ')
break;
s1[j] = str[j];
}
for(k = j + 1; k < strlen(str); k++)
s2[k - j - 1] = str[k];
}
//puts(s1);
//puts(s2);
a1 = atoi(s1);
a2 = atoi(s2);
if(c == 'a')
{
printf("%d+%d=%d\n", a1, a2, a1 + a2);
sprintf(s, "%d", a1 + a2);
printf("%d\n", strlen(s1) + strlen(s2) + strlen(s) + 2);
}
else if(c == 'b')
{
printf("%d-%d=%d\n", a1, a2, a1 - a2);
sprintf(s, "%d", a1 - a2);
printf("%d\n", strlen(s1) + strlen(s2) + strlen(s) + 2);
}
else if(c == 'c')
{
printf("%d*%d=%d\n", a1, a2, a1 * a2);
sprintf(s, "%d", a1 * a2);
printf("%d\n", strlen(s1) + strlen(s2) + strlen(s) + 2);
}
}
return 0;
}
by OOmega @ 2020-10-19 23:37:48
数组小了?
by 木鸟 @ 2020-10-20 09:51:04
@Omega癸 应该不是,即使换了数组大小到10000左右,还是全部RE
by 木鸟 @ 2020-10-20 10:16:06
有大佬帮忙看一下吗。。。
by asdypeij @ 2020-10-23 15:16:42
我也是
#include <iostream>
#include <sstream>
#include <string.h>
#include <string>
using namespace std;
int n, a, b, ans, len;
char opt, s[20];
int main()
{
cin >> n;
getchar();
while(n){
gets(s);
a=b=0;
if(s[0]=='a'){
opt='+';
for(int i=1; i<strlen(s); i++){
if(s[i]!=' ')
{
b=b*10+s[i]-'0';
}
else if(s[i]==' '){
a=b;
b=0;
}
}
ans=a+b;
len=to_string(ans).length()+strlen(s)-1;
cout << a << "+" << b << "=" << ans << "\n";
cout << len << endl;
}
else if(s[0]=='b'){
opt='-';
for(int i=1; i<strlen(s); i++){
if(s[i]!=' ')
{
b=b*10+s[i]-'0';
}
else if(s[i]==' '){
a=b;
b=0;
}
}
ans=a-b;
len=to_string(ans).length()+strlen(s)-1;
cout << a << "-" << b << "=" << ans << "\n";
cout << len << "\n";
}
else if(s[0]=='c'){
opt='*';
for(int i=1; i<strlen(s); i++){
if(s[i]!=' ')
{
b=b*10+s[i]-'0';
}
else if(s[i]==' '){
a=b;
b=0;
}
}
ans=a*b;
len=to_string(ans).length()+strlen(s)-1;
cout << a << "*" << b << "=" << ans << "\n";
cout << len << "\n";
}
else if(s[0]!='a' && s[0]!='b' && s[0]!='c'){
for(int i=0; i<strlen(s); i++){
if(s[i]!=' ')
{
b=b*10+s[i]-'0';
}
else if(s[i]==' '){
a=b;
b=0;
}
}
if(opt=='+') ans=a+b;
if(opt=='-') ans=a-b;
if(opt=='*') ans=a*b;
len=to_string(ans).length()+strlen(s)+1;
cout << a << opt << b << "=" << ans << "\n";
cout << len << "\n";
}
n--;
}
return 0;
}