PAT
summermoonlight
满船清梦压星河
展开
-
PAT A1003注意点
始终注意i是什么,警惕下面这种错误!!! for (int i = 0; i < temppath.size(); i++) { numteam += weight[temppath[i]];//这里要注意,不是+=weight[i]!!! } #include<iostream> #include<vector> #include<cstring> #include<algorithm> #include<map> #include&l原创 2021-05-04 09:24:33 · 81 阅读 · 0 评论 -
PAT题目分类
文章目录图的遍历最短路径最小生成树 图的遍历 A1013 Battle over cities A1021 Deepest Root A1034 Head of a Gang A1076 Forwards on Weibo 最短路径 A1003 Emergency A1018 Public Bike Management A1030 Travel Plan A1072 Gas Station A1087 All Roads lead to Rone 最小生成树 ...原创 2021-05-03 08:53:46 · 53 阅读 · 0 评论 -
PAT A1104 Sum of Number Segments
17分,感觉题目数据有问题 #include <cstdio> int main(){ int n; double v, ans = 0; scanf("%d", &n); for(int i = 1; i <= n; i++){ scanf("%lf", &v); ans += v * i * (n + 1 -i); } printf("%.2f\n", ans); return 0; }原创 2021-04-23 20:52:36 · 79 阅读 · 0 评论 -
PAT A1048 Find Coins题解
满分题解 #include<iostream> #include<stdio.h> #include<string> #include<vector> #include<cctype> #include<algorithm> using namespace std; const int maxn = 100005; int a[maxn]; int n, m; int main() { //freopen("input.txt.原创 2021-04-22 16:20:55 · 69 阅读 · 0 评论 -
PAT A1044 Shopping in Mars题解
注意要点: 首先看两个函数定义 lower_bound( begin,end,num) 从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字, 找到返回该数字的地址,不存在则返回end。 通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。 upper_bound( begin,end,num) 从数组的begin位置到end-1位置二分查找第一个大于num的数字, 找到返回该数字的地址,不存在则返回end。 通过返回的地址减去起始地址begin,得到找到数字在数组原创 2021-04-22 15:57:20 · 68 阅读 · 0 评论 -
PAT A1067 Sort with Swap(0, i)题解
注意要点: 题中,我们需要找到index最小的未归位的数,这里我们可以使用,for循环遍历,但是注意到数组规模为100000,所以如果每次使用从1开始的循环,必然会有超时出现,我们需要用一个k保存结果,k表示,index小于k的数组全部已经排序完毕。所以我们每次从k开始遍历就行。 另外,swap的思路有两种 1、 找到数字0所在位置k,再找到数字k所在位置m,swap(a[k],a[m]),循环 2、 找到位置0上的数字x,swap(a[0],a[x]) 比较得知,方法2时间复杂度大大的优于方法1 满原创 2021-04-22 10:57:14 · 100 阅读 · 0 评论 -
PAT A1010 Radix题解
满分题解,使用二分查找 #include <stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<string> #include<cctype> #include<string.h> using namespace std; const long long inf = (1LL << 63) - 1; const i原创 2021-04-21 18:57:17 · 141 阅读 · 0 评论 -
PAT A1085 Perfect Sequence题解
此解与《算法笔记》中有一点点不一样,书中solve函数对每一个i在[i+1,n-1]寻找第一个大于x的数。这使得,返回的index最大为n-1,不能表达出,x>数列中所有数的情况。所以书中需要判断,数组中是否所有数均小于x,是则直接返回。 我的solve函数在区间[i+1,n]寻找第一个大于x的数,若返回的index==n,则说明,数组中是否所有数均小于x,省去判断步骤。 #include <stdio.h> #include<iostream> #include<vec原创 2021-04-20 21:53:28 · 80 阅读 · 0 评论 -
PAT A1033 To Fill or Not to Fill 题解
详见《算法笔记》p151,我和作者思路相同,具体实现有一点点不一样,作者代码更简洁,我使用了flag,代码稍显臃肿。 #include <stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<string> #include<cctype> using namespace std; const int maxn = 505; double cm原创 2021-04-19 20:06:31 · 78 阅读 · 0 评论 -
PAT A1065 A+B and C (64bit)
1、long long的范围:[-263,263 ) 2、a,b最大为263 -1,a+b最大为264 -2,因此使用long long存储正溢出的值区间为[-263, -2] 3、a+b首先要存放在一个long long变量中,再与c比较,否则会出错 #include<stdio.h> #include<iostream> #include<algorithm> #include<string> #include<vector> using n原创 2021-04-03 13:48:03 · 56 阅读 · 0 评论 -
PAT A1042 Shuffling Machine
#include<stdio.h> #include<iostream> #include<algorithm> #include<string> #include<vector> using namespace std; int start[55]; int ee[55]; int nn[55]; char str[] = { 'S','H','C','D','J' }; int main() {; //freopen("input原创 2021-04-02 21:44:05 · 43 阅读 · 0 评论