Intermediate question
Description
Enter 10 numbers in ascending order of digits and digits. If the numbers are the same, they are sorted in ascending order.
Input Description: 10 positive integers, which must be within the int range and separated by spaces.
Output Description: 10 numbers. The values are separated by spaces. The last number is not followed by spaces.
Input example: 11 3 2 4 5 9 8 7 10 6
Output example: 10 2 11 3 4 5 6 7 8 9
Solution: Call the sort function that comes with C ++ and rewrite the Compare function.
#include<string>#include<algorithm>#include<sstream>#include<iostream>#include<vector>using namespace std;bool cmp(string &s1,string &s2){ unsigned len1; len1 = s1.size(); unsigned len2 ; len2 = s2.size(); unsigned res1=0,res2=0; for(unsigned i = 0;i<len1;i++) { res1 += (s1[i]-‘0‘); } for(unsigned j=0;j<len2;j++) { res2 +=(s2[j]-‘0‘); } if(res1==res2) { istringstream is1(s1),is2(s2); unsigned d1,d2; is1>>d1; is2>>d2; return (d1<d2); } return (res1<res2); }void main(){ vector<string> ves(10,""); for(int i=0;i<10;i++) cin>>ves[i]; sort(ves.begin(),ves.end(),cmp); for(int i=0;i<10;i++) {
cout<<ves[i];
if(i!=9)
cout<<" ";
}
cout<<endl; }