题集通道:https://pintia.cn/problem-sets/994805342720868352/problems/type/7

A1001 :  A+B Format (20 point(s))

  解这道题的关键是题目所给的条件: - 10e6 <= a,b <= 10e6,所以a+b最多为7位数。

  代码如下:

 #include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
int sum = a + b;
if(sum < )
{
cout << '-';
sum = -sum;
}
bool flag = false;
int tmpNum = sum/;
sum%=;
if(tmpNum > ) {printf("%d", tmpNum);flag = true;}
tmpNum = sum/;
sum%=;
if(flag) printf(",%03d", tmpNum);
else if(tmpNum > ){printf("%d", tmpNum);flag = true;}
flag ? printf(",%03d", sum) : printf("%d", sum);
return ;
}

A1002 : A+B for Polynomials (25 point(s))

  解这道题的关键是题目所给的条件:每个多项式的格式是幂逐渐减小的。

  1.可以依次找多项式的较大项进行处理。

  2.也可以直接建立一个容量1000的数组,把他们都当做1000项的多项式处理。

  方法1的代码如下:

 #include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct NODE
{
int exp;
double val;
NODE():exp(),val(){}
NODE(int exp, double val):exp(exp),val(val){}
}node;
vector<node> poly1, poly2;
vector<node> resultPoly;
int main()
{
int n, tmpNum;
double tmpDouble;
cin >> n;
while(n--)
{
cin >> tmpNum >> tmpDouble;
poly1.push_back(NODE(tmpNum, tmpDouble));
}
cin >> n;
while(n--)
{
cin >> tmpNum >> tmpDouble;
poly2.push_back(NODE(tmpNum, tmpDouble));
}
int index1=, index2 = ;
while(index1 < poly1.size() && index2 < poly2.size())
{
if(poly1[index1].exp == poly2[index2].exp)
{
double tmpDouble = poly1[index1].val+poly2[index2].val;
if(tmpDouble != )
resultPoly.push_back(NODE(poly1[index1].exp, tmpDouble));
index1 ++; index2 ++;
}
else
(poly1[index1].exp > poly2[index2].exp) ? resultPoly.push_back(poly1[index1++]) : resultPoly.push_back(poly2[index2++]);
}
while(index1 < poly1.size()) resultPoly.push_back(poly1[index1++]);
while(index2 < poly2.size()) resultPoly.push_back(poly2[index2++]);
cout << resultPoly.size();
for(auto it = resultPoly.begin(); it != resultPoly.end(); ++ it)
printf(" %d %.1f", it->exp, it->val);
return ;
}

A1003 : Emergency (25 point(s))

  解这道题目的关键是:最短路径(Dijkstra)、最短路径数目(所有最短路径的上一个节点路径数之和)、最多救护人员(所有最短路径中上一个节点最多的救护人员+本节点救护人员数

  1.邻接矩阵存储,Dijkstra处理

  代码如下:

 #include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
const int INF = 0x7f7f7f7f;
int routeMap[][];
int dis[]={};
int assNum[]={};
int pathCnt[]={};
int assistCnt[]={};
int N, M, C1, C2;
void Dijkstra(int u)
{
dis[u] = ;
assistCnt[u] = assNum[u];
pathCnt[u] = ;
vector<bool> flagVec(, false);
for(int i = ; i < N; ++ i)
{
int minNum = INF, v = -;
for(int j = ; j < N; ++ j)
if(!flagVec[j] && dis[j] < minNum)
{
minNum = dis[j];v = j;
}
if(v == -)
break;
flagVec[v] = true;
for(int j = ; j < N; ++ j)
{
if(!flagVec[j] && routeMap[v][j]+dis[v] < dis[j])
{
dis[j] = routeMap[v][j]+dis[v];
assistCnt[j] = assistCnt[v] + assNum[j];
pathCnt[j] = pathCnt[v];
}
else if(!flagVec[j] && routeMap[v][j]+dis[v] == dis[j])
{
pathCnt[j] += pathCnt[v];
if(assistCnt[j] < assistCnt[v] + assNum[j])
assistCnt[j] = assistCnt[v] + assNum[j];
}
}
}
}
int main()
{
cin >> N >> M >> C1 >> C2;
memset(routeMap, 0x7f, sizeof(routeMap));
memset(dis, 0x7f, sizeof(dis));
int tmpSt, tmpEnd, tmpDis;
for(int i = ; i < N; ++ i)
cin >> assNum[i];
for(int i = ; i < M; ++ i)
{
cin >> tmpSt >> tmpEnd >> tmpDis;
routeMap[tmpSt][tmpEnd] = tmpDis;
routeMap[tmpEnd][tmpSt] = tmpDis;
}
Dijkstra(C1);
cout << pathCnt[C2] << " " << assistCnt[C2];
return ;
}

A1003 : Counting Leaves (30 point(s))

  解这道题目的关键是:需要输出每层的叶子节点数。

  注意:N为0时不处理

  1.静态存储树,bfs

 #include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int N, M;
typedef struct NODE
{
vector<int> child;
}node;
vector<node> treeVec();
void Bfs(int u)
{
queue<int> bfsQue;
bfsQue.push(u);
bool symbolFlag = false;
while(!bfsQue.empty())
{
int len = bfsQue.size();
int leafCnt = ;
for(int i = ; i < len; ++ i)
{
int tmpNum = bfsQue.front();
bfsQue.pop();
if(treeVec[tmpNum].child.size() == )
leafCnt++;
else
{
for(auto it = treeVec[tmpNum].child.begin(); it != treeVec[tmpNum].child.end(); ++ it)
{
bfsQue.push(*it);
}
}
}
symbolFlag ? printf(" ") : symbolFlag = true;
cout << leafCnt;
}
}
int main()
{
cin >> N >> M;
int tmpId, tmpChildCnt, tmpChild;
if(N == )
return ;
for(int i = ; i < M; ++ i)
{
cin >> tmpId >> tmpChildCnt;
for(int j = ; j < tmpChildCnt; ++j)
{
cin >> tmpChild;
treeVec[tmpId].child.push_back(tmpChild);
}
}
Bfs();
return ;
}

PAT A1001-A1004的更多相关文章

  1. PAT A1001 A+B Format (20 分)

    AC代码 #include <cstdio> #include <algorithm> using namespace std; const int maxn = 11; in ...

  2. PAT甲级——A1004 Counting Leaves

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  3. PAT A1001 A+B Format

    Calculate a+b and output the sum in standard format -- that is, the digits must be separated into gr ...

  4. PAT题目AC汇总(待补全)

    题目AC汇总 甲级AC PAT A1001 A+B Format (20 分) PAT A1002 A+B for Polynomials(25) PAT A1005 Spell It Right ( ...

  5. PAT A1004 Counting Leaves (30 分)——树,DFS,BFS

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  6. PAT甲级——【牛客练习A1004】

    题目描述 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For ex ...

  7. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  8. PAT/字符串处理习题集(二)

    B1024. 科学计数法 (20) Description: 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+ ...

  9. PTA A1003&A1004

    第二天 A1003 Emergency (25 分) 题目内容 As an emergency rescue team leader of a city, you are given a specia ...

  10. PTA A1001&A1002

    从今天起每天刷1-2题PAT甲级 第一天 A1001 A+B Format (20 分) 题目内容 Calculate a+b and output the sum in standard forma ...

随机推荐

  1. django-腾讯paas-appengine阅读

    1 重写View基类的dispatch函数 api/baseview.py 在一个post请求中,在header中,CONTENT_TYPE为application/json,然后在request.b ...

  2. ubuntu 中加速pip指令下载插件的速度

    在使用pip下载时很多时候下载速度特别慢,时不时就会发生timeout. 这是因为安装源与本机之间网络不畅导致,其实可以自己指定pip的下载来源,就像指定ubuntu更新源那样. 接下来谈谈步骤: 1 ...

  3. Redis 详解 (四) redis的底层数据结构

    目录 1.演示数据类型的实现 2.简单动态字符串 3.链表 4.字典 5.跳跃表 6.整数集合 7.压缩列表 8.总结 上一篇博客我们介绍了 redis的五大数据类型详细用法,但是在 Redis 中, ...

  4. Windows下C++遍历文件夹中的文件

    Windows下,在VS中开发,C++遍历文件夹下文件. 在Windows下,遍历文件所用到的函数和结构体,需要在程序中包含头文件#include <io.h>,在VS中,头文件io.h实 ...

  5. 洛谷 三月月赛 B

    搞出每一位与前一位的差,然后区间修改只是会影响区间的端点,所以只修改一下端点的值就好. %%%高一神犇线段树 #include<bits/stdc++.h> #define N 10000 ...

  6. 【SQL必知必会笔记(1)】数据库基础、SQL、MySQL8.0.16下数据库、表的创建及数据插入

    文章目录 1.数据库基础 1.1 数据库(database) 1.2 表(table) 1.3 列和数据类型 1.4 行 1.5 主键 2.什么是SQL 3.创建后续练习所需数据库.表(MySQL8. ...

  7. HZNU-ACM寒假集训Day12小结 数论入门 题解

    算不出的等式 BJOI2012 看到这题 真没什么办法 无奈看题解 1.注意到p/q 联想到斜率 2.注意到 [ ] 联想到整点 注意到k在变化,构造一次函数 f(x)=p/q*x ,g(x)=q/p ...

  8. Node.js 文件系统模块

    章节 Node.js 介绍 Node.js 入门 Node.js 模块 Node.js HTTP模块 Node.js 文件系统模块 Node.js URL模块 Node.js NPM Node.js ...

  9. Sklearn 速查

    ## 版权所有,转帖注明出处 章节 SciKit-Learn 加载数据集 SciKit-Learn 数据集基本信息 SciKit-Learn 使用matplotlib可视化数据 SciKit-Lear ...

  10. Django static配置

    STATIC_URL = '/static/' # HTML中使用的静态文件夹前缀 STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static&q ...