【8.26】PAT甲级训练

1001

题目要求

两数相加,将结果按三位一逗号输出
inputoutput
-1000000 1-999,999

细节

使用vector容器
int c1=911,c2=119,sum;//
sum=c1+c2;
vector<int> v;
while(sum!=0){
	v.emplace_back(sum%10);
	sum/=10;
}
for(int i=v.size()-1;i>=0;i--)
{
	cout<<v[i];
}

1002

题目要求

将两个多项式相加,将系数不为零的项输出
inputoutput
2 1 2.4 0 3.2 // 2 2 1.5 1 0.53 2 1.5 1 2.9 0 3.2

细节

使用map容器,
map<int,double>m;//key为次数,value为系数
cin>>cishu>>xishu;
m[cishu]+=xishu;
if(m[cishu]==0)
	m.erase(cishu);//系数为0,则擦除当前项
对map进行遍历输出
for(map<int,double>::itrator it=map.begin();it!=map.end();it++)
{
	printf("%d %.1f",it->first,it->second);
}

1003

题目要求

图的遍历
从C1到C2的最短距离,同时获得最多的救援队数量
采用DFS

细节

采用vector数组存放每个城市的邻接城市
int min_len_of_each_city[500]={100000};
void DFS(int curent_city,int curent_len,int curent_team)
{
	if(curent_len>min_len_of_each_city[curent_city])
		return;
	if(curent_city==C2)
	{
		if(curent_len==min_len_of_each_city[curent_city]){
			paths++;//最短路线数量
			if(curent_team>teams)
				teams=curent_team;
		}
		else
		{
			paths=1;
			min_len_of_each_city[curent_city]=curent_len;
			teams=curent_team;
		}
	}
	else
	{
		if(curent_len<min_len_of_each_city[curent_city]){
			min_len_of_each_city[curent_city]=curent_len;
		}
		for(int i=0;i<v[curent_city].size();i++){
			int j=v[curent_city][i];
			DFS(j,curent_len+city_distance[curent_city][j],curent_team+city_team[j]);
		}
	}
}

1004

题目要求

家庭图谱,树的遍历
给出总结点数,非叶子结点数
下一行给出每个非叶子节点的子节点数和子节点编号
输出每一层的叶子节点数
inputoutput
2 1
01 1 020 1

细节

采用DFS
void dfs(int curID,int curlevel)
{
    if(curlevel>maxlevel) maxlevel=curlevel;
    if(children[curID].size()==0)
        num_of_eachlevel[curlevel]++;
    else{
        for(int i=0;i<children[curID].size();i++)
            dfs(children[curID][i],curlevel+1);
    }
}

1005

题目要求

给出一个很大的数,计算所有位数求和后,将结果每一位用英文输出

细节

使用c=getchar()读取每一位字符
使用sum=c-'0';将字符数字转换为整型数字
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值