站外题求助
查看原帖
站外题求助
282016
I_Love_Potter楼主2022/6/1 20:54

仓库清仓大甩卖, 给你一些货物出仓的记录, 你需要统计每种货物出单了多少次. 每种货物用一个字符串表示, 你需要按照货物的字典序输出你的统计结果.

第一行输入一个整数n (1≤n≤100000)

接下来nn 行每行输入一个字符串与一个整数

S cnt

表示货物S卖出了cnt件

其中S由小写字母构成, 长度不超过10

1≤cnt≤100

第一行输出一个整数m表示卖出货物的种类数

接下来m行按照字典序每行输出一个字符串与一个整数表示某种货物一共卖出的数量

我的代码

#include<bits/stdc++.h>
using namespace std;
set<string> st;
map<string,int> mp;
int main(){
    //freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	std::ios::sync_with_stdio(false);
	int n;
	cin>>n;
	while(n--){
		string s;
		int x;
		cin>>s;
		cin>>x;
		mp[s]+=x;
		st.insert(s);
	}
	cout<<st.size()<<endl; 
	for(set<string>::iterator it=st.begin();it!=st.end();it++){
		cout<<*it<<" "<<mp[*it]<<endl;
		//cout<<*it;
		//printf(" %d\n",mp[*it]);
	}
	return 0;
}
2022/6/1 20:54
加载中...