标签:short out stream incr 思路 find its fine use
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6348 Accepted Submission(s): 2215
int rank = 1; map<string,int>::iterator it; for(it = shop.begin();it != shop.end(); it++){ if(it->second>shop["memory"]) rank++; } cout<<rank<<endl;
所以我经过查阅资料发现了另一种方法,用一个数组来把价格存起来,然后用sort排序后,再通过比较数组中的值是否和“memory”商店的价格相等来输出他的位置,即为他的排名
#include <iostream> #include <string> #include <map> #include <algorithm> using namespace std; bool my_camp(int x,int y){ return x>y; } int main() { int n,m,p; int a[10001]; map<string,int>shop; while(cin>>n){ string s; for(int i = 1;i <= n; i++) cin>>s; cin>>m; while(m--){ for(int i = 1;i <= n; i++){ cin>>p>>s; shop[s] += p; a[i] = shop[s]; } /*int rank = 1; map<string,int>::iterator it; for(it = shop.begin();it != shop.end(); it++){ if(it->second>shop["memory"]) rank++; } cout<<rank<<endl; */ sort(a+1,a+n+1,my_camp); for(int i = 1;i <= n; i++){ if(a[i] == shop["memory"]){ cout<<i<<endl; break; } } } shop.clear(); } return 0; }
标签:short out stream incr 思路 find its fine use
原文地址:https://www.cnblogs.com/jingshixin/p/12241794.html