#先排序,因为对珠子个数还有要求,所以将每种珠子进行计数,对应种类的珠子个数相减就可以得到买方卖方相差的数目啦。
#include <iostream> #include <algorithm> #include <string.h> using namespace std; int cmp(const void *a,const void *b) { return *((char*)a)-*((char*)b); } int main() { char sell[1001]={0}; char buy[1001]={0}; int count_sell[123]={0}; int count_buy[123]={0}; cin>>sell; cin>>buy; qsort(sell,strlen(sell),sizeof(char),cmp); qsort(buy,strlen(buy),sizeof(char),cmp); for(int i=0;sell[i]!='\0';i++) { count_sell[sell[i]-48]+=1; } for(int i=0;buy[i]!='\0';i++) { count_buy[buy[i]-48]+=1; } int lack=0,over=0; //0~9 for(int i=0;i<10;i++) { if(count_buy[i]<count_sell[i]) { over+=count_sell[i]-count_buy[i]; } else lack+=count_buy[i]-count_sell[i]; } //A~Z for(int i=17;i<43;i++) { if(count_buy[i]<count_sell[i]) over+=count_sell[i]-count_buy[i]; else lack+=count_buy[i]-count_sell[i]; } //a~z for(int i=49;i<75;i++) { if(count_buy[i]<count_sell[i]) over+=count_sell[i]-count_buy[i]; else lack+=count_buy[i]-count_sell[i]; } if(lack>0) cout<<"No "<<lack; else cout<<"Yes "<<over; return 0; }