洛谷IDE与洛谷测评机是否相同?
查看原帖
洛谷IDE与洛谷测评机是否相同?
242524
JRzyh楼主2023/8/24 20:40

代码在提交时RE,但IDE上正常

怀疑是getline的bug,所以同时问问有无可以替代getline(cin,str)的读入整行方式?

提交记录

代码:

#include<bits/stdc++.h>
using namespace std;
int pow10[9]={1,10,100,1000,10000,100000,1000000,10000000,100000000};
string solu(int a,int b,int op)
{
	int ans;
	string rt="";
	if(op==1)ans=a+b;
	if(op==2)ans=a-b;
	if(op==3)ans=a*b;
	if(ans<0)rt="-",ans=-ans;
	int sz=log(ans)/log(10);
	//cout<<ans<<endl;
	while(sz>=0)
	{
		//cout<<sz<<" ";
		rt+=(ans/pow10[sz])%10+'0';
		sz--;
	}
	return rt;
}
char opt[5];
int n,op;
string str;
int main()
{
	opt[1]='+';opt[2]='-';opt[3]='*';
	cin>>n;
	getchar();
	for(int j=1;j<=n;j++)
	{
		int st=0;
		getline(cin,str);
		//cout<<str<<endl; 
		if(str[0]=='a')op=1,st=2;
		if(str[0]=='b')op=2,st=2;
		if(str[0]=='c')op=3,st=2;
		//cout<<1;
			int a=0,b=0,i;
			string ans="";
			for(i=st;str[i]!=' ';i++)
			{
				a*=10;
				a+=(str[i]-'0');
				ans+=str[i];
				//cout<<str[i]<<endl;
			}
			//cout<<a<<endl;
			i++;
			ans+=opt[op];
			for(;i<str.size();i++)
			{
				b*=10;
				b+=(str[i]-'0');
				ans+=str[i];
				//cout<<str[i]<<endl;
			}
			//cout<<b<<endl;
			ans+='=';
			ans+=solu(a,b,op);
			//cout<<1;
			cout<<ans<<endl<<ans.size()<<endl;
	}
    return 0;
}

2023/8/24 20:40
加载中...