数据结构与算法
守望_shouwang
1
展开
-
字符串问题
以.结尾的字符串,字符串是数字和字母,若是数字转化为*,然后输出*的数量和转化后的字符串#include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>int main() { char c; int numberTotal=0; while ((c = getchar()) != '.'){ if(c>='0'&.原创 2020-12-12 21:21:59 · 209 阅读 · 0 评论 -
完全数
如果正整数M等于他的全部因子(不包含M本身)之和,则M叫做完全数,例如6=1+2+3,28=1+2+7+14,求1000以内的完全数#include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>#include<stdio.h>#include<stdlib.h>int main() { for(int i=1;i<=30;i原创 2020-12-04 14:54:21 · 451 阅读 · 1 评论 -
倒置数
输入:135输出:531#include<stdio.h>#include<string.h>#include<stdlib.h>int main() { int x,y=0; scanf("%d",&x); while(x!=0){ y=y*10+x%10; x=x/10; } printf("%d",y);}原创 2020-11-20 11:30:57 · 396 阅读 · 1 评论 -
甲乙丙分钱游戏
甲乙丙三人共有24元钱,先由甲分钱给乙、丙两人,所分给的数与各自已有数相同;接着由乙分给甲、丙,分法相同;再接着由丙分给甲、乙,分法相同。经三次分钱之后,每个人的钱数一样多,求原来每个人的钱数分别是多少?#include<stdio.h>#include<string.h>#include<stdlib.h>int main () { int a=8,b=8,c=8; a=a/2; b=b/2; c=a+b+c; a=a原创 2020-11-17 21:45:55 · 5538 阅读 · 4 评论 -
猴子吃桃问题
猴子吃桃问题。猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃一个。以后早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,只剩下一个桃子了。求第一天共摘多少个桃子。 分析:10 1;9 4;8 10;以此类推,n=n*2+2,循环九次。#include<stdio.h>#include<string.h>#include<stdlib.h>int main () { ...原创 2020-11-15 21:04:09 · 197 阅读 · 0 评论 -
小写字母转换成大写字母,然后输出到一个磁盘文件中保存。
#include<stdio.h>#include<string.h>#include<stdlib.h>int main () { FILE *fp; char str[100]; int i=0; if ((fp=fopen("d:\\a1.txt","w"))==NULL) { printf("can not open file\n"); exit(0); } gets(str).原创 2020-11-03 11:13:45 · 1019 阅读 · 0 评论 -
哈夫曼树
注意:哈夫曼树并不唯一,但带权路径长度一定是相同的。第一部分;由给定结点构造哈夫曼树(1)8个结点的权值大小如下:(2)从19,21,2,3,6,7,10,32中选择两个权小结点。选中2,3。同时算出这两个结点的和5。(3)从19,21,6,7,10,32,5中选出两个权小结点。选中5,6。同时计算出它们的和11。(4)从19,21,7,10,32,11中选出两个权小结点。选中7,10。同时计算出它们的和17。(BTW:这时选出的两个数字都不是已经构造好的二叉树里面的..原创 2020-10-31 23:17:01 · 4402 阅读 · 0 评论 -
删除链表中最小值的点
LinkList Select_Min(LinkList &L) { LNode *pre=L,*p=L->next; LNode *minPre=pre,*minP=p; while(p!=NULL){ if(p->data<minP->data){ minP=p; minPre=pre; } pre=p; p=p->next; .原创 2020-10-28 22:51:48 · 181 阅读 · 0 评论 -
N个人围成圈报数问题
#include<stdio.h>int a[10]= {0};int main() { int n,m; int s=0; //s表示出圈人数 int c=0; //c表示人的编号 int k=0; //k表示报的数 scanf("%d%d",&n,&m); while(s!=n) { c++; if(c>n) c=1; if(a[c]==0) { .原创 2020-10-28 21:02:58 · 335 阅读 · 0 评论 -
查找链表中最小值的点
//查找链表中最小值的点LinkList Select_Min(LinkList &L) { LNode *q,*p; p=L; q=L->next; int dd; if(q) dd=q->data; while(q->next!=NULL) { if(dd>q->next->data) { dd=q->data; p=q->next;.原创 2020-10-27 11:05:14 · 3114 阅读 · 1 评论 -
判断该链表中第二项起的每个元素值是否等于其序号的平方减去其前驱的值
//判断该链表中第二项起的每个元素值是否等于其序号的平方减去其前驱的值bool Judge(LinkList &L){ LNode *p,*pre; p=L->next->next; pre=L->next; int i=2; while(p!=NULL){ if(p->data=i*i-pre->data){ i++; pre=p; p=.原创 2020-10-24 20:45:13 · 234 阅读 · 0 评论 -
结构体排序(选择排序)
#include<algorithm>#include<stdio.h>#include<string.h>struct student { char number[10]; int score;} stu;void SelectSort(student s[],int n){ struct student x; for(int i=0;i<n-1;i++){ int minN=i; for.原创 2020-10-19 11:43:21 · 1209 阅读 · 0 评论 -
求素数
#include<stdio.h>#include<math.h>#include<string.h>#include<iostream>#include<algorithm>#include<stdlib.h>int main() { int sum=0,flag; for(int i=2; i<=1000; i++){ flag=0; for(int j=2;j&l.原创 2020-10-14 16:53:04 · 69 阅读 · 0 评论 -
统计字符串中字母数字空格及其他字符
#include<algorithm>#include<stdio.h>#include<string.h>int main(){ char a[1000]; int numTotal=0,letterTotal=0,spaceTotal=0,otherTotal=0; gets(a); for(int i=0;i<strlen(a);i++){ if(a[i]>='0'&&a[i]<.原创 2020-10-14 12:04:09 · 338 阅读 · 0 评论 -
结构体排序
#include<algorithm>#include<stdio.h>#include<string.h>struct student{ char name[10]; int age;}stu;int cmp(student a,student b){ return a.age>b.age;}int main() { student s[3]; for(int i=0;i<3;i++){ .原创 2020-10-11 21:12:39 · 100 阅读 · 0 评论 -
如何快速找出未知长度单链表的中间结点
快慢指针的思路,设置两个指针p、mid都指向单链表的头结点。其中p的移动速度是mid的2倍,当p指向末尾结点时,mid刚好就在中间了status getMidNode(LinkList L){ LinkList p = L, mid = L; while(p){ if(p->next == NULL){ break; } p = p->next->next; mid = mid-.原创 2020-10-04 12:05:51 · 228 阅读 · 0 评论 -
奇数前半部分,偶数后半部分
一个随机数组,把所有的奇数放在前半部分,所有的偶数放在后半部分。#include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>#include<stdio.h>#include<stdlib.h>void swapNum(int &i,int &j){ int temp=i; i=j; j=temp;原创 2020-10-01 10:46:16 · 197 阅读 · 0 评论 -
排序
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>//直接插入排序void InsertSort(int a[],int n) { int i,j,temp; for(i=1; i<n; i++) { if(a[i]<a[i-1]) { temp=a[i]; for(.原创 2020-09-05 21:56:17 · 741 阅读 · 0 评论 -
二分查找
二分查找#include<stdio.h>#include<stdlib.h>#include<string.h>int Binary_Search(SqlList L,int key) { int low=0,high=L.TableLen-1,mid; while(low<high) { mid=(low+high)/2; if(L.elem[mid]==key) { retu.原创 2020-08-27 21:50:44 · 89 阅读 · 0 评论 -
二叉排序树
#include<stdio.h>#include<stdlib.h>#include<string.h>typedef struct BSTNode { int key; struct BSTNode *lchild,*rchild;} BSTNode,*BSTree;//二叉排序树中查找值为key的结点BSTNode *BST_Search(BSTree T,int key){ while(T!=NULL&&T-&.原创 2020-08-23 18:36:16 · 60 阅读 · 0 评论 -
二叉树两结点最近公共祖先
int Comm_Ancestor(SqTree T,int i,int j){ if(T[i]!='#'&&T[j]!='#'){ while(i!=j){ if(i>j) i=i/2; else j=j/2; } return T[i]; }}原创 2020-08-22 16:12:49 · 138 阅读 · 0 评论 -
二叉树
创建#include<stdio.h>#include<stdlib.h>#include<string.h>typedef struct BiTNode { int data; struct BiTNode *lchild,*rchild;} BiTNode,*BiTree;int main() { BiTNode root=NULL; root=(BiTree)malloc(sizeof(BiTNode)));原创 2020-08-22 16:06:42 · 66 阅读 · 0 评论 -
括号匹配问题
#include<stdio.h>#include<stdlib.h>//括号匹配问题bool bracketCheck(char str[],int length) { SqStack s; InitStack(s); for(int i=0; i<length; i++) { //遍历字符串,左括号压入栈 if(str[i]=='('||str[i]=='['||str[i]=='{') { .原创 2020-08-15 18:27:04 · 134 阅读 · 0 评论 -
栈
#include<stdio.h>#include<stdlib.h>#define maxSize 100typedef struct{ int data[maxSize]; int top;}SqStack;//初始化栈void InitStack(SqStack &s){ s.top=-1;}//判断栈是否为空bool StackEmpty(SqStack &s){ if(s.top==-1){ .原创 2020-08-13 17:27:28 · 66 阅读 · 0 评论 -
双链表
#include<stdio.h>#include<stdlib.h>typedef struct DNode{ int data; struct DNode *prior,*next;}DNode,*DLinklist;bool InitDLinkList(DLinklist &L){ L=(DNode *)malloc(sizeof(DNode)); if(L==NULL){ return false; }.原创 2020-08-13 16:46:56 · 74 阅读 · 0 评论 -
寻找两个单链表的第一个公共结点
LinkList Search_1stNode(LinkList &L1,LinkList &L2){ int len1=length(L1),len2=length(L2); LinkList longList,shortList; int dist; if(len1>len2){ longList=L1->next; shortList=L2->next; dist=len1-len...原创 2020-08-13 15:54:07 · 148 阅读 · 0 评论 -
反转链表
一 递归反转//递归反转链表void reverseList_2(LinkList &L){ if(L->next!=NULL){ reverseList_2(L->next); } printf("%d ",L->data);}示意图:原创 2020-08-13 15:26:34 · 81 阅读 · 0 评论 -
线性表之单链表
一 带头结点的构建和插入#include<stdio.h>#include<stdlib.h>struct LNode{ int data; struct LNode *next;}LNode,*LinkList;bool InitList(LinkList &L){ L=(LNode *)malloc(sizeof(LNode)); if(L==NULL){ return false; } L-.原创 2020-08-02 22:43:11 · 91 阅读 · 0 评论 -
顺序表的增删改查
#include<stdio.h>#include<stdlib.h>#define MaxSize 10typedef struct{ int data[MaxSize]; int length;}SqList;void InitList(SqList &L){ for(int i=0;i<L.length;i++){ L.data[i]=i; }}bool ListInsert(SqList &.原创 2020-07-31 17:53:08 · 202 阅读 · 0 评论 -
线性表之顺序表
静态实现:#include<stdio.h>#define MaxSize 10typedef struct{ int data[MaxSize]; int length;}SqList;void InitList(SqList &L){ //&L带回来值 for(int i=0;i<MaxSize;i++){ L.data[i]=0; } L.length=0; //必须初始化为0}i.原创 2020-07-29 22:10:33 · 106 阅读 · 0 评论 -
动态规划
动态规划动态规划相信大家都知道,动态规划算法也是新手在刚接触算法设计时很苦恼的问题,有时候觉得难以理解,但是真正理解之后,就会觉得动态规划其实并没有想象中那么难。网上也有很多关于讲解动态规划的文章,大多都是叙述概念,讲解原理,让人觉得晦涩难懂,即使一时间看懂了,发现当自己做题的时候又会觉得无所适从。我觉得,理解算法最重要的还是在于练习,只有通过自己练习,才可以更快地提升。话不多说,接下来,下面...转载 2017-11-21 21:13:40 · 241 阅读 · 0 评论 -
Unlucky Number I
Unlucky Number IDescription Some positive integers’ representationsonly consist of the unlucky digits 4 and 7. We call them Unlucky Numbers. Forexample, numbers 74, 774, 7 are unlucky while 2,...原创 2016-05-09 21:44:39 · 272 阅读 · 0 评论 -
Jam's math problem(HDU5615)
Jam's math problemProblem DescriptionJam has a math problem. He just learned factorization.He is trying to factorize ax2+bx+c into the form of pqx2+(qk+mp)x+km=(px+k)(qx+m).He could only solve t...原创 2016-02-06 17:12:10 · 298 阅读 · 0 评论 -
DNA和RNA的匹配(hdu5590)
题:DescriptionAfter getting scores in begins to work with biological questions.Now he give you a simple biological questions:he gives you a sequence and a sequence,then he asks you whether ...原创 2016-01-29 10:19:31 · 736 阅读 · 0 评论 -
处理数字
H - NumbersDescriptionThere are n numbers ${A}_{1},{A}_{2}....{A}_{n}$,your task is to check whether there exists there different positive integers i, j, k ($1\leq i , j , k \leq n$) such that ${A...原创 2016-01-23 19:31:18 · 297 阅读 · 0 评论 -
泉水(深搜)
泉水Description Leyni是一个地址调查员,有一天在他调查的地方突然出现个泉眼。由于当地的地势不均匀,有高有低,他觉得如果这个泉眼不断的向外溶出水来,这意味着这里在不久的将来将会一个小湖。水往低处流,凡是比泉眼地势低或者等于的地方都会被水淹没,地势高的地方水不会越过。而且又因为泉水比较弱,当所有地势低的地方被淹没后,水位将不会上涨,一直定在跟泉眼一样的水位上。 ...原创 2016-01-26 15:06:10 · 768 阅读 · 0 评论 -
The mook jong (HDU 5366)
The mook jongDescription![](../../data/images/C613-1001-1.jpg) ZJiaQ want to become a strong man, so he decided to play the mook jong。ZJiaQ want to put some mook jongs in his backyard. H原创 2016-02-21 22:18:33 · 302 阅读 · 0 评论 -
Presents(CodeForces 136A)
PresentsDescriptionLittle Petya very much likes gifts. Recently he has received a new laptop as a New Year gift from his mother. He immediately decided to give it to somebody else as what ca原创 2016-02-21 16:58:58 · 837 阅读 · 0 评论 -
二分查找
思路:逐渐缩短范围代码:#include#includeusing namespace std;int binSearch(int elem[],int n,int key){ int low=0,high=n-1; while(low { int mid=(low+high)/2; if(key==el原创 2016-11-10 21:26:41 · 175 阅读 · 0 评论 -
wyh2000 and a string problem(HDU5284)
D - wyh2000 and a string problemTime Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uSubmit Status Practice HDU 5284DescriptionYoung theoretical computer scientist wyh2...原创 2016-01-22 17:33:09 · 347 阅读 · 0 评论