题集通道: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. 启动Eureka出现错误:Archive for required library in project cannot be read or is not a valid ZIP file

    To fix issues like that, let Maven download the files again: Delete the folder D:/mypath/.m2/reposit ...

  2. redis改配置

    命令行: 暂时生效,适合于做测试,或者线上马上服务修改,重启失效 CONFIG set stop-writes-on-bgsave-error no OK CONFIG get stop-writes ...

  3. C# OBJ模型解析的封装(网上看到的保留一份)

    /// <author>Lukas Eibensteiner</author> /// <date>19.02.2013</date> /// < ...

  4. node.js - 定义全局变量

    1,定义全局变量 app.set('name','八戒') 2,获取全局变量 app.get('name')

  5. 二、JavaScript之点击按钮改变HTML样式 (CSS)

    一.代码如下 二.点击前 三.点击后 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" c ...

  6. kafka创建topic,生产和消费指定topic消息

    启动zookeeper和Kafka之后,进入kafka目录(安装/启动kafka参考前面一章:https://www.cnblogs.com/cici20166/p/9425613.html) 1.创 ...

  7. HDU - 5695 Gym Class (优先队列+拓扑排序)

    题意:有N个人,每个人的ID为1~N,部分同学A不希望部分同学B排在他之前,排好队之后,每个同学会找出包括自己在内的前方所有同学的最小ID,作为自己评价这堂课的分数.在满足这个前提的情况下,将N个人排 ...

  8. CGridCtrl设置行列不可拉伸

    m_HFlexGrid.SetColumnResize(FALSE); m_HFlexGrid.SetRowResize(FALSE);

  9. C++ #if 1

    当注释掉大块代码时,使用"#if 0"比使用"/**/"要好,因为用"/**/"做大段的注释要防止被注释掉的代码中有嵌套的"/** ...

  10. c\c++ 中字符串分割,并且转换为整形数据

    在项目开发中,经常使用到字符串分割, 并且将其转换为整形(比如IP的分割获取,MAC地址的分割获取等),代码如下: #ifndef _UNICODE void StrToIntData( char * ...