Skip to content

Commit 76c1edd

Browse files
committed
merge pat
1 parent b060fc3 commit 76c1edd

File tree

297 files changed

+24229
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

297 files changed

+24229
-0
lines changed

pat/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#PAT

pat/c/0001.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <stdio.h>
2+
int main() {
3+
int a,b;
4+
while(~scanf("%d %d",&a,&b)) //¶à´ÎÊäÈëaºÍb¡£
5+
{
6+
printf("%d\n",a+b);
7+
}
8+
}

pat/c/1002.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <stdio.h>
2+
#include <algorithm>
3+
using namespace std;
4+
//1.double数据用lf
5+
//2.这里需要注意没有最后一个数据后面没有多余的空格!!需要考虑的很全面
6+
//比如说,我们需要确定哪一个是最后一个数据;以及当多项式全为0时该怎么输出!
7+
8+
typedef struct {
9+
int exponent;
10+
double coeffcient;
11+
}Ploy;
12+
13+
14+
bool cmp(Ploy a,Ploy b){
15+
return a.exponent>b.exponent;
16+
}
17+
18+
int main(){
19+
Ploy p1[20],p2[20];
20+
int A,B;
21+
scanf("%d",&A);
22+
int i = 0 ,j = 0;
23+
int k = 0,l = 0;//用来遍历p1和p2
24+
25+
for(i = 0;i<A;i++){
26+
scanf("%d %lf",&p1[i].exponent,&p1[i].coeffcient);
27+
}
28+
29+
scanf("%d",&B);
30+
for(j = 0;j<B;j++){
31+
scanf("%d %lf",&p2[j].exponent,&p2[j].coeffcient);
32+
}
33+
34+
int lengthP1 = i,lenghtP2 = j;//保存两个的原长
35+
36+
while(l<lenghtP2){
37+
for(k = 0;k<lengthP1;k++){
38+
if(p1[k].exponent == p2[l].exponent){
39+
p1[k].coeffcient +=p2[l].coeffcient;
40+
break;//并且跳出循环
41+
}
42+
}
43+
44+
if(k == lengthP1){//说明没有找到相同项
45+
p1[lengthP1++] = p2[l];
46+
}
47+
l++;
48+
}
49+
50+
sort(p1,p1+lengthP1,cmp);
51+
52+
int count = 0;
53+
for(i = 0;i<lengthP1;i++){
54+
if(p1[i].coeffcient != 0){
55+
count++;
56+
}
57+
}
58+
if(count !=0){
59+
printf("%d ",count);//非零指数项共有count
60+
}
61+
else{
62+
printf("%d",count);//非零指数项共有count
63+
}
64+
65+
66+
67+
//使用count2的目的是为了控制最后一次输出时没有多余的空格
68+
int count2= 0;
69+
for(i = 0;i<lengthP1;i++){
70+
if(p1[i].coeffcient == 0){
71+
continue;
72+
}
73+
else{
74+
if(count2 != count -1){
75+
count2++;
76+
printf("%d %.1lf ",p1[i].exponent,p1[i].coeffcient);
77+
}
78+
else{
79+
printf("%d %.1lf",p1[i].exponent,p1[i].coeffcient);
80+
}
81+
}
82+
}
83+
}
84+
85+
/***
86+
3 2 1.5 1 0.5 0 3
87+
1 100 2.3
88+
4 3 2.4 2 1.5 1 0.5 0 3
89+
90+
91+
2 2 -1.5 1 2.4
92+
2 2 1.5 1 -2.4
93+
**/
94+

pat/c/1003.cpp

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#include<cstdio>
2+
#include<algorithm>
3+
#include<iostream>
4+
#define maxn 1000
5+
const int MAXV=1000;
6+
const int INF=0x3fffffff;
7+
8+
using namespace std;
9+
10+
int N,M,C1,C2;
11+
int G[maxn][maxn];//存储顶点间边的信息
12+
int weight[maxn] ;//各个顶点的权重
13+
14+
//先求最短距离
15+
bool vis[MAXV]={false};//表示已经访问过的节点,初始值都是为false,未访问; 如果为true,则表示已访问
16+
int d[MAXV] ; //dis中保留的是 源点到目标城市的访问距离 ,初始的情况下,距离都是最大值
17+
18+
int pre[MAXV];//到顶点v的前驱节点
19+
int way[MAXV];//令起点s到顶点u的最短路径条数为num[u]
20+
int w[MAXV];//表示到各个顶点的最大顶点权之和
21+
22+
void dijkstra(int s){
23+
fill(d,d+MAXV,INF);//fill函数将整个d数组赋值为INF
24+
fill(way,way+MAXV,0);//将从源点到顶点的路径条数设置为0
25+
26+
d[s] = 0; //起点到自身的距离为0
27+
way[s] = 1;//起点到自身的路径为1
28+
w[s] = weight[s];//初始化自身的值
29+
30+
int i,j;
31+
for(i = 0;i< N; i++){
32+
int u = -1, MIN= INF ;//当前最短路径的这个顶点
33+
for(j = 0;j < N;j++){//第一层 for 循环是用于寻找当前尚未遍历节点中最短的那个路径
34+
//vis[j] == false => 尚未遍历
35+
//最短路径 d[j] < MIN ,然后执行更新操作
36+
if(vis[j] == false && d[j] < MIN){ //更新 MIN 值
37+
u = j;
38+
MIN = d[j];
39+
}
40+
}
41+
42+
if(u == -1) return;
43+
vis[u] = true; //对节点u标记为已访问
44+
45+
//以u为中间节点,对整个未访问的节点执行一次更新操作
46+
for(int v = 0; v < N;v++){
47+
//如果节点v尚未访问
48+
//并且有 u->v 的边
49+
//并且 d[u]+G[u][v] < d[v]
50+
if(vis[v] == false && G[u][v]!=INF ){
51+
if(d[u]+G[u][v] == d[v]) {
52+
way[v] += way[u] ;//说明到v的最短路径有多条
53+
if(w[u] + weight[v] > w[v]) //最短路径相等的情况下,并不更新
54+
//只有在w[u] + weight[v] > w[v] 时才更新
55+
w[v] = w[u] + weight[v];
56+
}
57+
58+
if(d[u]+G[u][v] < d[v]) {
59+
d[v] = d[u] + G[u][v];//优化 d[v] 这个距离
60+
pre[v] = u;//计算前驱节点
61+
way[v] = way[u];
62+
w[v] = w[u] + weight[v];//因为是最短路径,所以必须更新
63+
}
64+
}
65+
}
66+
}
67+
}
68+
69+
int main(){
70+
int s;//源点
71+
int des;//终点
72+
73+
cin >> N >> M >> s >>des;
74+
int i,j;
75+
int ver1,ver2;
76+
int edgeWei;
77+
fill(G[0],G[0]+MAXV*MAXV,INF);//初始化图G
78+
79+
//输入各个顶点的权重
80+
for(i = 0;i< N;i++){
81+
cin >> weight[i];
82+
}
83+
84+
85+
for(i = 0;i< M;i++){
86+
cin >> ver1>> ver2 >> edgeWei;
87+
G[ver1][ver2] = edgeWei;//输入边之间的权重
88+
G[ver2][ver1] = edgeWei;//输入边之间的权重
89+
}
90+
91+
dijkstra(s);
92+
// for(i = 0;i< N;i++){
93+
// cout << d[i] <<" ";
94+
// }cout <<"\n";
95+
96+
// cout<<"===========\n";
97+
// for(i = 0;i < N;i++) {
98+
// cout <<i<<"的前驱节点是:"<< pre[i]<<"\n";
99+
// }
100+
101+
102+
// cout<<"===========\n";
103+
// for(i = 0;i < N;i++) {
104+
// cout <<i<<"的路径有:"<< way[i]<<"\n";
105+
// }
106+
107+
cout << way[des] <<" "<< w[des];
108+
// for(i = 0;i< N;i++){
109+
// cout <<i <<"收集到的人手有:"<< w[i]<<"\n";
110+
// }
111+
}
112+
/*
113+
6 8 0
114+
10 2 1 5 3 4
115+
0 1 2
116+
0 3 4
117+
0 4 4
118+
1 3 2
119+
2 5 1
120+
3 2 2
121+
3 4 3
122+
4 5 3
123+
124+
5 6 2
125+
1 2 1 5 3
126+
0 1 1
127+
0 2 2
128+
0 3 1
129+
1 2 1
130+
2 4 1
131+
3 4 1
132+
*/

pat/c/1004.cpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include <cstdio>
2+
#include <algorithm>
3+
#include <queue>
4+
#include <vector>
5+
#define maxSize 100010
6+
using namespace std;
7+
8+
//建立一个树的结点类型
9+
typedef struct{
10+
int depth;//记录结点所在层次深度
11+
vector<int> child;//孩子结点
12+
}BiTree;
13+
14+
15+
BiTree bt[maxSize];//新建一个BiTree型数组
16+
17+
18+
//DFS遍历,记录每个结点的深度
19+
void DFSBiTree(int index,int deep){
20+
bt[index].depth = deep;//改变所有结点的深度值
21+
22+
//作为返回
23+
if(bt[index].child.size() == 0){//如果是叶子结点 --->无孩子结点
24+
return ;
25+
}
26+
int i;
27+
for(i = 0;i< bt[index].child.size();i++){
28+
DFSBiTree(bt[index].child[i],deep+1);
29+
}
30+
}
31+
32+
//BFS层次遍历
33+
void BFSBiTree(int root){
34+
queue<int> qu;//申请一个队列
35+
qu.push(root); //将根节点入队
36+
int num = 0;//记录每层叶子结点的数目
37+
int current;//存储当前处理的结点值
38+
int temp;
39+
40+
while(!qu.empty()){//如果队列非空
41+
temp = qu.front();//获得队首
42+
//printf("temp = %d,",temp);
43+
int i ;//循环变量
44+
if(bt[temp].child.size() > 0){//如果还有孩子结点
45+
for(i = 0;i<bt[temp].child.size();i++){//将其孩子结点分别入队
46+
qu.push(bt[temp].child[i]);
47+
}
48+
qu.pop();//出队
49+
current = qu.front();//出队后的队首
50+
//printf("current = %d\n",current);
51+
52+
if(bt[temp].depth == bt[current].depth){//如果两层深度相同
53+
//printf("temp.depth = %d , current.depth = %d\n",bt[temp].depth,bt[current].depth);
54+
if( bt[temp].child.size() == 0){//且temp为叶子结点
55+
num++;
56+
}
57+
}
58+
else{
59+
printf("%d ",num);
60+
num = 0;//置零
61+
}
62+
}
63+
else{//
64+
if( qu.size() > 1){//如果队列中还有很多元素
65+
qu.pop();
66+
current = qu.front();
67+
if(bt[temp].depth == bt[current].depth){//如果两层深度相同
68+
if( bt[temp].child.size() == 0){//且temp为叶子结点
69+
num++;
70+
}
71+
}
72+
else{
73+
num++;
74+
printf("%d ",num);
75+
num = 0;//置零
76+
}
77+
}
78+
else{
79+
num++;//否则这个节点肯定为树中的最后一个节点,直接输出
80+
printf("%d",num);
81+
qu.pop();
82+
}
83+
}
84+
}
85+
}
86+
87+
int main(){
88+
int n,m;//n是结点数,m是非叶子结点数
89+
scanf("%d %d",&n,&m);
90+
int i ,j;
91+
92+
for(i = 0;i<m;i++){
93+
int node,childNumber,childIndex;;//非叶子结点 //孩子结点数 //孩子结点下标
94+
scanf("%d %d",&node,&childNumber);//输入非叶子结点的下标 以及 其孩子结点数
95+
for(j = 1;j <= childNumber;j++){//输入孩子结点信息 --->根节点从1开始
96+
scanf("%d",&childIndex);//输入孩子结点下标
97+
bt[node].child.push_back(childIndex); //加入到孩子节点中
98+
}
99+
}
100+
101+
DFSBiTree(1,0);//根节点的下边是在1处,初始深度为0
102+
103+
// for(j = 1 ;j <= n;j++){
104+
// printf("j = %d,",j);
105+
// for(i = 1;i <= bt[j].child.size();i++){
106+
// printf("%d ",bt[j].child[i-1]);//注意这里的孩子结点在数组中存储起始地址是0
107+
// }
108+
// printf(",depth = %d\n",bt[j].depth);
109+
// }
110+
111+
BFSBiTree(1);
112+
}
113+
/**
114+
2 1
115+
01 1 02
116+
117+
2 1
118+
1 1 2
119+
120+
10 5
121+
01 3 02 03 04
122+
02 2 05 06
123+
03 2 07 08
124+
04 1 09
125+
05 1 10
126+
*/

0 commit comments

Comments
 (0)