算法
OJ题解
WinnieJiang
这个作者很懒,什么都没留下…
展开
-
PAT(甲) - 1017 Queueing at Bank
注意单位#include <bits/stdc++.h>using namespace std;typedef struct{ int stamp; int process_time;} person;typedef struct{ int hh; int mm; int ss;} times;int times2stamp(times t){ return t.ss + t.mm * 60 + t.hh * 60 * 6原创 2021-08-16 17:50:28 · 134 阅读 · 0 评论 -
PAT(甲) - 1016 Phone Bills
注意审题:“It is guaranteed that at least one call is well paired in the input.” 只是说了输入中有配对的记录,但并没有说每个人都有配对的记录,因此存在个人话费为0的情况,此时什么都不需要输出(包括名字)#include <bits/stdc++.h>using namespace std;typedef struct{ string name; int stamp; int status;}原创 2021-08-15 18:19:57 · 118 阅读 · 0 评论 -
PAT(甲) - 1015 Reversible Primes
注意1不为质数,2为质数的特殊情况其中测试点2为值为2的测试#include <bits/stdc++.h>using namespace std;int prime(int num){ if (num == 1) return -1; for (int i = 2; i <= sqrt(num); i++) { if (num % i == 0) return -1; } retu原创 2021-07-31 17:49:55 · 100 阅读 · 0 评论 -
PAT(甲) - 1014 Waiting in Line
#include <bits/stdc++.h>using namespace std;typedef struct{ int start; int length; int end;} cu;string t2s(int t){ int h = 8 + t / 60; int m = t % 60; string temp = ""; if (h < 10) temp = temp + "0";原创 2021-07-28 16:42:11 · 49 阅读 · 0 评论 -
PAT(甲) - 1013 Battle Over Cities
#include <bits/stdc++.h>using namespace std;int mapp[1000][1000];int after[1000][1000];int vis[1000];int n;void dfs(int a){ vis[a] = 1; for (int i = 1; i <= n; i++) { if (after[a][i] == 1 && vis[i] == 0)原创 2021-06-04 19:58:13 · 56 阅读 · 0 评论 -
HDOJ - 1002 A + B Problem II
我的想法是:先将两个加数中长度小的那个放在变量a中,长度大的放在变量b中,从后向前累加,h暂存进位,结果放到变量c中;考虑到两个数相加后可能有总进位,导致strlen(c) = strlen(b) + 1,所以c从strlen(b)的位置开始存放,要是没有总进位,再往前挪一个避坑:前面交换了a和b后,输出的时候要交换回来;strlen()返回的是无符号型,运算到负数时返回的是最大数,所以要先转成int#include <bits/stdc++.h>using namespace std.原创 2021-05-28 15:52:05 · 51 阅读 · 0 评论 -
PAT(甲) - 1012 The Best Rank
#include <bits/stdc++.h>using namespace std;int main(){ ios::sync_with_stdio(false); int n, m; cin >> n >> m; int score[3][n]; double avg[n]; int rank[4][n]; string id[n]; string classes[4] = {"A", "C",原创 2021-05-25 15:21:57 · 47 阅读 · 0 评论 -
PAT(甲) - 1011 World Cup Betting
#include <bits/stdc++.h>using namespace std;int main(){ double sum = 1; for (int i = 0; i < 3; i++) { double w, t, l; cin >> w >> t >> l; if (w > t) { if (w > l)原创 2021-05-25 14:44:22 · 54 阅读 · 0 评论 -
HDOJ - 2602 Bone Collector
易错点背包容量要和物品体积先进行比较,防止边界溢出需要考虑到物品体积为0的情况数组要从0编号到n(背包容量),而不是n-1,因为涉及到与体积的直接比较#include <bits/stdc++.h>using namespace std;#define ll long longint main(){ ios::sync_with_stdio(false); int T; cin >> T; while (T--) {原创 2021-05-22 19:48:09 · 63 阅读 · 0 评论 -
PAT(甲) - 1010 Radix
本题主要难点在于数据格式,除了中间结果外,radix 和二分查找的上下界也必须用long longstoi() 将字符串转为对应radix的数值,一种写法是long long stoi(string temp, long long radix){ int e = 0; long long sum = 0; for (int i = temp.length() - 1; i >= 0; i--) { sum += ctoi(temp[i]) * (l原创 2021-05-15 21:48:04 · 246 阅读 · 2 评论 -
PAT(甲) - 1009 Product of Polynomials
Product:多项式相乘#include <bits/stdc++.h>using namespace std;typedef struct{ int e; double c;} node;vector<node> ve;double cof[1000001];int main(){ // ios::sync_with_stdio(false); int n; cin >> n; int e;原创 2021-05-14 14:54:59 · 58 阅读 · 0 评论 -
PAT(甲) - 1008 Elevator
#include <bits/stdc++.h>using namespace std;int main(){ ios::sync_with_stdio(false); int n; cin >> n; int now = 0; int sum = 0; for (int i = 0; i < n; i++) { int t; cin >> t; int原创 2021-05-14 11:53:53 · 50 阅读 · 0 评论 -
PAT(甲) - 1007 Maximum Subsequence Sum
Tips输出的不是首尾位置,而是该位置上的数值注意最大值为0与全是负数被迫输出为0的区别#include <bits/stdc++.h>using namespace std;int main(){ ios::sync_with_stdio(false); int n; cin >> n; int num[n]; int sum = 0; int maxx = -1; int start, end; i原创 2021-05-14 11:25:07 · 43 阅读 · 0 评论 -
PAT(甲) - 1006 Sign In and Sign Out
#include <bits/stdc++.h>using namespace std;int main(){ // ios::sync_with_stdio(false); int n; cin >> n; string in, out; int tsi = 1e6, tso = -1e6; string name; int h, m, s; int ts; while (n--) {原创 2021-05-14 10:28:41 · 43 阅读 · 0 评论 -
PAT(甲) - 1005 Spell It Right
#include <bits/stdc++.h>using namespace std;int main(){ ios::sync_with_stdio(false); string d2e[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; int sum = 0; char c; while (cin >> c原创 2021-05-12 21:07:56 · 41 阅读 · 0 评论 -
PAT(甲) - 1004 Counting Leaves
使用bfs进行搜索#include <bits/stdc++.h>using namespace std;typedef struct Node{ vector<struct Node *> child;} node;node tree[100];queue<node *> q;int main(){ // ios::sync_with_stdio(false); int n, m; while (cin >&原创 2021-05-12 19:58:26 · 79 阅读 · 0 评论 -
PAT(甲) - 1003 Emergency
使用vector存储地图#include <bits/stdc++.h>using namespace std;int n, m, s, e;int city[500];typedef struct{ int d; int l;} v;vector<v> ve[500];int short_len = 1e6;int count_way = 0;int max_team = 0;int visit[500];void dfs(int cu原创 2021-05-11 19:17:21 · 45 阅读 · 0 评论 -
PAT(甲) - 1002 A+B for Polynomials
注意K是系数非0项的个数,因此a+b=0时,只需输出0即可;而在有些题目中,会要求输出项的个数,则结果为1#include <bits/stdc++.h>using namespace std;int main(){ // ios::sync_with_stdio(false); double c[1001]; memset(c, 0, sizeof(c)); int count = 0; for (int i = 0; i < 2; i+原创 2021-05-11 18:34:55 · 38 阅读 · 0 评论 -
PAT(甲) - 1001 A+B Format
方法一:通过mod不断取出3位数放到数组中#include <bits/stdc++.h>using namespace std;int main(){ int a, b; cin >> a >> b; int sum = a + b; if (sum < 0) cout << '-'; sum = abs(sum); int list[100]; int i = 0;.原创 2021-05-11 18:12:30 · 38 阅读 · 0 评论 -
HDOJ - Is It A Tree?
HDOJ - Is It A Tree? 题解现存问题测试数据的坑正确代码该题主要考察“并查集”这一知识点。现存问题由于HDOJ上该题测试数据较弱,网上的一些答案不用并查集,只检查入度也能AC有的答案放了并查集相关代码,但其实根本没用上(if 判断条件)1、2 的算法能很容易找到反例(一个环+一个树),因为他们没有对连通性进行判断或判断不足普遍没有考虑单点情况(仅单点或一个图+一个点)测试数据的坑HDOJ上的测试数据存在输入不完全的情况,即存在测试用例0 0都没输入就截断了验证原创 2021-04-15 18:12:47 · 126 阅读 · 0 评论