pat A1079
题意:
解题思路:
重点还是在于树的一个深度搜索
参考代码:
/*
解题思路:
1.静态树+DFS
2.设置库函数+结构体+变量
3.DFS深度遍历:形参:int类型 index depth
1.递归的思路
2.如果结构数组中index下的孩子的长度为0表示到达叶节点,然后累加货物的数量*单价的比率(叶子结点的值*概率(其中概率是比率的深度次幂))并且结束函数
3.for循环按照当前结点,递归DFS,同时深度+1
4.main函数:
1.输入npr,同时r/100
2.for循环进行输入:先输入k,如果k为0,表示叶子结点,则在对应的结构数组的下标中的data域进行统计
3.如果遇到非0,则把对应的数字,压入到vector数组中
4.调用DFS函数,其值全为0 0
5.最后用单价p*ans
*/
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=100005;
int n;
double p,r;
struct node
{
int data;
vector<int> child;
}Node[maxn];
double ans=0; //千万注意ans的值,同样设置为double类型
void DFS(int index, int depth)
{
if(Node[index].child.size()==0)
{
ans+=Node[index].data*pow(r+1,depth);
return ;
}
else
{
for(int i=0; i<Node[index].child.size(); i++)
DFS(Node[index].child[i],depth+1);
}
}
int main()
{
scanf("%d%lf%lf",&n,&p,&r); //直接用scanf输入不会出错
r/=100;
int k,temp;
for(int i=0; i<n; i++)
{
cin >> k;
if(k==0) //k为0,一定要输入他的值
cin >> Node[i].data;
else
{
for(int j=0; j<k; j++)
{
cin >> temp;
Node[i].child.push_back(temp);
}
}
}
DFS(0,0);
printf("%.1f",ans*p);
return 0;
}
注意事项:
scanf函数是通过指针指向变量的。
%f告诉scanf函数在所传地址位置上存储一个float型值, 而%lf告诉scanf函数在所传地址位置上存储一个double型值。
这里float和double的区别是非常重要的。
printf() 函数中不存在 %lf ,输入 double 用 %lf 输出用 %f
pat A1090
题意:
解题思路:
参考代码:
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
double p=0.0,r;
int n,num=0,maxnDepth=0; //num表示最大深度的叶结点个数,maxDepth表示最大深度
const int maxn=100005;
vector<int> child[maxn]; //存放树,这个设置表示横着是vector数组,而每一个下标向下又是一个数组,这个数组需要压入
void DFS(int index,int depth) //注意这是深度优先搜索
{
if(child[index].size()==0) //南墙
{
if(depth>maxnDepth) //更新 最大深度,和最大深度的个数
{
maxnDepth=depth;
num=1;
}
else if(depth==maxnDepth)
{
num++;
}
return ; //返回空值
}
for(int i=0; i<child[index].size(); i++) //岔道口:对于这个题而言,它是选择另一条路进行向下走,所以index其实表示各个路口,只不过是选择另一个路口进行向下走,而且该路口是他下面的路口,所以深度也要+1
{
DFS(child[index][i],depth+1);
}
}
int main()
{
int father,root;
scanf("%d %lf %lf",&n,&p,&r);
r/=100;
for(int i=0; i<n; i++)
{
cin >> father;
if(father!=-1)
{
child[father].push_back(i);
}
else
{
root=i; //找根结点
}
}
DFS(root,0);
printf("%.2f %d\n",p*pow(1+r,maxnDepth),num); //最后输出最大的单价,就是直接用深度可以计算,以及输出最大深度时的个数
return 0;
}
pat A1094
题意:
解题思路:
参考代码:
#include<iostream>
#include<vector>
using namespace std;
const int maxn=101;
vector<int> Node[maxn];
int n,m,maxnum=-1,maxshen=-1;
int hashtable[maxn]={0};
void DFS(int index, int depth) //可以通过DFS遍历每一层的人数
{
hashtable[depth]++;
for(int i=0; i<Node[index].size(); i++) //其他的路口
DFS(Node[index][i],depth+1);
}
int main()
{
cin >> n >> m;
int temp,temp1,temp2;
for(int i=0; i<m; i++)
{
cin >> temp >> temp1;
for(int i=0; i<temp1; i++)
{
cin >> temp2;
Node[temp].push_back(temp2);
}
}
DFS(1,1);
for(int i=1; i<maxn; i++)
{
if(hashtable[i]>maxnum)
{
maxnum=hashtable[i];
maxshen=i;
}
}
cout << maxnum << " " << maxshen;
return 0;
}
pat A1106
题意:
注意几个点:
注意事项:
1.为了计算根结点时的最小单价,必须将初始值设置为最大
参考代码:
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
const int maxn=100003;
int n,renshu[maxn]={0},lowd;
double p,r,minvalue=9999999999.9; //我感觉主要的原因是minvalue赋初值还是要double类型,不要用整数
vector<int> child[maxn];
void DFS(int index,int depth)
{
if(child[index].size()==0)
{
if(p*pow(1+r,depth)<minvalue)
{
minvalue=p*pow(1+r,depth);
lowd=depth;
}
renshu[depth]++;
return ;
}
for(int i=0; i<child[index].size(); i++)
{
DFS(child[index][i],depth+1);
}
}
int main()
{
cin >> n >> p >> r;
int temp,temp1;
r/=100;
for(int i=0; i<n; i++)
{
cin >> temp;
if(temp!=0)
{
for(int j=0; j<temp; j++)
{
cin >> temp1;
child[i].push_back(temp1);
}
}
}
DFS(0,0);
printf("%.4f %d",minvalue,renshu[lowd]);
return 0;
}
A1004
#include<iostream>
#include<vector>
using namespace std;
int n,m;
const int maxn=101;
vector<int> child[maxn];
int yezi[maxn]={0};
int maxdepth=-1;
void DFS(int index, int depth) //还是DFS的思想,题目要求统计每层叶子结点的个数,初始值全设置为0,如果遍历的叶子结点,就在当前深度对应的数值自增1
{
if(child[index].size()==0)
{
if(depth>maxdepth)
maxdepth=depth; //统计最大的层数是为了后面遍历每一层的叶子结点个数以便输出
yezi[depth]++;
return ;
}
for(int i=0; i<child[index].size(); i++)
DFS(child[index][i],depth+1);
}
int main()
{
cin >> n >> m;
int temp,temp1,temp2;
for(int i=0; i<m; i++)
{
cin >> temp >> temp1;
for(int j=0; j<temp1; j++)
{
cin >> temp2;
child[temp].push_back(temp2);
}
}
DFS(1,1);
for(int i=1; i<=maxdepth; i++)
{
if(i!=1)
cout << " ";
cout << yezi[i];
}
return 0;
}
A1053:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
/*
本题中:就是将数字放到树中,然后寻找和满足条件的,本题的实质是DFS,找条件,是普通树的一个问题
*/
const int maxn=101;
int n,m,S,path[maxn];
struct node
{
int data;
vector<int> child;
}Node[maxn];
bool cmp(int a, int b)
{
return Node[a].data>Node[b].data;
}
void DFS(int index, int Nown, int sum)
{
if(sum>S)
return ;
if(sum==S)
{
if(Node[index].child.size()!=0)
return ;
for(int i=0; i<Nown; i++)
{
cout << Node[path[i]].data;
if(i<Nown-1)
cout << " ";
else
cout << endl;
}
return ;
}
for(int i=0; i<Node[index].child.size(); i++) //i表示的是Node对应的index的下的孩子的所有下标,然后Node后面紧接着对应的是父亲结点的下标,也就是当前结点index的下标
{
int childl=Node[index].child[i]; //孩子下标
path[Nown]=childl; //让path中的值为childl
DFS(Node[index].child[i],Nown+1,sum+Node[childl].data); //sum其实加的是每个孩子的值,index相当于父亲结点
}
}
int main()
{
cin >> n >> m >> S;
for(int i=0; i<n; i++)
{
cin >> Node[i].data;
}
int temp,temp1,temp2;
for(int i=0; i<m; i++)
{
cin >> temp >> temp1;
for(int j=0; j<temp1; j++)
{
cin >> temp2;
Node[temp].child.push_back(temp2);
}
sort(Node[temp].child.begin(),Node[temp].child.end(),cmp);
}
path[0]=0;
DFS(0,1,Node[0].data);
return 0;
}