1. 程式人生 > >【PAT】1072 開學寄語(20 分)

【PAT】1072 開學寄語(20 分)

下圖是上海某校的新學期開學寄語:天將降大任於斯人也,必先刪其微博,卸其 QQ,封其電腦,奪其手機,收其 ipad,斷其 wifi,使其百無聊賴,然後,淨面、理髮、整衣,然後思過、讀書、鍛鍊、明智、開悟、精進。而後必成大器也!

jiyu.JPG

本題要求你寫個程式幫助這所學校的老師檢查所有學生的物品,以助其成大器。

輸入格式:

輸入第一行給出兩個正整數 N(≤ 1000)和 M(≤ 6),分別是學生人數和需要被查繳的物品種類數。第二行給出 M 個需要被查繳的物品編號,其中編號為 4 位數字。隨後 N 行,每行給出一位學生的姓名縮寫(由 1-4 個大寫英文字母組成)、個人物品數量 K(0 ≤ K ≤ 10)、以及 K 個物品的編號。

輸出格式:

順次檢查每個學生攜帶的物品,如果有需要被查繳的物品存在,則按以下格式輸出該生的資訊和其需要被查繳的物品的資訊(注意行末不得有多餘空格):

姓名縮寫: 物品編號1 物品編號2 ……

最後一行輸出存在問題的學生的總人數和被查繳物品的總數。

輸入樣例:

4 2
2333 6666
CYLL 3 1234 2345 3456
U 4 9966 6666 8888 6666
GG 2 2333 7777
JJ 3 0012 6666 2333

輸出樣例:

U: 6666 6666
GG: 2333
JJ: 6666 2333
3 5

用時30:54

哈子噶西

雖然有個warning但還是被本小機靈鬼水過去了

禁忌の三重迴圈

沒有什麼坑,輸出格式注意一下吧,over 

#include <iostream>
#include <string>
#include <algorithm> 
using namespace std;
struct Stu{
	string name;
	int wu;
	string c[11];
	bool flag = false;
};
Stu student[1005];
int n,m;
string limit[10];

int main(){
	cin>>n>>m;
	for(int i = 0; i < m; i++){
		cin>>limit[i];
	}
//	for(int i = 0; i < m; i++){
//		cout<<limit[i]<<" ";
//	}cout<<endl;
	
	for(int i = 0; i < n; i++){
		cin>>student[i].name>>student[i].wu;
		for(int j = 0; j < student[i].wu; j++){
			cin>>student[i].c[j];
		}
	}
//	for(int i = 0; i < n; i++){
//		cout<<student[i].name<<" "<<student[i].wu;
//		for(int j = 0; j < student[i].wu; j++){
//			cout<<student[i].c[j]<<" ";
//		}
//		cout<<endl;
//	}
	int wjwp = 0,wjr = 0;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < student[i].wu; j++){
			for(int k = 0; k < m; k++){
				if(limit[k] == student[i].c[j]){
					wjwp++;
					student[i].flag = true;
				}
			}
		}
		if(student[i].flag){
			wjr++;
		}
	}
	for(int i = 0; i < n; i++){
		if(student[i].flag){
			int yyy = 0;
			cout<<student[i].name<<": ";
			for(int j = 0; j < student[i].wu; j++){
				for(int k = 0; k < m; k++){
					if(limit[k] == student[i].c[j] && yyy == 0){
						cout<<limit[k];
						yyy++;
					}else if(limit[k] == student[i].c[j] && yyy != 0){
						cout<<" "<<limit[k];
					}
				}
			}cout<<endl;
		}
	}
	cout<<wjr<<" "<<wjwp<<endl;
	
	return 0;
}