标签:out include begin iostream name auto names print pac
这道题很容易理解。我第一遍做的时候借助了map,没想到第二遍还是一样,哈哈哈。
但是map用的更娴熟了,开心。
#include<iostream> #include<map> using namespace std; int main() { int n,t,cnt = 0; cin>>n; map<int,int> mp; while(n--) { scanf("%d",&t); int sum = 0; while(t != 0) { sum += t%10; t /= 10; } mp[sum]++; } cout<<mp.size()<<endl; for(auto it = mp.begin(); it != mp.end(); ++it) { if(it != mp.begin()) printf(" "); cout<<it->first; } return 0; }
另外,这题应该借助 set(自排序+去重)更好。
#include<iostream> #include<set> using namespace std; int main() { int n,t,cnt = 0; cin>>n; set<int> st; while(n--) { scanf("%d",&t); int sum = 0; while(t != 0) { sum += t%10; t /= 10; } st.insert(sum); } cout<<st.size()<<endl; for(auto it = st.begin(); it != st.end(); ++it) { if(it != st.begin()) printf(" "); cout<<*it; } return 0; }
标签:out include begin iostream name auto names print pac
原文地址:https://www.cnblogs.com/keep23456/p/12354360.html