PAT A1001-A1004
题集通道: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的更多相关文章
- PAT A1001 A+B Format (20 分)
AC代码 #include <cstdio> #include <algorithm> using namespace std; const int maxn = 11; in ...
- PAT甲级——A1004 Counting Leaves
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
- PAT A1001 A+B Format
Calculate a+b and output the sum in standard format -- that is, the digits must be separated into gr ...
- PAT题目AC汇总(待补全)
题目AC汇总 甲级AC PAT A1001 A+B Format (20 分) PAT A1002 A+B for Polynomials(25) PAT A1005 Spell It Right ( ...
- 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 ...
- PAT甲级——【牛客练习A1004】
题目描述 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For ex ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- PAT/字符串处理习题集(二)
B1024. 科学计数法 (20) Description: 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+ ...
- PTA A1003&A1004
第二天 A1003 Emergency (25 分) 题目内容 As an emergency rescue team leader of a city, you are given a specia ...
- PTA A1001&A1002
从今天起每天刷1-2题PAT甲级 第一天 A1001 A+B Format (20 分) 题目内容 Calculate a+b and output the sum in standard forma ...
随机推荐
- S7-300位逻辑指令仿真练习 停车场
第三章 S7-300 指令应用 位逻辑指令 M存储器 在PLC中M存储区(也称位存储区,又称内部存储器标志位(M)存储器区),它属于系统存储区.在你选定具体的CPU型号后,可以查看CPU的技术规格,其 ...
- P 1008 说反话
转跳点:
- wireshark混杂模式
来自:https://blog.csdn.net/mukami0621/article/details/78645825 通过设置网卡为混杂模式就能捕获局域网内所有发包内容,包括非广播包和非发给自己主 ...
- python 获取cpu、内存、硬盘等实时信息 psutil
psutil是一个跨平台库,能够轻松实现获取系统运行的进程和系统利用率(CPU,内存,磁盘,网络等)信息,主要应用于系统监控,分析和限制系统资源及进程的管理,它实现了同等命令行工具提供的功能,如ps, ...
- WebStorm 使用经验
1.优点 1.1 可自动提示图片的宽高 1.2 标签名字可重构(改名) 1.3 css重命名 1.4 可把内联的style移到外部 1.5 可实现声明提升 1.6 设置项是可搜索的 1.7 有 ...
- 指令——less
一.Liunx系统下的一般命令格式. 命令——实际上就是在Liunx终端中,在命令行中输入的内容. Liunx中一个命令的完整格式为: #指令主体(空格) [选项](空格) [操作对象] 指令主体—— ...
- 九十五、SAP中查看自定义包的所有模块,对象,函数主,事务等
一.输入SE80 二.选择包,再查下Z* 三.可以看到,查下出来的包 四.可以看到我们想要的内容了
- HDU_4960 2014多校9 Another OCD Patient DP
其实现在想起来是个巨简单的DP,模型就跟LCS很像,比赛的时候居然没想出来,在聪哥提醒下还卡了个地方 就是说给定一串n个数字的序列,可以连续合并,最终使得序列是回文的,题目也给定了合并数字所需的代价, ...
- 干货分享:深度解析Supplement Essay写作
今天Hotessay小编给同学们介绍下附加文书的创作思路.因为附加文书基本上都是短essay,所以简洁才是硬道理! 通常,我们可以把美国大学的附加文书分为以下几类: 1.Tell us about y ...
- UVA - 1153 Keep the Customer Satisfied(顾客是上帝)(贪心)
题意:有n(n<=800000)个工作,已知每个工作需要的时间qi和截止时间di(必须在此之前完成),最多能完成多少个工作?工作只能串行完成.第一项任务开始的时间不早于时刻0. 分析:按截止时间 ...