編譯後50分
/*
系統呼叫了 abort 函式! terminate called after throwing an instance of 'St9bad_alloc' what(): std::bad_alloc Aborted (core dumped)
*/
//跑出以上文字
//以下是我程式碼 ,請大大幫忙看看謝謝!
#include<bits/stdc++.h>
#define ED end()
#define BG begin()
#define PB push_back
#define MP make_pair
#define ll long long
#define pii pair<int,int>
#define Size size()
//#define test_
using namespace std;
const int MAXN = 1010;
int main(){
int times; cin>>times;
map< string, map<string,int> > mp;
string animal,place;
int num;
string setp[1010];
string seta[1010];
int m=0,n=0;
for(int i=0;i<times;i++){
cin>>animal>>num>>place;
if(mp[place][animal]) mp[place][animal]+=num;
else mp[place][animal]=num;
bool chk=false;
for(int j=0;j<m;j++) if(setp[j]==place) chk=true;
if(!chk) setp[m]=place,m++;
chk=false;
for(int j=0;j<n;j++) if(seta[j]==animal) chk=true;
if(!chk) seta[n]=animal,n++;
chk=false;
}
for(int i=0;i<m;i++){
cout<<setp[i]<<" : ";
int cnt=0;
for(int j=0;j<n;j++){
if(mp[setp[i]][seta[j]]>0){
if(cnt>0) cout<<", ";
cout<<seta[j]<<" "<<mp[setp[i]][seta[j]];
cnt++;
}
}
cout<<'\n';
}
}
編譯後50分
/*
系統呼叫了 abort 函式! terminate called after throwing an instance of 'St9bad_alloc' what(): std::bad_alloc Aborted (core dumped)
*/
//跑出以上文字
//以下是我程式碼 ,請大大幫忙看看謝謝!
#include<bits/stdc++.h>
#define ED end()
#define BG begin()
#define PB push_back
#define MP make_pair
#define ll long long
#define pii pair<int,int>
#define Size size()
//#define test_
using namespace std;
const int MAXN = 1010;
int main(){
int times; cin>>times;
map< string, map<string,int> > mp;
string animal,place;
int num;
string setp[1010];
string seta[1010];
int m=0,n=0;
for(int i=0;i<times;i++){
cin>>animal>>num>>place;
if(mp[place][animal]) mp[place][animal]+=num;
else mp[place][animal]=num;
bool chk=false;
for(int j=0;j<m;j++) if(setp[j]==place) chk=true;
if(!chk) setp[m]=place,m++;
chk=false;
for(int j=0;j<n;j++) if(seta[j]==animal) chk=true;
if(!chk) seta[n]=animal,n++;
chk=false;
}
for(int i=0;i<m;i++){
cout<<setp[i]<<" : ";
int cnt=0;
for(int j=0;j<n;j++){
if(mp[setp[i]][seta[j]]>0){
if(cnt>0) cout<<", ";
cout<<seta[j]<<" "<<mp[setp[i]][seta[j]];
cnt++;
}
}
cout<<'\n';
}
}
最好先用 find 來檢查該 key 有沒有存在
不然會吃 St9bad_alloc 錯誤