求助 P1957
  • 板块学术版
  • 楼主__20jiang09__
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/1/3 16:09
  • 上次更新2023/10/28 12:54:16
查看原帖
求助 P1957
398862
__20jiang09__楼主2022/1/3 16:09

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
string nowtype;
string itos(int x){
	string ans;
	do{
		int tmp=x%10;
		ans+=(tmp+'0');
		x/=10;
	}while(x>0);
	reverse(ans.begin(),ans.end());
	return ans;
}
int stoi(string x){
	int ans=0;
	for(int i=0;i<x.size();i++){
		ans=ans*10+(x[i]-'0');
	}
	return ans;
}
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		string type;
		int a,b;
		cin>>type;
		if(type[0]>='a'&&type[0]<='z'){
			nowtype=type;
			cin>>a>>b;
		}
		else{
			a=stoi(type);
			cin>>b;
		}
		if(nowtype=="a"){
			int ans=a+b;
			string stra,strb,strans;
			stra=itos(a);
			strb=itos(b);
			strans=itos(ans);
			string str;
			str=stra+'+'+strb+'='+strans;
			cout<<str<<endl<<str.size()<<endl;
		}
		if(nowtype=="b"){
			int ans=a-b;
			bool isf=0;
			if(ans<0){
				isf=1;
				ans=-ans;
			} 
			string stra,strb,strans;
			stra=itos(a);
			strb=itos(b);
			strans=itos(ans);
			string str;
			if(isf){
				str=stra+'-'+strb+'=';
				str=str+'-';
				str+=strans;
			}
			else  str=stra+'-'+strb+'='+strans;
			cout<<str<<endl<<str.size()<<endl;
		}
		if(nowtype=="c"){
			int ans=a*b;
			string stra,strb,strans;
			stra=itos(a);
			strb=itos(b);
			strans=itos(ans);
			string str;
			cout<<str<<endl<<str.size()<<endl;
		}
	}
	return 0;
}

在本地跑没问题,但是在洛谷里出现如下报错信息: /tmp/compiler_n127hl5k/src: 在函数‘int stoi(std::string)’中: /tmp/compiler_n127hl5k/src:17:22: 警告:comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string::size_type’ {aka ‘long unsigned int’} [-Wsign-compare] 17 | for(int i=0;i<x.size();i++){ | ~^~~~~~~~~ /tmp/compiler_n127hl5k/src: 在函数‘int main()’中: /tmp/compiler_n127hl5k/src:34:31: 错误:调用重载的‘stoi(std::string&)’有歧义 34 | a=stoi(typ); | ~~~~^~~~~ /tmp/compiler_n127hl5k/src:15:5: 附注:candidate: ‘int stoi(std::string)’ 15 | int stoi(string x){ | ^~~~ In file included from /nix/store/fdbr19mgwzmp1f17nbd9pqjv9vl9kzrq-luogu-gcc-11.2.0/include/c++/11.2.0/string:55, from /nix/store/fdbr19mgwzmp1f17nbd9pqjv9vl9kzrq-luogu-gcc-11.2.0/include/c++/11.2.0/bits/locale_classes.h:40, from /nix/store/fdbr19mgwzmp1f17nbd9pqjv9vl9kzrq-luogu-gcc-11.2.0/include/c++/11.2.0/bits/ios_base.h:41, from /nix/store/fdbr19mgwzmp1f17nbd9pqjv9vl9kzrq-luogu-gcc-11.2.0/include/c++/11.2.0/ios:42, from /nix/store/fdbr19mgwzmp1f17nbd9pqjv9vl9kzrq-luogu-gcc-11.2.0/include/c++/11.2.0/istream:38, from /nix/store/fdbr19mgwzmp1f17nbd9pqjv9vl9kzrq-luogu-gcc-11.2.0/include/c++/11.2.0/sstream:38, from /nix/store/fdbr19mgwzmp1f17nbd9pqjv9vl9kzrq-luogu-gcc-11.2.0/include/c++/11.2.0/complex:45, from /nix/store/fdbr19mgwzmp1f17nbd9pqjv9vl9kzrq-luogu-gcc-11.2.0/include/c++/11.2.0/ccomplex:39, from /nix/store/fdbr19mgwzmp1f17nbd9pqjv9vl9kzrq-luogu-gcc-11.2.0/include/c++/11.2.0/x86_64-unknown-linux-gnu/bits/stdc++.h:54, from /tmp/compiler_n127hl5k/src:1: /nix/store/fdbr19mgwzmp1f17nbd9pqjv9vl9kzrq-luogu-gcc-11.2.0/include/c++/11.2.0/bits/basic_string.h:6618:3: 附注:candidate: ‘int std::__cxx11::stoi(const string&, std::size_t*, int)’ 6618 | stoi(const string& __str, size_t* __idx = 0, int __base = 10) | ^~~~

2022/1/3 16:09
加载中...