yzy0323 @ 2024-02-26 16:48:34
#include <iostream>
#include <algorithm>
using namespace std;
int cal(int num)
{
int wei = 0;
if (num == 0)
return 1;
while (num >= 1 || num <= -1)
{
num /= 10;
wei++;
}
return wei;
}
int main()
{
int n, a, b, c;
char ch,cht;
cin >> n;
cin >> ch;
for (int i = 0; i < n; i++)
{
cin >> cht;
int wei;
if (cht >= '0' && cht <= '9')
{
char tnum[10005];
int j;
cin.get(tnum+1,10004);
tnum[0] = cht;
a = b = 0;
for (j = 0; tnum[j] != ' '; j++)
a = a * 10 + tnum[j] - '0';
while (tnum[++j] != '\0')
{
b = b * 10 + tnum[j] - '0';
}
}
else
{
cin >> a >> b;
ch = cht;
}
switch (ch)
{
case 'a':
c = a + b;
wei = cal(a) + cal(b) + cal(c) + 2;
printf("%d+%d=%d\n%d\n", a, b, c, wei);
break;
case 'b':
c = a - b;
wei = cal(a) + cal(b) + cal(c) + 2;
if (c < 0)
wei++;
printf("%d-%d=%d\n%d\n", a, b, c, wei);
break;
case 'c':
c = a * b;
wei = cal(a) + cal(b) + cal(c) + 2;
printf("%d*%d=%d\n%d\n", a, b, c, wei);
break;
}
}
return 0;
}
//
by cpp_xhq @ 2024-03-16 09:57:44
我的代码
#include <bits/stdc++.h>
using namespace std;
int n,x,y;
string a;
int yx=0;
int longlong(int a)
{
int z=0;
if(a==0) z=1;
if(a<=0) z=1;
while(a)
{
a/=10;
z++;
}
return z;
}
int strzint(string a)
{
int n;
for(int i=0;i<a.size();i++)
{
n=n*10+(a[i]-'0');
}
return n;
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a;
if (a.size()==1&&a[0]>='a'&&a[0]<='c')
{
yx=a[0]-'a';//a==0 b==1 c==2;
cin>>x>>y;
if (yx==0)
{
cout<<x<<"+"<<y<<"="<<(x+y)<<endl;
cout<<(longlong(x)+longlong(y)+longlong(x+y)+2);
}else if(yx==1)
{
cout<<x<<"-"<<y<<"="<<(x-y)<<endl;
cout<<(longlong(x)+longlong(y)+longlong(x-y)+2);
}else
{
cout<<x<<"*"<<y<<"="<<(x*y)<<endl;
cout<<(longlong(x)+longlong(y)+longlong(x*y)+2);
}
cout<<endl;
}else
{
x=strzint(a);
cin>>y;
if (yx==0)
{
cout<<x<<"+"<<y<<"="<<(x+y)<<endl;
cout<<(longlong(x)+longlong(y)+longlong(x+y)+2);
}else if(yx==1)
{
cout<<x<<"-"<<y<<"="<<(x-y)<<endl;
cout<<(longlong(x)+longlong(y)+longlong(x-y)+2);
}else
{
cout<<x<<"*"<<y<<"="<<(x*y)<<endl;
cout<<(longlong(x)+longlong(y)+longlong(x*y)+2);
}
cout<<endl;
}
}
return 0;
}
by yzy0323 @ 2024-03-16 09:59:31
@cpp_xhq ch表示当前的表示字符,就是算术的类型,从第二个测试样例开始,输入的第一个字符为cht,同时把后面的数字暂存,然后cht判断是字母还是数字,是字母就更改ch,数字就不用更改。
by cpp_xhq @ 2024-03-16 10:02:37
@yzy0323
if (cht >= '0' && cht <= '9')
第一次应该判断ch
by cpp_xhq @ 2024-03-16 10:04:01
@cpp_xhq 不对
by yzy0323 @ 2024-03-16 10:04:32
@cpp_xhq 谢谢,我也是这个思路
by NTLT60 @ 2024-03-18 14:42:48
@违规用户名971024 大佬牛逼