注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
刚做完test7,三题:
- investment SQL。地里和 stack overflow 都有答案
- twitter recommend
- time serist。都过了。鉴于有人第三题有test没过,我把代码粘一下。大家相互交流哈,做的时候别直接copy&paste就好啦。
- using namespace std;
- map<string, map<string, int>, greater<string> > records;
- string startMonth, endMonth;
- string date, type;
- int num;
- string removeComma(string str){
- return str.substr(0, str.length()-1);
- }
- string getMonth(string date) {
- return date.substr(0, date.length()-3);
- }
- void addRecord(string date, string type, int num) {
- string month = getMonth(date);
- records[month][type] += num;
- }
- void searchRange(){
- map<string, map<string, int> >::iterator startIter, endIter;
- startIter = records.upper_bound(endMonth);
- endIter = records.upper_bound(startMonth);
- for(auto i = startIter; i != endIter; i++) {
- cout << i->first;
- for(auto j = i->second.begin(); j != i->second.end(); j++) {
- cout << ", " << j->first << ", " << j->second;
- }
- cout << endl;
- }
- }
- int main() {
- cin>>startMonth>>endMonth;
- startMonth = removeComma(startMonth);
- while(cin>>date) {
- date = removeComma(date);
- cin>>type;
- type = removeComma(type);
- cin>>num;
- addRecord(date, type, num);
- }
- searchRange();
- return 0;
- }
复制代码
|