bluetored @ 2022-10-05 20:25:28
就过了两个点
#include <stdio.h>
#include <string.h>
int len (int n)
{
int cnt=0;
do
{
n/=10;
cnt++;
}
while (n!=0);
return cnt;
}
int main ()
{
char cal[100];
int n;
int a,b,res;
char p[51];
char s;
scanf ("%d",&n);
for (int i=1;i<=n;i++)
{
scanf ("%s",p);
if(p[0]<='z'&&p[0]>='a')
{
s=p[0];
scanf ("%d %d",&a,&b);
if (s=='a'){
res =a+b;
printf ("%d+%d=%d\n",a,b,res);
printf ("%d\n",len(a)+len(b)+len(res)+2);
}
if (s=='b'){
res =a-b;
printf ("%d-%d=%d\n",a,b,res);
printf ("%d\n",len(a)+len(b)+len(res)+2);
}
if (s=='c'){
res =a*b;
printf ("%d*%d=%d\n",a,b,res);
printf ("%d\n",len(a)+len(b)+len(res)+2);
}
}
else {
a=0;
for (int i=0;i<strlen(p);i++)
{
a=a*10+p[i]-'0';
}
scanf ("%d",&b);
if (s=='a'){
res =a+b;
printf ("%d+%d=%d\n",a,b,res);
printf ("%d\n",len(a)+len(b)+len(res)+2);
}
if (s=='b'){
res =a-b;
printf ("%d-%d=%d\n",a,b,res);
printf ("%d\n",len(a)+len(b)+len(res)+2);
}
if (s=='c'){
res =a*b;
printf ("%d*%d=%d\n",a,b,res);
printf ("%d\n",len(a)+len(b)+len(res)+2);
}
}
}
return 0;
}
by Feng_Jing @ 2022-10-05 20:28:07
@bluetored 请问哈,为什么你不直接输入两个数呢
by Hisaishi_Kanade @ 2022-10-05 20:32:06
sscanf
by bluetored @ 2022-10-05 20:35:41
@bye_wjx 这个C++才能用吧
by bluetored @ 2022-10-05 20:42:17
@FengJingFJ2022 你是指的else那部分吗?如果是,我的意思是把p[]中储存的值给a,然后只给b赋值,所以值输入了一个数。我是小白还有很多不明白的要请教你们。
by bluetored @ 2022-10-05 20:43:38
找到错误了,带负号的数应该让长度加一,终于满分了,泪目