自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(73)
  • 收藏
  • 关注

原创 PTA-1107 dfs/并查集

#include<iostream>#include<vector>using namespace std;vector<int> v[1000], ans[1001];int visited[1001] = { 0 };int n, cursor = 0;bool same(int m, int n) { for (int each1 : v[m]) { for (int each2 : v[n]) if (e

2021-06-15 20:49:29 122

原创 1034.Head of a Gang dfs/并查集

Head of a Gangdfs求连通子图:1.通过dfs来遍历所有成员,将所有有通话记录的成员放在同一个团伙中(求连通子图),注意用结构体来储存团伙信息(团伙中用vector容器来储存内含成员)。2.用<string,int>的unordered_map来形成名字对序号的映射,用<int,string>的unordered_map来方便查询。#include<iostream>#include<unordered_map>#include&lt

2021-06-13 23:45:08 114

原创 1037.Magic Coupon 很简单的贪心

Magic Coupon解答:排序过后正正相乘,负负相乘,累加。#include<iostream>#include<algorithm>using namespace std;int main(){ int n1,n2,i,j,sum=0; int a[100000],b[100000]; cin>>n1; for(i=0;i<n1;i++) cin>>a[i]; cin>>n2;

2021-06-01 23:10:10 99

原创 1033.To Fill or Not to Fill

To Fill or Not to Fill分析:贪心算法。0.假设增加一个目的地处的加油站,距离为目的地的距离,价格为0,考虑从0距离开始能否到达最后一个加油站的问题1.因为先开始没有油,所以如果所有的加油站距离都没有等于0的,那么说明车哪也去不了,直接输出并return2.将加油站按照距离dis从小到大排序3.先去第一个加油站,设置变量nowdis表示当前所在的距离,maxdis是能够到达的最大距离,nowprice是当前的站点的价格,totalPrice是总的价格。贪心思想:0.寻找比自

2021-06-01 22:14:14 177

原创 1032.Sharing 链表

Sharing思路:采用逆向思维,先将两个序列放到两个vector容器中,再从尾到头遍历,判断地址是否相等(因为数据相同的结点地址相等,所以本题可以只判断地址是否相等,不用涉及结点data),遍历到不相等的结点序号时停止,输出下一个即可。(本题不建议用stack栈,因为前面的结点弹出去了不便于找不相等处的下一个结点)解答:#include<iostream>#include<vector>using namespace std;struct node{ char

2021-06-01 19:07:34 66

原创 1031.Hello World for U(水)

Hello World for U这是一道数学问题:n1=n3=max{ k | k<=n2 for n3=<n2<=N}且n1+n2+n3-2=N;时求n1,n3的值及n2的值,因为是三个边且要让三条边尽量相等,所以对n+2/3即可得到n1,n3解答:#include<iostream>using namespace std;int main() { int n1, n2, n3, i, j, k; string s; cin >&g

2021-06-01 18:34:40 48

原创 1030.Travel Plan

#include#includeusing namespace std;int N, M, S, D;//城市数 路数 起点 终点vector v[501];//邻接表来储存城市之间的信息int dis[501][501], cost[501][501];//二维数组来储存距离,费用信息int mindis_to[501];vector path, ans_path;int Mindis = 999999, Mincost = 999999;void dfs(int cur, int di

2021-05-31 23:35:53 82

原创 1029.Median 水排序

Median注意偶数位数的序列取的是中间两个的第一个解答:#include<iostream>#include<vector>#include<algorithm>using namespace std;int main(){ vector<int> v; int M,tmp; cin>>M; for(int i=0;i<M;i++){ cin>>tmp;

2021-05-31 20:58:15 101

原创 1028.List Sorting

List Sorting注意这道题如果s数组设置为100001->运行超时,设置为99999->段错误,即最后一个测试点为100000个学生解答:#include<iostream>#include<algorithm>#include<cstdio>using namespace std;struct student{ string Name; int Score,Id;}s[100000];bool comp1(stude

2021-05-30 17:12:29 55

原创 1027.Colors in Mars(水)

Colors in Mars不要忘了10-12用的是ABC…解答:#include<iostream>#include<string>using namespace std;int main(){ int red,green,blue; char book[14]="0123456789ABC"; cin>>red>>green>>blue; string ans; ans+=book[red/

2021-05-30 16:40:08 42

原创 1026.Table Tennis复杂的队列问题

#include<iostream>#include<queue>#include<algorithm>#include<vector>using namespace std;struct player{ int come_time,leave_time,play_time; int isvip=0;//0非vip,1是vip int serve_time=0;//初始化为0,便于区别是否已经得到服务};struct ta

2021-05-30 16:11:45 107

原创 1025.PAT Ranking

PAT Ranking解答:思路:用结构体来存放数据,用tmp容器来存放临时学生数据,用sort算法来对成绩进行排序,再给这些临时学生数据赋排名,昨晚排名操作后将临时数据全部插入到最终学生结构体数组中即可。注意同等成绩排名相同的排名方法: for(int j=1;j<K;j++){ if(tmp[j].Score==tmp[j-1].Score) tmp[j].location_rank=tmp[j-1].location_rank; e

2021-05-29 23:44:42 95

原创 1024.Palindromic Number

#include<iostream>using namespace std;bool judge(int* digit, int len) { for (int i = 0; i < len / 2; i++) { if (digit[i] != digit[len - i - 1]) return false; } return true;}int main() { string s; int K, digit1[20]

2021-05-29 18:45:30 59

原创 PTA-1023-数字字符串转化

Have Fun with Numbers解答:用哈希表来表示一个数字映射这个数字的个数,当然也也可以用一个数组来存放0-9的个数,但是用哈希表时只用判断哈希表是否为空,如果用数组就要遍历这个数组是否都为0#include<iostream>#include<unordered_map>using namespace std;unordered_map<int,int>Map;int main(){ string Strnum; int d

2021-05-28 23:19:33 232

原创 PTA-1022-模拟&哈希表

Digital Library解答:#include<iostream>#include<unordered_map>#include<set>using namespace std;unordered_map<string,set<string>> Map[6];//定义5个哈希表(只用1-5号),用来表示5个特性对id的映射int main(){ int N; cin>>N; getchar(

2021-05-28 21:49:56 87

原创 PTA-1021-Deepest Root

Deepest Root思路:1.进行一次dfs,统计有多少连通区域,如果n个点只有1个连通区域,就是树,如果多于1个连通区域,即证明不是树2.如果是树,采用类似高中化学找碳链最长子链的方法:①第一次dfs2,以任意一个结点(random),开始dfs找到最大深度终点1 2②第二次dfs2,以找到的最深终点(1 2)为起点,进行dfs,找到最大深度(到3 4)解答:#include<iostream>#include<vector>#include<set

2021-05-27 16:05:54 168

原创 PAT-1020-Tree Traversals

Tree Traversals解答:前序遍历递归会自动给根节点的左右根节点赋值,当用map来保存数据,map会自动按index从小到大排序,结束后只需遍历输出map即可得到前序遍历结果下面是萌神柳婼的解法:如何做到一个i既在中序中用又在后序中用???因为由后序遍历图解可知,i序号前面的为左子树,i序号后面的为右子树!!!#include <cstdio>#include <vector>#include <map>using namespace std

2021-05-25 23:02:26 67

原创 PTA-1019--General Palindromic Number(回文数判断)

General Palindromic Number判断进制转化后的数字是否回文数解答:#include<iostream>using namespace std;long N,b;int a[1000];int len=0;bool check(){ for(int i=0,l=len/2;i<l;i++){ if(a[i]!=a[len-i-1]) return false; } return true;}int main()

2021-05-25 20:07:25 113

原创 PTA-1008 Public Bike Management

Public Bike Management本题为深度优先搜索,通过维护多个最优解最后得到结果解答:#include<iostream>#include<vector>using namespace std;int Cmax,N,Sp,M;int bike[501],dis[501][501],mindis_to[501];//存放每个station有多少自行车vector<int>v[501];//采用邻接表来存放数据vector<int>

2021-05-25 19:24:28 64

原创 PTA-1017

Queueing at Bank解答:总结队列操作:①送客 先将当前时间该出队的人出队,如果没有需要出队的人就跳过当前操作②入队 送客过后需要判断是否存在空余窗口,如果有就入队③迎客 入队过后就直接迎客(因为本题中黄线内人数不限,即窗口前有一个人就处理一个人,与1014中黄线内人数有限对比),所以直接对入队后的人设定离开时间即可PS:因为本题中黄线内人数不限,所以不需要一个队列数组来储存黄线内人数,只需要一个数组(窗口个数大小)来储存窗口当前正在处理人的序号即可#include<iost

2021-05-25 11:27:48 87

原创 PTA-1016(哈希表,排序)

Phone Bills解答:#include<iostream>#include<string>#include<vector>#include<map>#include<algorithm>using namespace std;struct record { int dd, hh, mm, t; string tag;};double danjia[24];int N;map<string,

2021-05-22 19:25:51 121

原创 PTA-1015

PTA-1015A reversible prime in any number system is a prime whose “reverse” in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime.Now given any two positive integers N (&lt

2021-05-22 15:09:06 48

原创 PTA-1014

Waiting in LineSuppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are:The space inside the yellow line in front of each

2021-05-22 10:33:33 74

原创 结构体浅拷贝

结构体浅拷贝#define _CRT_SECURE_NO_WARNINGS#include<iostream>using namespace std;struct Student { int id;}students[200];int main() { struct Student s1 = students[0]; s1.id = 10; cout << students[0].id; system("pause"); return 0;}..

2021-05-19 15:17:22 40

原创 PTA-1012

The Best RankTo evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage stude

2021-05-19 14:54:20 54

原创 PTA-1011(水)

World Cup Betting对于要求小数点位数的问题还是乖乖用printf吧…解答:#include <iostream>using namespace std;int main() { double Max = -1, a, res = 0.65; int temp = -1; char book[3] = { 'W','T','L' }; for (int i = 0; i < 3; i++) { for (int j =

2021-05-17 09:48:45 48

原创 PTA-1010(SSS)

Radix判断两个数是否相等,输入四个数N1,N2,tag,radix。如果tag = 1,代表N1为radix进制,寻找使N1=N2成立的进制数。如果tag = 2,代表N2为radix进制,寻找使N1=N2成立的进制数。解答:进制转化+二分+数据溢出#include <iostream>#include <cmath>#include <string>#include <cstdio>#include <algorithm>u

2021-05-16 17:03:21 125

原创 PTA-1009

Product of Polynomials这是一道多项式相乘的题目解答:#include<iostream>using namespace std;int main() { int n1, n2, a; double b; double ans[1001] = { 0.0 }, bns[2001] = { 0.0 }; scanf("%d",&n1); for (int i = 0; i < n1; i++) {

2021-05-16 15:19:34 68

原创 PTA-1008

Elevator1.第一个数字是有多少个换楼层指令,后面是换楼层指令2.能不贮存数据就不储存解答:#include <iostream>using namespace std;int main() { int n,cur=0,time=0,temp=0; cin>>n; for(int i=0;i<n;i++){ cin>>temp; if(temp>cur) time+=(temp-cur

2021-05-15 23:04:20 63

原创 PTA(1007)-Maximum Subsequence Sum

Maximum Subsequence Sum本题考察动态规划(有双指针内味儿)解答:1.当temp<0的时候不需要更新,因为为0时不会影响sum值,所以不需要更新tempindex2.注意范围操作:保存小于0的下标,当由小于0进入大于0时,将这个下标赋值给rindex#include<iostream>using namespace std;#include<vector>int main() { int n; cin>>n;

2021-05-15 22:11:21 134

原创 PTA-1006

Sign In and Sign Out解答:1.INT_MAX,INT_MIN(int范围内的最大最小值)的头文件是2.注意时间的读入及比较先后的处理方式#include <iostream>#include <climits>using namespace std;int main(){ int n,fin=INT_MAX,lout=INT_MIN; string IDIN,IDOUT; cin>>n; for(int

2021-05-15 16:28:35 85

原创 PTA-1005

Spell It Right本题即要求将数字逐位相加并逐位输出数字英文1.用to_string的解答:直接用to_string将一串数字直接转化成字符串#include <iostream>using namespace std;int main() { string a; int sum=0;//sum必须要初始化!!! cin >> a; for(int i=0;i<a.length();i++) sum+=(a

2021-05-15 15:31:58 75

原创 PTA-1004

Counting Leaves第一行的意思是一共有两个节点,1个非根节点第一行以后的行,第一个数都是第几个结点,第二个是这个结点有几个子节点,第二个后面的数就都是这个结点的子节点的序号例如v[1]就是存放1号结点的子节点序号,v[2]就是存放2号结点的子节点序号,所以递归时传进dfs的是v[node,depth+1]DFS解答:#include <iostream>#include <vector>#include <algorithm>using na

2021-05-15 14:12:33 133

原创 PTA(3)--Emergency

PTA(3)–Emergency解答:1.深度优先搜索的尝试解答:深度优先搜索适用于单纯搜索最短距离,因其采用递归实现搜索的特性,如果要在递归中做一些步骤记录就较为困难,如本题中要记录路线中可叫上帮忙的救援队数量并取其最大值就难以实现。#define _CRT_SECURE_NO_WARNINGS#include<iostream>using namespace std;int m, n, c1, c2;int e[501][501];int Min = 99999999

2021-05-13 15:45:46 130

原创 Floyd-warshall算法(多源最短路径)

Floyd-warshall算法(多源最短路径)这种算法通常用于求一个点到其余多个点的最短距离下面的代码是求1这个顶点到其余5个顶点的最短距离:#define _CRT_SECURE_NO_WARNINGS#include<iostream>using namespace std;int main() { int m, n, e[10][10]; int inf = 999999999; int dis[10]; cin >> m >> n; f

2021-05-12 16:44:15 142

原创 啊哈!算法之图的遍历

图的遍历深度优先搜索#define _CRT_SECURE_NO_WARNINGS#include<iostream>using namespace std;int book[101], sum, n, e[101][101];void dfs(int cur);int main() { int m , a, b; cin >> m >> n; //读入一个n*n的无向图 //初始化对角线 for (int i = 1; i <= n;

2021-05-11 21:03:51 59

原创 PTA(1002)

PTA(1002)解答:本题对数据的输入并没有按照题目所给格式按照空格隔开。#include <iostream>#include <stdio.h>#include <set>using namespace std;int main(int argc, char const *argv[]){ float coef[1001] = {0}; // 记录每个不同指数前面系数,比如a[4]=2表示2x^4 int num; scanf("%d",

2021-05-10 19:57:05 78

原创 PTA(1001)-17

PTA(1001)-17解答:我的思路是将这个数逐位分离放到stack容器(栈)中,再利用先进后出的特性进行打印,注意打印时将前面的几位特殊处理(如99,123,234特殊处理前面的99两个数字)#include<iostream>#include<stack>using namespace std;#define ll long longint main() { ll a, b; cin >> a >> b; ll s

2021-05-09 17:09:29 47

原创 快速排序优化

快速排序优化普通快速排序常规的快速排序是采用两个哨兵i,j来分别从左右找到的更换值,一直找到i,j两个哨兵相遇(i=j)在一个while循环中用两个while分别来找更换值#define _CRT_SECURE_NO_WARNINGS#include<iostream>using namespace std;int a[101];void quicksort(int l, int r) { if (l > r) return; int i = l; int j =

2021-05-03 19:35:33 59

原创 剑指offer(29)--topk

topk1.sort排序class Solution {public: vector<int> GetLeastNumbers_Solution(vector<int> input, int k) { vector<int> ret; if(k==0 || k>input.size()) return ret; sort(input.begin(),input.end()); retur

2021-05-03 15:52:46 50

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除