题集通道: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. 《ES6标准入门》(阮一峰)--10.对象的扩展

    1.属性的简洁表示法 ES6 允许在大括号里面,直接写入变量和函数,作为对象的属性和方法.这样的书写更加简洁. const foo = 'bar'; const baz = {foo}; baz // ...

  2. maven在windows下的安装配置及手动引入oracle数据库jar包

    一.maven的安装配置 注意:在进行如下配置之前,有个前提是你的java的jdk安装配置正确才行 1.首先,下载maven,网址http://maven.apache.org/download.cg ...

  3. 洛谷 P2426 删数

    题目传送门 解题思路: 区间DP,f[i][j]表示区间i~j可获得的最大值,因为本题的所有区间是可以直接一次性把自己全删掉的,所以所有区间初始化为被一次性删除的值,然后枚举断点,跑区间DP. AC代 ...

  4. (java) webdriver 启动firefox driver时,加载firebug的扩展

    去网上下载一个firebug.xpi(对应版本, 我的ff是17,可以使用firebug-1.11.4.xpi,最好使用非firefox浏览器下载,不然提示你直接安装到firefox) @Before ...

  5. 环境变量和文件查找&文件打包与解压缩

    环境变量和文件查找 介绍环境变量的作用与用法 及几种搜索文件的方法 学会这些技巧可以高效地使用 Linux 知识点:环境变量的设置 环境变量的修改 环境变量 要解释环境变量,得先明白变量是什么,准确的 ...

  6. 全面介绍Windows内存管理机制及C++内存分配实例

    转自:http://blog.csdn.net/yeming81/article/details/2046193 本文基本上是windows via c/c++上的内容,笔记做得不错.. 本文背景: ...

  7. Windows系统自带选择文件的对话重写和居中处理

    class CMyFileDialog: public CFileDialogImpl<CMyFileDialog> { public: CMyFileDialog(BOOL bOpenF ...

  8. COGS 1489玩纸牌

    %%%http://blog.csdn.net/clover_hxy/article/details/53171234 #include<bits/stdc++.h> #define LL ...

  9. Vulkan 开发学习资料汇总

    开发资料汇总 1.API Reference 2.Vulkan Spec 有详细说明的pdf 文章 1.知乎Vulkan-高性能渲染 2.Life of a triangle - NVIDIA's l ...

  10. typeof()与Object.prototype.toString.call()

    用typeof方法只能初步判断number string undefined boolean object function symbol这几种初步类型 使用Object.prototype.toSt ...