//1072 开学寄语v1
#include <iostream>
#include <map>
using namespace std;
int main(){
int N,M,i,j,t,tem,stucnt=0,goods=0,n; cin>>N>>M;
int check[M];
string name;
map<string,int> mp;
for(i=0;i<M;i++) cin>>check[i];
for(i=0;i<N;i++){ //N个学生
int k=1;
cin>>name>>n;
for(j=0;j<n;j++){
cin>> tem;
for(t=0;t<M;t++){
if(tem==check[t]){
goods++; //存在一件,要被计数
if(k==1){ //只输出一遍姓名
k--;
cout<<name<<":";
}
if(k==0) cout<<" "<<tem; //每次输出物品编号前有个空格
if(mp[name]==0){
mp[name]++;
stucnt++;
}
}
}
if(j==n-1) cout<<endl;
}
}
cout<<stucnt<<" "<<goods<<endl;
return 0;
}
2个错误
1:由于物品为4位的数字,而你用int接受之后,会舍弃前面的0,要么你改用string,要么用iomanip里的函数或者printf控制输出
2,换行的问题,如果某个学生没有违禁物品,就不用输出他的信息(k仍为1);
#include <iostream>
#include<string>
#include<iomanip>
#include <map>
using namespace std;
int main() {
int N, M, i, j, t, tem, stucnt = 0, goods = 0, n; cin >> N >> M;
int check[8];
string name;
map<string, int> mp;
for (i = 0; i < M; i++)
cin >> check[i];
for (i = 0; i < N; i++) { //N个学生
int k = 1;
cin >> name >> n;
for (j = 0; j < n; j++) {
cin >> tem;
for (t = 0; t < M; t++) {
if (tem == check[t]) {
goods++;
if (k == 1) {
k--;
cout << name << ":";
}
if (k == 0)
cout << " " << setw(4) << setfill('0') << tem; //ERROR 1
if (mp[name] == 0) {
mp[name]++;
stucnt++;
}
}
}
if (j == n - 1 && k == 0) //ERROR 2
cout << endl;
}
}
cout << stucnt << " " << goods << endl;
return 0;
}
https://blog.csdn.net/oShuaiFeng/article/details/80714131
推荐一个好用的C/C++在线编译器:https://www.winfengtech.com/compile.htm