huaihuaihuai @ 2020-10-20 17:14:17
#include <stdio.h>
#include <string.h>
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
char math;
int num[2];
scanf("\n%c",&math);
if(math <97){//即非abccd
scanf("%d %d",&num[0],&num[1]);
int t=num[0];
//还原数字
int cum=0;
while(t!=0){
t/=10;
cum++;
}
math-=48;//实现从字符到整形的转换
int shu=math;//math用个int来代替防止溢出
for(int i=0;i<cum;i++) shu*=10;//还原数字
num[0]+=shu; //还原数字
int N=(printf("%d+%d=%d\n",num[0],num[1],num[0]+num[1]));
printf("%d\n",N-1);//多了个换行所以要-1
}
else{
scanf("%d %d",&num[0],&num[1]);
char ch;
int sum;
switch(math){
case 'a':{
ch='+';
sum=num[0]+num[1];
break;
}
case 'b':{
ch='-';
sum=num[0]-num[1];
break;
}
case 'c':{
ch='*';
sum=num[0]*num[1];
break;
}
}
int N=(printf("%d%c%d=%d\n",num[0],ch,num[1],sum));
printf("%d\n",N-1);
}
}
return 0;
}
by Accepted喵 @ 2020-10-21 23:17:58
先附上自己的代码:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
string a[50+2];
char b[50+2];
int check(int a) {
int ans=0;
if(a==0) return 1;
else if(a<0){
ans=1;
a=-a;
}
while(a) {
ans++;
a/=10;
}
return ans;
}
int main(){
int n;
scanf("%d ",&n);
for(int i=0;i<n;i++){
getline(cin,a[i]);
if(a[i][0]>='a'&&a[i][0]<='c'){
if(a[i][0]=='a') b[i]='+';
else if(a[i][0]=='b') b[i]='-';
else if(a[i][0]=='c') b[i]='*';
}
else if(a[i][0]>='0'&&a[i][0]<='9') b[i]=b[i-1];
}
for(int i=0;i<n;i++){
int c=0,d=0,j,w=1;
int len=a[i].size();
for(j=len-1;j>=0;j--){
if(a[i][j]>='0'&&a[i][j]<='9'){
d=d+(a[i][j]-'0')*w;
w*=10;
}
if(a[i][j]==' ') break;
}
w=1;
for(j-=1;j>=0;j--){
if(a[i][j]>='0'&&a[i][j]<='9'){
c=c+(a[i][j]-'0')*w;
w*=10;
}
if(a[i][j]==' ') break;
}
printf("%d%c%d=",c,b[i],d);
int an;
if(b[i]=='+') an=c+d;
else if(b[i]=='-') an=c-d;
else if(b[i]=='*') an=c*d;
printf("%d\n",an);
printf("%d\n",check(c)+check(d)+check(an)+2);
}
}
提示:看到题目标签“字符串”
by huaihuaihuai @ 2020-10-22 09:40:34
@Accepted喵 多谢已经找到错误之处
by Accepted喵 @ 2020-10-23 13:03:04
@huai880 不谢!
by Accepted喵 @ 2020-10-23 13:04:25
@huai880 还没过啊