题集通道: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. 在linux上建立多个ORACLE的实例

    1.打开终端,输入如下的命令:      [root@ptest4 ~]# export DISPLAY=localhost:1       [root@ptest4 ~]# xhost + 2.切换 ...

  2. LeetCode559 N叉树的最大深度

    题目: 思路: 直接递归求解最大深度就可以,这里主要记录一下Java中比较获得两个数中最大值的方法. import java.math.*; class Solution { public int m ...

  3. Day4-T3

    原题目 吉儿是一家古董店的老板娘,由于她经营有道,小店开得红红火火.昨天,吉儿无意之中得到了散 落民间几百年的珍宝——月亮之眼.吉儿深知“月亮之眼”价值连城:它是由许多珍珠相连而成的,工 匠们用金线连 ...

  4. windows下移植别人配置好的python环境

    一般来说,我们在windows下配置python环境的时候可能会比较推荐用anaconda,那么有一个比较方便的anaconda环境移植方法,也就是说,如果我已经在windows上安装好了anacon ...

  5. css画布

    绘制基本图形 绘制直线 <style> .canvas{ } </style> <canvas id="myCanvas1" style=" ...

  6. React+Flask打造前后端分离项目开发环境

    目录 前言 Backend-Flask Frontend-React Done References 前言 新的一年,开始水第一篇技术文.碰巧最近React玩得多,撸一篇文章纪念一下开发环境的搭建.

  7. Linux课后练习(第二章命令)20200218

  8. C++ 设置自动启动

    WCHAR pFileName[MAX_PATH] = {}; //得到程序自身的全路径 DWORD dwRet = GetModuleFileName(NULL, pFileName, MAX_PA ...

  9. 屏幕适配 - JS - 网站布局元素

    网页可见区域宽:document.body.clientWidth; 网页可见区域高:document.body.clientHeight; 网页可见区域高:document.body.offsetW ...

  10. java百货中心供应链管理系统 源码

    开发环境: Windows操作系统开发工具:MyEclipse/Eclipse + JDK+ Tomcat + MySQL 数据库 百货中心供应链管理系统主要用于实现了企业管理数据统计等.本系统结构如 ...