PAT _(刷题3) 1130 1131 1132 1133 1134 1137 1138 1139 1140 1141 1142 1144 1145 1146 1148 1149 1150

目录

1130 Infix Expression (25分) (dfs) 

1131 Subway Map (30分) (dfs)(新 ※)

1132 Cut Integer (20分) (水题)

 1133 Splitting A Linked List (25分) (链表 cmp sort)待改 22

1134 Vertex Cover (25分) (水题 unordered_set st;)

 

1137 Final Grading (25分) (map,sort,cmp)待改 23 

1138 Postorder Traversal (25分) (已知前序+中序 求后序)

1139 First Contact (30分) (水题)

 

1140 Look-and-say Sequence (20分)

1141 PAT Ranking of Institutions (25分) (结构体 sort)  待改 22

1142 Maximal Clique (25分) (无向图, (最大)完全子图)

1144 The Missing Number (20分) (E 水题)

1145 Hashing - Average Search Time (25分) (平方探测 hash)

1146 Topological Order (25分) (拓扑序)

1148 Werewolf - Simple Version (20分)

1149 Dangerous Goods Packaging (25分)(map > ),vector

1150 Travelling Salesman Problem (25分) (旅行商问题=哈密顿回路)


1130 Infix Expression (25分) (dfs) 

 

#include
#include
#include
#include
#include
#include
#include
#define pb push_back 
using namespace std;
const int maxn=100005;
int n,cnt[25];
struct node{
	string data;
	int left,right;
	node(){
		left=-1;right=-1; 
	}
};
vector g(25);
vector ans;
string dfs(int u){
	if(g[u].left==-1 && g[u].right==-1) return g[u].data;
	else if(g[u].left==-1 && g[u].right!=-1) return "("+g[u].data+dfs(g[u].right)+")";
	else if(g[u].left!=-1 && g[u].right==-1) return "("+dfs(g[u].left)+g[u].data+")";
	else if(g[u].left!=-1 && g[u].right!=-1) return "("+dfs(g[u].left)+g[u].data+dfs(g[u].right)+")";
}
int main(){
	cin>>n;
	string c; int a,b;
	for(int i=1;i<=n;i++){
		cin>>c>>a>>b;
		g[i].data=c; g[i].left=a; g[i].right=b;
		cnt[a]=1; cnt[b]=1; 
	}
	int i=1;
	for(;i<=n;i++) 
	 	if(cnt[i]==0) break;
	int root=i;
	string ans=dfs(root);
	if(ans[0]=='(') ans=ans.substr(1,ans.size()-2);
	cout<

1131 Subway Map (30分) (dfs)

题⽬⼤意:找出⼀条路线,使得对任何给定的起点和终点,可以找出中途经停站最少的路线;如果经停站⼀样多,则取需要换乘线路次数最少的路线
 
 
 
 

 

1132 Cut Integer (20分) (水题)

#include
#include
#include
#include
#include
#include
#include
#define pb push_back 
#define ll long long 
using namespace std;
const int maxn=100005;
int n;
ll getnum(ll tmp){
	//位数
	ll cnt=0;
	while(tmp!=0){
		tmp/=10;
		cnt++;	
	}
	return cnt;	
}
ll power(ll a,ll b){
	ll tmp=1;
	while(b--){
		tmp*=a;
	}
	return tmp;
}
int main(){
	cin>>n;
	ll num,w,a,b,p1;
	for(int i=0;i>num;
		w=getnum(num);
		p1=power(10,w/2);
		a=num%p1; b=num/p1; 
		if((a*b)!=0&&num%(a*b)==0)cout<<"Yes\n";
		else cout<<"No\n"; 	
	}
	return 0;	
}

 1133 Splitting A Linked List (25分) (链表 cmp sort)

#include
#include
#include
#include
#include
#include
#include
#define pb push_back 
using namespace std;
const int maxn=100005;
int start,n,k;
struct node{
	int data;
	int flag,index;
	int addr,next; 
	node(){
		flag=maxn;
		index=maxn;
	}
}num[maxn];
bool cmp(node a,node b){
	if(a.flag!=b.flag)
		return a.flagk){
			num[add].flag=1;
		}else{
			num[add].flag=0;
		}
	}
	int in=0;
	while(start!=-1){
		num[start].index=in++;
		start=num[start].next;
	}
	sort(num,num+maxn,cmp);
	for(int i=0;i

 

1134 Vertex Cover (25分) (水题 unordered_set st;)

#include
#include
#include
#include
#include
#include
#include
#define pb push_back
#define pii pair
using namespace std;
const int maxn=100005;
int n,m,k;
unordered_set st;
vector g(10005);
int main(){
	cin>>n>>m;
	for(int i=0;i>g[i].first>>g[i].second;
	}
	int num,tmp,flag=0;
	cin>>k;
	while(k--){
		cin>>num;
		flag=0;
		st.clear();
		while(num--){
			cin>>tmp;
			st.insert(tmp);
		}
		for(int i=0;i

 

 

1137 Final Grading (25分) (map,sort,cmp)

 he/she must first obtain no less than 200 points from the online programming assignments, and then receive a final grade no less than 60 out of 100. 

注意: rounded up to an integer 四舍五入为整数

#include
#include
#include
#include
#include
#include
#include
#include
#define pb push_back 
using namespace std;
const int maxn=100005;
int p,m,n,cnt;
struct node{
	string id;
	int prog,mid,final,score;//最终成绩 
	node(){
		prog=mid=final=score=-1;
	}
}num[100005];
map mp; //id-index 
bool cmp(node a,node b){
	if(a.score!=b.score){
		return a.score>b.score;
	}else return a.id>str>>sc;
		num[i].id=str; num[i].prog=sc;
		mp[str]=cnt++;
	}
	for(int i=0;i>str>>sc;
		if(mp[str]==0) {
			mp[str]=cnt;
			num[cnt].id=str; num[cnt].mid=sc; cnt++;
		}else {
			ind=mp[str];
			num[ind].id=str; num[ind].mid=sc;	
		}
		
	}
	for(int i=0;i>str>>sc;
		if(mp[str]==0) {
			mp[str]=cnt; 
			num[cnt].id=str; num[cnt].final=sc; cnt++;
		}else {
			ind=mp[str]; 
			num[ind].id=str; num[ind].final=sc;	
		}
	}
	for(int i=0;inum[i].final){
			num[i].score=0.4*num[i].mid+0.6*num[i].final+0.5; 
		}else{
			num[i].score=num[i].final; 
		}
		if(num[i].prog<200) num[i].score=0; 
	}
	sort(num,num+cnt,cmp); 
	for(int i=0;i=60 && num[i].score<=100 ;i++){
		cout<

 

1138 Postorder Traversal (25分) (已知前序+中序 求后序)

vector g; g.resize(n);   

给定n<50000  ,实际开50 0005数组   

#include
#include
#include
#include
#include
#include
#include
#define pb push_back 
using namespace std;
const int maxn=500005;
int n,pre[maxn],in[maxn],pos[maxn]; 
vector post;
bool flag;
void dfs(int prel,int prer,int inl,int inr){
	if(inl>inr || flag) return ;
	
	int head=pos[pre[prel]];
	int len=head-inl; 
	dfs(prel+1,prel+len,inl,head-1);
	dfs(prel+len+1,prer,head+1,inr); 
	if(!flag){
		flag=true;
		cout<

1139 First Contact (30分) (水题)

 

 

 

 

1140 Look-and-say Sequence (20分)

#include 
#include 
#include 
using namespace std;
int main() {
 	string s;
 	int n, j;
 	cin >> s >> n;
 	for (int cnt = 1; cnt < n; cnt++) {
 		string t;
 		for (int i = 0; i < s.length(); i = j) {
 			for (j = i; j < s.length() && s[j] == s[i]; j++);
 			t += s[i] + to_string(j - i);
 		}
 		s = t;
 	}
 	cout << s;
 	return 0; 
}

1141 PAT Ranking of Institutions (25分) (结构体 sort) 

22

#include
#include
#include
#include
#include
#include
#include 
#include
#define pb push_back 
using namespace std;
const int maxn=100005;
map mp; 
int n,cnt;
struct node{
	string sch;
	int totalA,totalB,totalT,total; //参加pat甲已顶级的所有人分数之和 
	int num;
	node(){
		totalA=totalB=totalT=total=num=0; 
	}
};

bool cmp(node a,node b){
	if(a.total!=b.total) 
		return a.total>b.total;
	else if(a.total==b.total && a.num!=b.num)
		 return a.num='A' && str[i]<='Z'){
			str[i]=str[i]+32;
		}
	}
	return str; 
}
int main(){
	scanf("%d",&n);
	vector g(n); 
	string stu,str;
	int sc,index;
	for(int i=0;i>stu>>sc>>str;
		str=getsimple(str); //转为小写 
		if(mp.find(str)==mp.end()){
			mp[str]=cnt;cnt++;
		}
		index=mp[str];
		g[index].sch=str;
		if(stu[0]=='A'){
			g[index].totalA +=sc;
		}else if(stu[0]=='B'){
			g[index].totalB +=sc;
		}else if(stu[0]='T'){
			g[index].totalT +=sc;
		}
		g[index].num++;  
	}
	for(int i=0;i0 && g[i].total!=g[i-1].total)rank=i+1; 
		cout<

1142 Maximal Clique (25分) (无向图, (最大)完全子图)

分析:先判断是否是clique,即判断是否任意两边都相连;之后判断是否是maximal即遍历所有不在集合中的剩余的点,看是否存在⼀个点满⾜和集合中所有的结点相连,最后如果都满⾜,那就输出Yes表示是Maximal clique

 

#include 
#include 
using namespace std;
int e[210][210];
int main() {
     int nv, ne, m, ta, tb, k;
     scanf("%d %d", &nv, &ne);
     for (int i = 0; i < ne; i++) {
         scanf("%d %d", &ta, &tb);
         e[ta][tb] = e[tb][ta] = 1;
     }
     scanf("%d", &m);
     for (int i = 0; i < m; i++) {
         scanf("%d", &k);
         vector v(k);
         int hash[210] = {0}, isclique = 1, isMaximal = 1;
         for (int j = 0; j < k; j++) {
             scanf("%d", &v[j]);
             hash[v[j]] = 1;
         }
         for (int j = 0; j < k; j++) {
             if (isclique == 0) break;
             for (int l = j + 1; l < k; l++) {
                 if (e[v[j]][v[l]] == 0) {
                     isclique = 0;
                     printf("Not a Clique\n");
                     break;
                 }
             }
         }
         if (isclique == 0) continue;
         for (int j = 1; j <= nv; j++) {
             if (hash[j] == 0) {
                 for (int l = 0; l < k; l++) {
                     if (e[v[l]][j] == 0) break;
                     if (l == k - 1) isMaximal = 0;
                 }
             }
             if (isMaximal == 0) {
                 printf("Not Maximal\n");
                 break;
             }
         }
         if (isMaximal == 1) printf("Yes\n");
     }
     return 0; 
}

 

1144 The Missing Number (20分) (E 水题)

#include
#include
#include
#include
#include
#include
#include
#define pb push_back 
using namespace std;
const int maxn=100005;
int n;
int Hash[maxn];

int main(){
	scanf("%d",&n);
	int tmp;
	for(int i=0;i=0 && tmp

1145 Hashing - Average Search Time (25分) (平方探测 hash

类似: 1078    Quadratic probing:平方探测

#include 
#include 
#include 
using namespace std;
bool isprime(int n) {
 	for (int i = 2; i * i <= n; i++)
 	if (n % i == 0) return false;
 	return true; 
}
int main() {
 	int tsize, n, m, a;
 	scanf("%d %d %d", &tsize, &n, &m);
 	while(!isprime(tsize)) tsize++;
 	vector v(tsize);

 	for (int i = 0; i < n; i++) {
 		scanf("%d", &a);
 		int flag = 0;
 		for (int j = 0; j < tsize; j++) {
 			int pos = (a + j * j) % tsize;
 				if (v[pos] == 0) {
 					v[pos] = a;
 					flag = 1;
 					break;
 				}
 		}
 		if (!flag) printf("%d cannot be inserted.\n", a);
 	}
 	int ans = 0;
 	for (int i = 0; i < m; i++) {
 		scanf("%d", &a);
 		for (int j = 0; j <= tsize; j++) {
 			ans++;
 			int pos = (a + j * j) % tsize;
 			if (v[pos] == a || v[pos] == 0) break;
 		}
 	}
 	printf("%.1f\n", ans * 1.0 / m);
 	return 0; 
}

 

1146 Topological Order (25分) (拓扑序)

给定一个DAG,和几个序列,判断这些序列是否是DAG的拓扑序

(注:单纯求拓扑序可以用dfs,或是bfs(每次让度=0的点入队))

分析:⽤邻接表v存储这个有向图,并将每个节点的⼊度保存在in数组中。
对每⼀个要判断是否是拓扑序列的结点遍历,如果当前结点的⼊度不为0则表示不是拓扑序列,每次选中某个点后要将它所指向的所有结点的⼊度-1,最后根据是否出现过⼊度不为0的点决定是否要输出当前的编号i
flag是⽤来判断之前是否输出过现在是否要输出空格的~judge是⽤来判断是否是拓扑序列的

 

#include 
#include 
using namespace std;
int main() {
     int n, m, a, b, k, flag = 0, in[1010];
     vector v[1010];
     scanf("%d %d", &n, &m);
     for (int i = 0; i < m; i++) {
         scanf("%d %d", &a, &b);
         v[a].push_back(b);
         in[b]++;
     }
     scanf("%d", &k);
     for (int i = 0; i < k; i++) {
         int judge = 1;
         vector tin(in, in+n+1);
         for (int j = 0; j < n; j++) {
             scanf("%d", &a);
             if (tin[a] != 0) judge = 0;
             for (int it : v[a]) tin[it]--;
         }
         if (judge == 1) continue;
         printf("%s%d", flag == 1 ? " ": "", i);
         flag = 1;
     }
     return 0; 
}

1148 Werewolf - Simple Version (20分)

分析:每个⼈说的数字保存在v数组中,i1nji+1n遍历,分别假设ij是狼⼈,a数组表示该⼈是狼⼈还是好⼈,等于1表示是好⼈,等于-1表示是狼⼈。
k1n分别判断k所说的话是真是假,k说的话和真实情况不同(即v[k] * a[abs(v[k])] < 0)则表示k在说谎,则将k放在lie数组中;遍历完成后 判断lie数组,如果说谎⼈数等于2并且这两个说谎的⼈⼀个是好⼈⼀个是狼⼈(即a[lie[0]] + a[lie[1]] == 0)表示满⾜题意,此时输出ijreturn,否则最后的时候输出No Solution
 
#include 
#include 
#include 
using namespace std;
int main() {
    int n;
    cin >> n;
    vector v(n+1);
    for (int i = 1; i <= n; i++) cin >> v[i];
    for (int i = 1; i <= n; i++) {
        for (int j = i + 1; j <= n; j++) {
            vector lie, a(n + 1, 1);
            a[i] = a[j] = -1;
            for (int k = 1; k <= n; k++)
                if (v[k] * a[abs(v[k])] < 0) lie.push_back(k);
                if (lie.size() == 2 && a[lie[0]] + a[lie[1]] == 0) {
                    cout << i << " " << j;
                    return 0;
                }
         }
    }
    cout << "No Solution";
    return 0; 
}

 

1149 Dangerous Goods Packaging (25分)(map> )

#include
#include
#include
#include
using namespace std;
int main() {
 	int n, k, t1, t2;
 	map> m;
 	scanf("%d%d", &n, &k);
 	for (int i = 0; i < n; i++) {
 		scanf("%d%d", &t1, &t2);
 		m[t1].push_back(t2);
 		m[t2].push_back(t1);
 	}
 	while (k--) {
 		int cnt, flag = 0, a[100000] = {0};
 		scanf("%d", &cnt);
 		vector v(cnt);
 		for (int i = 0; i < cnt; i++) {
 			scanf("%d", &v[i]);
 			a[v[i]] = 1;
 		}
 		for (int i = 0; i < v.size(); i++)
 			for (int j = 0; j < m[v[i]].size(); j++)
 				if (a[m[v[i]][j]] == 1) flag = 1;
 					printf("%s\n",flag ? "No" :"Yes");
 	}
 	return 0; 
}

 

1150 Travelling Salesman Problem (25分) (旅行商问题=哈密顿回路)

Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city and returns to the origin city 

跟之前那道哈密顿的题差不多,

#include
#include
#include
#include
#include
#include
#include
#include
#define pb push_back 
using namespace std;
const int maxn=100005;
int n,m,k;
int g[205][205],path[205];
int main(){
	cin>>n>>m;
	int a,b,d;
	for(int i=0;i>a>>b>>d;
		g[a][b]=g[b][a]=d;
	}
	int num,flag,sum,minsum=0x3f3f3f3f,index;
	cin>>k;
	for(int i=1;i<=k;i++){
		set st;
		flag=1;
		sum=0;
		memset(path,0,sizeof(path));
		cin>>num;
		cout<<"Path "<>path[j];
			st.insert(path[j]);
			if(j>0 && g[path[j-1]][path[j]]==0){
				flag=0;
			}else{
				sum+=g[path[j-1]][path[j]];
			}
		}
		
		if( st.size()!=n || path[0]!=path[num-1] || !flag){
			if(flag==1){
				cout<

你可能感兴趣的:(pat)