求助,本地能过但是洛谷全红
查看原帖
求助,本地能过但是洛谷全红
659308
aa9527楼主2022/7/7 19:22
#include<iostream>
#include<string.h>
using namespace std;
int length_int(int n)		//判断数字长度 
{
	int i=0;
	if(n<0)
	{
		n=n*(-1);
		i=1;
	}
	while(n>0)
	{
		n=n/10;
		i++;
	}
	return i;
}
int main()
{
	int n=0;
	cin>>n;
	char str[50]={0};
	int num[100]={0};
	int flag=0;
	int m=0;
	getchar();
	while(flag<n)
	{
		char buf=getchar();
		if(buf=='\n' || buf=='\r')
		{
			flag++;
			m++;
		}
		else if(buf>='a' && buf<='c')
		{
			str[flag]=buf;
		}
		else if(buf>='0' && buf<='9')
		{
			num[m]=num[m]*10+atoi(&buf);
		}
		else if(buf==' ')
		{
			if(num[m]!=0)
				m++;
		}
	}
	int j=0;
	int s=0;
	for(int i=0;i<n;i++)
	{
		if(str[i]!='a' && str[i]!='b' && str[i]!='c')
			str[i]=str[i-1];
		cout<<to_string(num[j]);
		if(str[i]=='a')
		{
			cout<<"+";
			s=num[j]+num[j+1];	
		}
		else if(str[i]=='b')
		{
			cout<<"-";
			s=num[j]-num[j+1];
		}
		else if(str[i]=='c')
		{
			cout<<"*";
			s=num[j]*num[j+1];
		}
		cout<<to_string(num[j+1]);
		cout<<"=";
		cout<<to_string(s)<<endl;
		cout<<length_int(num[j])+length_int(num[j+1])+length_int(s)+2;
		j=j+2;
		if(i<n-1)
			cout<<endl;
	}
}
2022/7/7 19:22
加载中...