自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 PAT A1052 卖个萌【c语言】

2022-09-16 16:51:07 163 1

原创 PAT A1050 螺旋矩阵【c语言】

2022-09-15 21:19:49 141

原创 PAT A1048 数字加密【c语言/测试点2/测试点5】

2022-09-15 16:32:13 279

原创 PAT A1044 火星数字【c语言/测试点2/测试点4】

2022-09-12 20:16:28 212

原创 PAT A1039 到底买不买【c语言】

2022-09-11 23:18:59 77

原创 PAT A1035 插入与归并【c语言/带注释】

2022-09-11 20:50:19 175

原创 PAT A1034 有理数四则运算【c语言/测试点3/测试点4】

2022-09-11 16:00:00 191

原创 PAT A1030 完美数列【c语言/测试点4/测试点5】

2022-09-10 22:03:14 279

原创 PAT A1025 反转链表【c语言】

2022-09-10 15:03:24 202

原创 PAT A1024 科学计数法

2022-09-09 21:38:06 60

原创 PAT A1014 福尔摩斯的约会【c语言】

2022-09-08 19:59:42 85

原创 PAT A1010 一元多项式求导【c语言】

2022-09-08 15:57:09 355

原创 PAT A1008 数组元素循环右移问题

2022-09-07 22:58:05 86

原创 PAT A1005 继续(3n+1)猜想【带注释】

2022-09-07 19:48:50 67

原创 PTA A1003 我要通过!(含推导过程)

PTA 例题 A1003

2022-09-07 15:13:23 163

原创 2020同济大学电子与信息工程学院计算机系夏令营机试题目【含题解、注释】

2022-07-01 22:39:10 1956 1

原创 【合并果子问题】c/c++ 优先队列的简单使用

  有n堆果子,每堆果子的质量已知,现在需要把这些果子合并成一堆,但是每次只能把两堆果子合并到一起,同时会消耗与两堆果子质量之和等值的体力。显然,在进行n-1次合并之后,就只剩下一堆了。  合并果子问题可以转化为:已知n个数,寻找一棵树,使得树的所有叶子结点的权值恰好为这n个数,并且使得这棵树的带权路径长度最小。(哈夫曼树问题)输入:32 9 1  应该先合并1和2,再将结果3存入队列,后合并3和9,存入15,此时队列中只有一个数,循环结束,输出结果  理想输出结果:32 9 115

2022-04-15 20:05:37 1367

原创 c/c++ 构建堆、删除堆顶元素、堆排序、在堆中添加元素

堆,是一颗完全二叉树,且任一结点大于等于其左右孩子结点。代码如下,详见注释:输入:1085 55 82 57 68 92 99 98 66 56#include<stdio.h>#include<algorithm>using namespace std;const int maxn = 110;int heap[maxn] = {0};int n;void downAdjust(int low, int high){ int i = low, j = 2

2022-04-15 01:13:31 989

原创 好朋友 并查集问题

规定:互反性:A和B是好朋友,等价于B和A是好朋友;传递性:A和B是好朋友,B和C是好朋友,则A和C也是好朋友;每组中任意两个都得是好朋友,每两组中不存在好朋友。这题明显就是求并查集问题,如果是好朋友,那么他们的根节点相同,因此可以对每次输入的数字进行合并判断,若根节点不同则不在一组,如果根节点相同就在一组,因此根节点的数量就是输出的组数。输入:7 51 22 33 11 45 6代码如下:#include<stdio.h>const int maxn = 11

2022-04-14 21:19:04 409

原创 【c/c++】PAT A1043 Is It a Binary Search Tree

Question:A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with keys less thanthe node’s key.The right subtree of a node contains only nodes with keys gr

2022-04-14 00:30:11 209

原创 【c/c++】PAT A1053 Path of Equal Weight(测试点6 坑)

  Question:Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L.  Now given any weighted tr

2022-04-13 00:26:50 465

原创 【c/c++】PAT A1020 Tree Traversals

Question:Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.题目大概意思:根据后序和中序序列确定一棵树,以

2022-04-11 23:35:27 80

原创 c/c++ 给定数组后按大小顺序构建树,并输出先序序列(完整可运行代码)

先给定想要构建的树的数据://给定数组为:int data[8]={5, 2, 1, 3, 4, 7, 6, 8};所给数据的构建的树,及先序序列如下,完整代码:#include<stdio.h>#include<queue>using namespace std;int data[8]={5,2,1,3,4,7,6,8};struct node{ int data; node* lchild; node* rchild;} Node;node* n

2022-04-11 21:54:02 713

原创 【迷宫问题】 c/c++ 解迷宫问题

题目描述:给定一个N行M列的迷宫,其中’.‘代表空地,可以通行;’#'代表障碍物,无法通行;'S’代表起点;‘T’代表终点;’'S’和’T’这两个位置也是空地,可以通行。在迷宫中可以向上、下、左、右四个不同的方向行走,请你判断从起点出发是否可以走到终点?如果可以,请你计算到达终点最少需要走几步。输入样例:#S#.#.#T##....##.##.##....##....####代码:#include<stdio.h>#include<queue>using name

2022-04-10 16:53:45 1196

原创 c/c++ 解矩阵有多少块(即求连通块的个数)

①首先是BFS,即Breadth First Search,代码如下:#include<stdio.h>#include<queue>using namespace std;const int maxn = 100;int m, n;struct node{ int x, y;} Node;bool inq[maxn][maxn] = {false};int matrix[maxn][maxn] = {0};int X[4]={0, 0, 1, -1};i

2022-04-10 00:07:01 511

原创 c/c++ 从指定数组中取n个数使其和为N的情况下,所取数的平方之和最大

代码如下:#include<stdio.h>#include<vector>using namespace std;int Num; //指定数 int num; //指定个数 int n; //n个数 const int maxn = 10;int a[maxn];int best = 0;vector<int> ans, temp;void solve(int tsum, int tnum, int t2sum, int index)

2022-04-09 17:07:23 610

原创 c/c++ 解背包问题

代码如下:#include<stdio.h>int n, weight;int b_c = 0;const int maxn = 10;int w[maxn] = {0};int c[maxn] = {0};void solve(int t_w, int t_c, int index){ if(index == n){ if(b_c < t_c && t_w<=weight){ b_c = t_c; } return; } s

2022-04-09 01:03:28 717

原创 【c/c++】PAT A1052 Linked List Sorting

有几组测试数据比较坑,就是某些节点可能无法和题目给的首地址的链表连在一起,是独立、游离的,这也算无效节点,如果所有节点都是有效的话,题目就没必要给出一个head变量!注意,当没有满足题意的节点时,要输出"0 -1"!代码如下:#include<stdio.h>#include<algorithm>using namespace std;const int maxn = 100010;struct Node{ int Key; int Address; int Ne

2022-04-08 21:51:04 242

原创 【c/c++】PAT A1032 Sharing

这题表面上是要求后缀相同的首地址,其实只需要求得第一个相同的地址就行,因为如果出现了地址相同的字母,那么后面的字母就一定都相同,他们的路线是相同的。#include<stdio.h>const int maxn = 100010;struct Node{ char data; int next; bool flag;}node[maxn];int main(){ int a1, a2, n; scanf("%d %d %d", &a1, &a2, &n

2022-04-08 20:16:45 234

原创 c\c++ 简单计算器

#include<iostream>#include<stack>#include<queue>#include<map>#include<string>using namespace std;struct node{ double num; char op; bool flag; //true表示操作数,false表示操作符 };string str;stack<node> s; //操作符栈queu

2022-04-07 20:11:28 90

原创 【c/c++】PAT A1060 Are They Equal【详细注释】

本人做的时候,首先是通过几个特殊数来判断代码逻辑有没有问题00000.12300000.012300123.400123.04这四组数能不能正确用科学计数法表示出来,通过这四组数的显示情况不断调整代码逻辑。有点坑的是,第六组测试数据估计是全零,输入为 " 4 0000 0000.0000 " 的时候应该也输出为"YES 0.0000*10^0",这里懒得修改代码逻辑了,直接特判(其实是怕代码改不过来了QAQ,以后有时间一定好好完善逻辑)贴个代码:#include<iostream&

2022-04-05 16:33:19 264

原创 【c/c++】PAT A1059 Prime Factors

数学原理:对于一个正整数n来说,如果它存在[2,n]范围内的质因子,要么这些质因子全部小于等于sqrt(n),要么存在一个大于sqrt(n)的质因子,而其余质因子全部小于等于sqrt(n)。如:对于46=2*23,对于180=2^2*3^2*5。代码如下:#include<stdio.h>#include<math.h>struct facror{ int num, count;}fac[10]; bool isPrime(int n){ if(n<=1) r

2022-04-03 10:41:39 277

原创 【c/c++】PAT B1013 数素数

代码如下,主要问题是注意输出格式。。。#include<stdio.h>#include<math.h>const int maxn = 1e4+1;const int INF = (1<<30)-1;int prime[maxn]={0};bool isPrime(int n){ if(n<=1) return false; int sqr = (int) sqrt(n*1.0); for(int i=2;i<=sqr;i++){ if

2022-04-03 00:20:04 231

原创 【c/c++】PAT A1069 The Black Hole of Numbers 数字黑洞

代码如下,注意按照要求,不够的位数要用零补齐!#include<stdio.h>#include<algorithm>using namespace std;int cmp(int a, int b){ //从大到小排 return a>b; }void to_array(int num[], int n){ //数字转数组 for(int i=0;i<4;i++){ num[i] = n%10; n /= 10; }}i

2022-04-02 21:28:20 238

原创 PAT B1045 快速排序

本题类似于有几个PAT,区别是,之前的题是记录A的左边有几个P,右边有几个T,而本题是记录左边的最大值,右边的最小值。参考柳神的另一种解法,对数组用快排,如果位置不变,大概率就是主元,但是还需要判断是不是左边都小于这个数(判断右边都大于这个数也可以)。测试点2比较坑,要求在没有主元的情况下,输出0\n\n,才能通过,醉了。代码如下:#include<stdio.h>using namespace std;int main(){ int n; scanf("%d", &n)

2022-04-02 17:38:02 80

原创 PAT B1040 有几个PAT

考虑到A在中间,可以通过找到A左边有多少个P,右边有多少个T,相乘即可得到对于任意一个A有多少个PAT的组合,依次遍历即可得到结果,代码如下:#include<stdio.h>#include<string.h>const int maxn = 1e5;int main(){ char str[maxn] = {}; scanf("%s", str); int len = strlen(str); int ln[len], rn[len]; ln[0] = st

2022-04-02 15:19:17 228

原创 【two pointers】两个递增序列的合并

将两个递增序列合并,如果简单地使用二重循环,则时间复杂度为O(n*m),当序列很大时会崩溃,但是采用two pointers算法可以将时间复杂度减少为O(n+m),代码如下#include<stdio.h>int n, m;void merge(int a[], int b[], int c[]){ int x=0, y=0; int i=0; while(x<n&&y<m){ if(a[x]<=b[y]) c[i++] = a[x++];

2022-04-01 21:22:18 149

原创 【二分法】凸多边形外接圆的半径

算法笔记,p134分两种情况讨论,如果(1)外接圆的圆心在多边形内部,这时有,除最长边的其他边角度加起来应该是大于Π;(2)外接圆的圆心在多边形外部或者在多边形上时,除最长边的其他边的角度应该小于等于Π,当且仅当圆心在多边形上时可以取到等号。因此,二分法逼近的规则是对于第一种情况,当其他圆心角大于最大圆心角,应该减小外接圆的半径,反之,应该增大r。对于第二种情况,当所有边的圆心角相加大于2Π时,应该增大外接圆半径r,反之,应该减小r。代码如下:#include<stdio.h>

2022-04-01 17:27:37 514

原创 【木棒切割问题】二分法解木棒切割

给出N根木棒,长度均已知,通过切割它们来得到至少K段相等的木棒(长度为为整数),问这些长度相等的木棒最长能有多长。参见算法笔记,P134#include<stdio.h>int n[3] = {10,24,15};const int maxn = (1>>30)-1;int binary(int K){ int L = (n[0] + n[1] + n[2])/K; //理论的最大长度 int left = 0, right = L; int mid = 0;

2022-03-29 23:20:01 376

原创 【装水问题】二分法渐进求解

有一个侧面看上去是半圆的储水装置,该半圆的半径为R,要求往里面装入高度为h的水,使其在侧面看去的面积S1与半圆面积S2的比例恰好为r,如下图所示。现给定R和r,求高度h。代码如下:#include<stdio.h>#include<math.h>using namespace std;const double min = 1e-5;const double pi = acos(-1.0);double f(double h, double R){ double x

2022-03-29 20:57:39 298

空空如也

空空如也

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

TA关注的人

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