pat-1153

#include<iostream>
#include<vector>
#include<unordered_map>
#include<algorithm>
#include<string>
using namespace std;
struct node{
	int val;
	string str;
};
bool cmp( node&a, node&b){
	return a.val==b.val?  a.str<b.str:  a.val>b.val;
}
vector<node> infor;
int main(){
	int n,m,tempv,type;
	string tempt,tstr;
	cin>>n>>m;
	for(int i=0;i<n;i++){
    cin>>tempt>>tempv;
		infor.push_back({tempv,tempt});
	}
	for(int i=1;i<=m;i++){
		cin>>type>>tstr;
		printf("Case %d: %d %s\n",i,type,tstr.c_str());//不能通过‘...’传递有不能平凡复制的类型‘输出是会出问题的,因为“%s”要求后面的对象的首地址,但是string不是这样的一个类型。 
		vector<node> ans;//c_str()注意首地址 
		unordered_map<string,int> t3;
		int cnt=0,sum=0;//条件重置 
		if(type==1){
			for(int j=0;j<n;j++){
				if(tstr[0]==infor[j].str[0]){//这写错了(注意) 
				ans.push_back(infor[j]);	
				}
			}
		}
		else if(type==2){
			for(int j=0;j<n;j++){
			
			if(tstr==infor[j].str.substr(1,3)){//下标从1开始3个字符 
		    cnt++;
		    sum+=infor[j].val;
		    }
			}
		    if(cnt!=0)
			printf("%d %d\n",cnt,sum);
		}
		else if(type==3){
			for(int j=0;j<n;j++){
				if(tstr==infor[j].str.substr(4,6)){
					ans.push_back(infor[j]);
				}
			}
		}
		//type 2也有可能没有
		if(cnt==0&&type==2){
			printf("NA\n");
		} 
		else if(type!=2&&ans.size()==0){
			printf("NA\n");
		}
		else if(type==1){
			sort(ans.begin(),ans.end(),cmp);
			for(int i=0;i<ans.size();i++){
				printf("%s %d\n",ans[i].str.c_str(),ans[i].val);
			}
		}
		else  if(type==3){
			//sort(ans.begin(),ans.end(),cmp);
			for(int i=0;i<ans.size();i++){
				t3[ans[i].str.substr(1,3)]++;
				
			}
			ans.clear();
			for(auto it=t3.begin();it!=t3.end();it++){
			ans.push_back({it->second,it->first });
			}
			sort(ans.begin(),ans.end(),cmp);
			for(int i=0;i<ans.size();i++){
			printf("%s %d\n",ans[i].str.c_str(),ans[i].val);
			}
		}
	} 
	return 0;
}

总结

1.变量名字抄错,每一部分需要什么变量一定要想清楚

2. 能通过‘...’传递有不能平凡复制的类型‘输出是会出问题的,因为“%s”要求后面的对象的首地址,但是string不是这样的一个类型。 ,要输出%s,  要使用....c_str()

3.map函数遍历    for(auto it=t3.begin();it!=t3.end();it++){
            ans.push_back({it->second,it->first });
            }

英语

 

问题  如何减少输错变量的错误   应该更具体简洁

扫描二维码关注公众号,回复: 12379243 查看本文章

猜你喜欢

转载自blog.csdn.net/m0_45359314/article/details/112985500
今日推荐