whytaciturn @ 2022-03-28 15:15:08
没加就全错了,加后过了
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int n;
char last,s[20],ans[20];
scanf("%d\n",&n);
while(n--)
{
int a,b,c;
fgets(s,sizeof(s),stdin);
if(s[0]=='a'||s[0]=='b'||s[0]=='c')
{last=s[0];s[0]=' ';}
sscanf(s,"%d %d",&a,&b);
switch(last){
case 'a':c=a+b;sprintf(ans,"%d+%d=%d",a,b,c);break;
case 'b':c=a-b;sprintf(ans,"%d-%d=%d",a,b,c);break;
case 'c':c=a*b;sprintf(ans,"%d*%d=%d",a,b,c);break;
}
printf("%s\n%d\n",ans,strlen(ans));
}
}
by lazy_dog_01 @ 2022-03-28 15:19:58
你后面循环体内用stdin读字符串,不加的话后面的换行就被当成字符串读进去,通常这种情况直接在后面加getchar()。
by SubscribeDiana @ 2022-03-28 15:25:33
换行符被读进去了。应该可以改用 cin
规避这个问题
by whytaciturn @ 2022-03-28 15:28:53
@lazy_dog_01 是不是输入完n后它或我必须要换行一下,而且只能是我来手动换行
by whytaciturn @ 2022-03-28 15:30:40
@SubscribeDiana 嗯嗯
by SubscribeDiana @ 2022-03-28 16:59:35
我的习惯是有字符串输入的题目一律用 cin
。有的题有多的换行符有的没有,很难搞