![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法笔记
日出前的千夜
半路出家小萌新
展开
-
PATA 1020 Tree Traversals
题目大意: 给定二叉树的后根序列和中根序列,输出层次序列。 输入: 第一行:节点个数 第二行:后根序列 第三行:中根序列 输出: 层次序列,数字间用一个空格隔开,末尾不允许有多余空格。 代码: #include <stdio.h> #include <queue> using namespace std; const int maxn=40; int post[maxn],in[maxn]; int N; struct biTreeNode{ int data;原创 2021-02-21 17:51:18 · 87 阅读 · 0 评论 -
PATA 1052 链表排序
题目大意 给定一个链表,请按照其关键字升序排序。 输入格式 第一行为一个小于10510^5105的正整数NNN以及头结点地址,其中NNN是 内存中结点的总个数,结点的地址是五位数字。用-1表示NULL。 接下来是NNN行,每行用以下形式表示一个结点: 地址 关键字(−105-10^5−105 ~ 10510^5105) 下一节点地址 输出格式 和输入格式相同,其中NNN表示链表中的结点总数,结点须按照关键字升序排序。 代码 #include <stdio.h> #include <algo原创 2021-02-18 16:06:42 · 103 阅读 · 0 评论 -
PATA 1032 Sharing
给出两条链表的首地址、若干结点的地址、数据、下一个结点的地址,求两条链表的首个共用节点的地址。 因为可以自定义结构体,所以采用的方法是将第一个链表所包括的结点做上标记,然后从前至后扫描第二个链表,找到第一个被标记的结点即为所求。若找不到则返回-1。 若不可以自定义结构体,一种方法是先分别遍历两个链表,计算两者长度的差值。而后分别设立两个指针指向两个链表的首结点。先让指向较长链表首结点的指针后移差值步,使两链表末尾对齐,而后将两指针同时后移,看是否能够指向同一结点。 #include <stdio.h&原创 2021-02-17 00:28:19 · 78 阅读 · 0 评论 -
PATA 1060 Are They Equal
题目 If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123×10^5 with simple chopping. Now given the number of significant digits on machine and two float numbers, you are原创 2021-02-07 00:05:20 · 163 阅读 · 0 评论 -
大整数加法
对于如1000位的整数,普通的变量类型无法进行直接处理。 这里定义了结构体bign,选用数组由低位至高位存储各个数位(因为计算时是由高到低进行),同时用变量len存储数字位数。 使用字符串输入,而后将字符串转化为bign类型。 在进行计算时,采用加法的本质原理,由低位至高位逐位相加,处理进位。 注意点: 将字符串逐位赋值给数组时,char类型为其ASCII编码,需要减去’0’得到数字。 要注意处理当计算到最高位仍有进位的情况。 #include <stdio.h> #include <原创 2021-02-02 23:01:33 · 386 阅读 · 0 评论 -
PATA 1059 Prime Factors
题目: Given any positive integer NNN, you are supposed to find all of its prime factors, and write them in the format N = p1p_1p1^ k1k_1k1×p2p_2p2^ k2k_2k2×⋯×pmp_mpm^kmk_mkm Input Specification: Each input file contains one test case原创 2021-02-02 17:15:22 · 105 阅读 · 0 评论 -
PATB 1013 数素数(埃式筛法)
题目: 令 PiP_iPi表示第 i 个素数。现任给两个正整数 MMM≤NNN≤10^4,请输出 PMP_MPM到 PNP_NPN的所有素数。 输入格式: 输入在一行中给出 MMM 和NNN,其间以空格分隔。 输出格式: 输出从 PMP_MPM到 PNP_NPN的所有素数,每 10 个数字占 1 行,其间以空格分隔,但行末不得有多余空格。 输入样例: 5 27 输出样例: 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89原创 2021-02-01 17:43:39 · 102 阅读 · 0 评论 -
最小公倍数
最小公倍数的求解在最大公约数的基础上进行。当使用辗转相除法得到a和b的最大公约数是d后,a和b的最小公倍数为ab/d。 由于ab在实际计算中可能会溢出,因此更恰当的写法是a/d*b。 题目: 首行输入一个整数n表示样例个数 每个样例为一行数据,其中第一个数字m表示该样例中包含的数据个数,其后是样例数据a1…am 对于每个样例,要求输出a1…am的最小公倍数 代码: #include <stdio.h> int gcd(int a, int b){ !b?a:gcd(b,a%b); }原创 2021-02-01 15:27:45 · 108 阅读 · 0 评论 -
PATA 1069 数字黑洞
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in this m原创 2021-01-31 23:59:52 · 105 阅读 · 0 评论 -
PATA 1093 有几个PAT
题目: 1093 Count PAT’s The string APPAPT contains two PAT’s as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th characters. Now given any string, you are supposed t原创 2021-01-31 16:52:20 · 82 阅读 · 0 评论 -
PATB 1009 说反话
给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。 #include <stdio.h> #include <string.h> #include <iostream> using namespace std; const int MAX_SIZE = 90; int main(){ char str[MAX_SIZE]; //gets(str);//PAT在cpp中限制 //int len=strlen(str); //in原创 2021-01-29 16:17:59 · 74 阅读 · 0 评论 -
PATB 1023 组个最小数
给定数字 0-9 各若干个。你可以以任意顺序排列这些数字,但必须全部使用。目标是使得最后得到的数尽可能小(注意 0 不能做首位)。例如:给定两个 0,两个 1,三个 5,一个 8,我们得到的最小的数就是 10015558。 现给定数字,请编写程序输出能够组成的最小的数。 #include <stdio.h> int main(){ int ans[52];//存储输出结果 int num[10];//存储输入数字 for(int i=0;i<10;i++)原创 2021-01-29 16:14:11 · 63 阅读 · 0 评论 -
PATB 1020 月饼最大收益
#include <stdio.h> #include <algorithm> using namespace std; struct good{ double store; double price_all; double price; }; bool cmp(good a,good b){ return a.price>b.price; } int main(){ int n; double D; double原创 2021-01-29 16:12:47 · 180 阅读 · 0 评论 -
n皇后问题 cpp 递归+回溯
#include <stdio.h> #include <math.h> const int max_n=11; int n,//问题规模(皇后个数) place[max_n],//用来存放当前排列(第1、2、3…列的皇后所在的行) hashTable[max_n]={false};//用来存放第i行是否已经放置了皇后 int cnt=0;//满足条件的情况总数 void generateP(int i){//为第i列分配皇后 if(i==n+1){ c原创 2021-01-28 10:00:19 · 144 阅读 · 1 评论