算法起步学习题解(题目杂选)
算法学习初期自己遇到的一些问题,当时学的可能比较杂乱,东搞一点西搞一点,所以将这些杂题放在了这里面
lndksk
用最简练的话分享最有价值的内容
展开
-
紫书1.1学习记录
1.1一、主要语法printf("%.1f") .后面代表输出数据的小数点后几位。sqrt(3)代表3的算数平方根二、注意问题浮点数/浮点数=浮点数整数/整数=整数整数与浮点数同时运算时先将整数变为浮点数调用数学函数需要加头文件math.h,在编译时链接数学库。...原创 2021-09-10 23:53:26 · 69 阅读 · 0 评论 -
紫书1.3顺序结构程序设计
语法%03d,不满三位前面补0两种交换变量方法取另一变量的更实用算法竞赛目的是按照题目解决问题,不是比谁写的程序看上去高级。原创 2021-09-12 00:27:09 · 62 阅读 · 0 评论 -
测试printf实验
%d最多记录10位,超过十位变复数,再大变成正数(原因不清)。%lf能记录十位以上,111111111*11111能够计算(具体上线没测试出来,未知)。%f也能记录十位以上,(同lf暂未测试出上线)。在vs中(sqrt-10),用%d输出结果为0;用%f及%lf输出-nan(ind),系统没有报错//查询结果nan 是 "not a number" 的缩写, 即计算结果 不是个 数。例如: 32 位 数 实际指数 128 ,数符 1 或 0,指数域 二进制 1111 1111。原创 2021-09-14 23:02:30 · 162 阅读 · 0 评论 -
紫书1.2变量及其输入
一.算法竞赛规范化1输入前不要加入提示信息2不要输出后暂停程序、3不要尝试画图访问网络等无关任务二.语法部分math.h中acos代表反余弦函数acos(1.0)表示pi的值,用asin(0)不能达到相同效果...原创 2021-09-11 13:03:29 · 55 阅读 · 0 评论 -
开灯问题代码
#include<stdio.h>#define maxn 1010#include<string.h>int main(void){ int a[maxn]; memset(a, 0, sizeof(a));//将数组元素全部替换为零 int n, k,i,first=1; scanf_s("%d %d", &n, &k); for (int i = 1; i <= k; i++) ...原创 2021-09-17 22:45:53 · 110 阅读 · 0 评论 -
紫书1.4分支结构程序设计
鸡兔同笼问题:满足条件n%2=1||a<0||b<0;//n为腿的数目,ab分别为鸡和兔的数目对取值范围的考察,细节很关键。排序三整数,三种排序方法,通过if语句分别将ab,ac,bc比较进行排序的方法简便||代表或,只要两边有一成立,则为真。...原创 2021-09-12 22:45:47 · 86 阅读 · 0 评论 -
通过竖式问题了解字符本质以及一些语法规则
char s[20] buf[99];sprintf(buf,"%d%d%d%d%d",abc,de,x,y,z);用sprintf像数组buf中输入字符strlen(buf)计算buf数组的字符个数strchr(s,buf[i])用buf[i]向s中逐一检索,如果发现相同,返回该字符的地址%5d占5个空,不足前面用空格补齐,%05d占5个空,不足前面用0补齐。...原创 2021-09-18 14:13:46 · 53 阅读 · 0 评论 -
阶乘之和(计时器引入)和最简单的文件输入输出
阶乘可以把程序改成每步取模,防止溢出计时器的代码:#include<time.h>printf("time used=%.2f\n",(double)clock()/CLOCKS_PER_SEC);记录程序运行的时间最简单的文本输入输出用freopen("文件详细路径",“r”,stdin);freopen("文件详细路径","w",stdout);在算法竞赛中如果能够进行重定向并用文件输入输出用此合适如果用标准输入输出可以在前面添加#define LOC原创 2021-09-16 21:39:53 · 68 阅读 · 0 评论 -
输入输出实验
B1得到预期结果B2得到预期结果B3得到预期结果B411s s1 -858993460B42B41s1 -858993460思考问题两根\\会输出一根\,用%%d会输出%d原创 2021-09-14 23:28:55 · 60 阅读 · 0 评论 -
黑马c++学习笔记(持续更新)
C++面向对象三大特性封装继承多态C++初学者常用创建模板#include <iostream>Using namespace std;Int main(void){System(“pause”)Return 0;}Cout<<Cin>>strSwich 不能判断范围。Rand()%100生成0-99的随机数(伪随机数)需要一个随机种子 srand((unsigned int)time(null)); .原创 2021-11-01 16:21:27 · 618 阅读 · 0 评论 -
c语言对齐与八进制十六进制输出
/*所有的输入用d来表示*/如果要进行对齐用%数字d表示向右对齐%-数字d表示向左对齐用%o表示输出八进制 %x表示十六进制输出%0数字d补零到多少位scanf("x = %d ,y = %d",&z,&k);输出时要用 x = number, x = number;...原创 2021-10-05 19:05:22 · 287 阅读 · 0 评论 -
文件重定向方式
# include<stdio.h># include<time.h># include<stdlib.h>int main(void){ freopen("C:\\Users\\22703\\Desktop\\in.txt(文件所在路径)", "r", stdin); freopen("C:\\Users\\22703\\Desktop\\out.txt(文件所在路径)", "w", stdout); int x; int y;...原创 2021-10-26 17:56:26 · 163 阅读 · 0 评论 -
set/multset容器学习(用于复习语法)
# include<iostream># include<set># include<string>using namespace std;//利用仿函数修改排序方法,没有剖析底层原理class Mycompare{public: bool operator()(int v1, int v2) const { return v1 > v2; }};class Person{public: Person(string name,.原创 2021-11-03 20:20:27 · 120 阅读 · 0 评论 -
vector容器(复习用)
# include<iostream>using namespace std;# include<vector>void printVector(vector<int>&v);//容器构造void test01(){ //无参构造 vector<int>v1; for (int i = 0; i < 10; i++) { v1.push_back(i); } printVector(v1); vector<.原创 2021-11-01 18:39:36 · 82 阅读 · 0 评论 -
deque容器(复习用)
# include<iostream># include<deque># include<algorithm>//标准算法的头文件using namespace std;void printDeque(const deque<int>& d){ for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)//容器中的数据为只读 { cout &.原创 2021-11-04 19:38:12 · 57 阅读 · 0 评论 -
map函数的一些语法(复习用)
# include<iostream># include<map>using namespace std;class Mycompare{public: bool operator()(int v1, int v2) const { return v1 > v2; }};void printmap(map<int,int>&m){ for (map<int, int>::iterator it = m.begin(.原创 2021-11-03 21:14:24 · 344 阅读 · 0 评论 -
uva 10763习题5-4
用一个multiset轻松解决# include<iostream># include<set>using namespace std;int main(void){ int n; while ((cin >> n) && n != 0) { multiset<int>a; multiset<int>b; while (n--) {...原创 2021-11-13 09:04:10 · 367 阅读 · 0 评论 -
uva 12096集合栈计算机(紫书题目)
看了很多解析,都没大有用,说了跟没说一样,甚至有可能对理解本题产生错误理解,作为一个STL初学者深受其害,绕了好久才想通了。所以以我现在萌新的视角和理解详细剖析一下这个题。首先说一下困扰我的核心,set容器不会存放多个set,set中存放的全是用map,vector容器相互结合得到的数字(set<int>),(set中全是数字,set中全是数字)把所有容器量化为数进行存储,每一个新出现的都会被赋予一个数字,用数字表现容器。然后我认为比较关键的是,map和vector的结合原创 2021-11-05 21:20:17 · 200 阅读 · 0 评论 -
关系运算符重载
# include <iostream>using namespace std;//重载关系运算符,不知道如何对比自定义的数据类型class person{public: person (string name,int age) { m_name = name; m_age = age; } string m_name; int m_age; //重载关系运算符 bool operator==(person& p) { if (m_name =.原创 2021-10-20 16:36:12 · 142 阅读 · 0 评论 -
黑马c++左移运算符重载
/*2021年10月18日20:30:11* 尽量设置private变量* 使用友元让全局函数访问里面的私有内容* */# include<iostream>using namespace std;//左移运算符重载class person{ void test01(); friend ostream& operator<<(ostream& p, person& x);public: person() { m_a = 1.原创 2021-10-19 18:06:31 · 98 阅读 · 0 评论 -
!EOF简单说明,常用来结束while循环
通常!EOF用语法while(scanf("%d",&a)!=EOF)结束while循环点ctrl+z再点enter结束循环vs需要连续三次ctrl+z在提交oj时用来结束while循环很好用原创 2021-10-15 13:41:33 · 3044 阅读 · 0 评论 -
链表(尾插法实现链表)
/** 链表*/#include<stdio.h>#include<malloc.h>#include<stdlib.h>typedef struct NODE{ struct NODE*pnext; int data;}NODE, *PNODE;PNODE create_list1(void);//头插法PNODE create_list2(void);//尾插法bool is_empty(PNODE p);int lenth(PNOD.原创 2021-10-10 11:00:31 · 76 阅读 · 0 评论 -
string容器的内容(复习用)
# include<iostream>using namespace std;# include<string>void test01(){ string str1 = "abcdefgde";//从0开始索引,没有返回-1 int pos = str1.find("df"); cout << pos; //rfind从右往左查找 //find从左往右查 pos=str1.rfind("de"); cout << pos;}.原创 2021-11-01 18:38:17 · 64 阅读 · 0 评论 -
DNA序列uva1368(初学者思路)
# include<stdio.h>#define max 55#define MAX 1100struct ST{ int numa; int numt; int numc; int numg;}st[1010];int main(void){ int t=0; char z; scanf("%d", &t); while(t--) { for (int n = 0; n < 1005; n++) { st[n].numa = 0;.原创 2021-10-02 14:47:27 · 71 阅读 · 0 评论 -
黑马重载赋值运算符
# include <iostream>using namespace std;//深浅拷贝问题,赋值运算符重载//class person{public: person(int age) { m_age = new int(age);//返回int*,开辟在堆区 } ~person() { if (m_age != NULL) { delete m_age; m_age = NULL; } cout << "调用了析构函数".原创 2021-10-20 16:12:41 · 86 阅读 · 0 评论 -
黑马c++递增运算符重载
# include <iostream>using namespace std;//递增运算符重载//前递增和后递增具有区别class myinteger{friend ostream& operator<<(ostream& cout, myinteger &x);public: myinteger() { m_num = 0; } //重载++运算符 //重载分为两种,前置递增或者后置递增 //重载前置++运算符 myi.原创 2021-10-19 18:31:56 · 210 阅读 · 0 评论 -
uva221紫书
字母都是比着紫书写的,方便进行查阅。这道题debug了很久,最后是函数for循环搞错了,把return ture也纳入了for循环而且默认了b中的数据是int型,浪费了很多时间//一个重要的思想,讲题目中所给的数据离散化,我暂且理解其为将不固定的题目中给出的数据//通过自己的操作无论出现什么情况,都将其转化为能够被记录的数据//此题就是将题目中给出的数据进行区间标记# include<iostream># include<algorithm>using nam原创 2021-11-11 20:36:32 · 58 阅读 · 0 评论 -
uva10763习题5-4
用multiset进行存储,可以包涵重复数据# include<iostream># include<set>using namespace std;int main(void){ int n; while ((cin >> n) && n != 0) { multiset<int>a; multiset<int>b; while (n--)...原创 2021-11-13 09:05:46 · 210 阅读 · 0 评论 -
紫书uva 814传输代理的交互
# include<iostream># include<map># include<set># include<string># include<vector>using namespace std;void parse_add(const string& s, string& user, string& mta)//对数据进行处理的函数{ int k = s.find('@'); user ...原创 2021-11-10 19:26:30 · 82 阅读 · 0 评论 -
01-复杂度2 Maximum Subsequence Sum (25 分)
#include <iostream>#include <vector>using namespace std;int main() {int n;scanf("%d", &n);vector<int> v(n);int leftindex = 0, rightindex = n - 1, sum = -1, temp = 0, tempindex = 0;for (int i = 0; i < n; i++) {scanf("...原创 2021-12-04 12:30:09 · 84 阅读 · 0 评论 -
UVA815紫书例题FLOOD
这个题思路还是比较好想的,1主要是输出坑了我一段时间,2在使用sort函数的时候没有注意自己是从1开始计数的时候也出现了bug。# include<iostream># include<algorithm># include<string.h>using namespace std;int a[10000];int main(void){ int m, n; double flood; int high; int kase = 0; w原创 2021-11-15 11:42:34 · 607 阅读 · 0 评论 -
1003 pta乙级(我要通过)
/*pta甲乙上的题感觉都很巧妙,这个题最大的问题在于读题,最核心的公式是(t-p-1)*(p)==(x.length()-t-1),中间(多余的)*前面=后面这段代码不难理解,但用map容器统计字母的思路非常值得学习,非常巧妙*/# include<iostream># include<string># include<map>using namespace std;int main(void){ int n,t=0,p=0; cin ...原创 2021-11-17 18:06:54 · 207 阅读 · 0 评论 -
02-线性结构3 Reversing Linked List (25 分)
# include<iostream>using namespace std;int a[100005];int b[100005];int data1[100005];int main(void){ int p0, num, x; scanf("%d %d %d", &p0, &num, &x); for (int i = 0; i < num; i++) { int node, data0, next; scanf("%d %d %.原创 2021-12-04 12:32:42 · 488 阅读 · 0 评论 -
03-树3 Tree Traversals Again (25 分)
这个题算是02的简化版,会了02这个题写起来很轻松,下面附上ac代码# include<stdio.h># include<queue>using namespace std;struct tree{ int number; int left; int right;}t[11];int n;int a[10];int inputtree(){ char le,ri; scanf("%d", &n); for (int i = 0; i &原创 2021-11-22 16:33:45 · 160 阅读 · 0 评论 -
01-复杂度1 最大子列和问题 (20 分)
# include<iostream>using namespace std;int a[100000+10];int MaxUbseqSum(int A[],int N){ int ThisSum, maxsum; int i; ThisSum = maxsum = 0; for (int i = 0;i < N;i++) { ThisSum += A[i]; if (ThisSum > maxsum)...原创 2021-12-04 12:29:27 · 429 阅读 · 0 评论 -
05-树7 堆中的路径 (25 分)
# include<iostream># include<algorithm># include<stack>using namespace std;struct Pile{ int a[2000]; int size; int maxsize;}pile;void insert(int x,Pile *pile){ pile->a[pile->size++] = x; int num = pile->size-1; for.原创 2021-12-04 12:44:16 · 194 阅读 · 0 评论 -
03-树2 List Leaves (25 分)
# include<stdio.h># include<queue>using namespace std;struct tree{ int number; int left; int right;}t[11];int n;int a[10];int inputtree(){ char le,ri; scanf("%d", &n); for (int i = 0; i < n; i++) { getchar(); scanf(.原创 2021-12-04 12:35:00 · 629 阅读 · 0 评论 -
uva 230紫书图书管理系统(已ac)
/*2021年11月14日16:27:32使用set和map,set记录书籍,并通过运算符重载对set中的数据进行排序,map记录书籍状态应该是比较常用的模拟算法注意其中学习到的知识点substr截取,struct结构体中的运算符重载,通过图记录状态,set记录并且遍历书籍*/# include<iostream># include<map># include<set># include<algorithm># include <.原创 2021-11-14 17:39:09 · 999 阅读 · 0 评论 -
AcWing 2005. 马蹄铁题解
题目描述尽管奶牛贝茜发现每个平衡括号字符串都很美观,但她特别喜欢被她称为“完全”平衡的括号字符串----一个由 ( 构成的字符串后接一个长度相同的 ) 构成的字符串。例如:(((())))有一天,当贝茜穿过牛棚时,她发现地面上有一个 N×N 的马蹄铁矩阵。每个马蹄铁的方向都看上去像 ( 或 )。从矩阵的左上角开始,贝茜希望四处走动以拾起马蹄铁,使得她捡起的马蹄铁按顺序构成的括号字符串是完全平衡的。请计算她能得到的最长完全平衡括号字符串的长度。每一步中,贝茜可以沿上下左右四个方向移原创 2022-01-16 12:06:38 · 223 阅读 · 0 评论 -
04-树6 Complete Binary Search Tree (30 分)
# include<iostream># include<vector># include<algorithm># include<cmath>using namespace std;vector<int>a, b;void solve(int first,int end,int item){ if (first > end) { return; } int N = end - first+1; int h = .原创 2021-12-04 12:43:26 · 368 阅读 · 0 评论