摘要:其实可以看作一个图,源点是5,5的邻边分别是6,4,10(三种走法),利用BFS,最先遍历17为最少时间。 #include<iostream> #include<queue> #include<vector> using namespace std; struct node{ int data;
阅读全文
摘要:#include<stdio.h> #include<string.h> struct node{ char a[100]; char b[100]; }; typedef struct node dir; dir s[100000]; int main(){ char A[200]; int to
阅读全文
摘要:构建一个map,还是查找问题。 麻烦点就是要 分解输入的过程 #include<iostream> #include<string> #include<map> using namespace std; int main(){ string a,b; map<string,string> m; wh
阅读全文
摘要:简单的结构体查找。要注意C语言中文字符占多个字节,输入输出中文要用%s。 #include<stdio.h> #include<string.h> struct node{ char n[1001]; char name[200]; char x[4]; int age; }; typedef st
阅读全文
摘要:用map做查找就行了。 #include<iostream> #include<string> #include<map> using namespace std; struct node{ string name; string x; int age; }; typedef struct node
阅读全文
摘要:用(优先队列)小根堆,先构建哈夫曼树,然后在递归遍历输出WPL。 #include<iostream> #include<queue> using namespace std; struct node{ int data; struct node* left; struct node* right;
阅读全文
摘要:C的思路就是,先把用数据录入,然后按要求选出最大(用选择排序是最简单的),最后输出。 #include<stdio.h> #include<math.h> struct node{ int a; int b; long sum; }; typedef struct node num; int cha
阅读全文
摘要:这题难点就是什么是复数的模了吧。 然后C++写个优先队列(大根堆)+操作符重载就行了。 #include<iostream> #include<string> #include<queue> #include<math.h> using namespace std; struct node{ int
阅读全文
摘要:先把BST建立起,然后递归遍历判断树就好了。 #include<iostream> #include<string> using namespace std; struct node{ char data; struct node* left; struct node* right; }; type
阅读全文
摘要:考二叉搜索树的插入。 #include<iostream> using namespace std; struct node{ int data; struct node* left; struct node* right; }; typedef struct node tree; int main
阅读全文
摘要:这个题目思路其实就是先序遍历的变形。 相当于沿着先序遍历的顺序跟着构建二叉树就行。然后中序遍历这个树。 #include<iostream> #include<string> using namespace std; struct tnode{ char data; struct tnode* le
阅读全文
摘要:思路是先构造出树,然后在后序遍历整个树。 #include<iostream> #include<string> using namespace std; struct Tnode{ char data; struct Tnode* left; struct Tnode* right; }; typ
阅读全文
摘要:递归判断当前节点和n的关系就好了。如果小于等于n那就是存在。 #include<iostream> using namespace std; int count(int i,int n){ if(i>n) return 0; return count(2*i,n)+ count(2*i+1,n)+1
阅读全文
摘要:#include<iostream> using namespace std; int compute(int n){ if(n==1) return 1; if(n==0) return 0; return compute(n-1)+ compute(n-2); } int main(){ int
阅读全文
摘要:递归C和C++一样,就写个C++了。 #include<iostream> using namespace std; void move(int n,char a,char b,char c){ if(n<=0) return; move(n-1,a,c,b); cout << n << ":"<<
阅读全文
摘要:递归一下。 #include<iostream> using namespace std; long compute(int n){ if(n==1) return 1; return n* compute(n-1); } int main(){ int n; while( cin >> n){ c
阅读全文
摘要:、 这是目前阶段做的最难最吃力的题目。调试了一遍又一遍去看逻辑上出现的各种问题。。。 #include<iostream> #include<string> #include<stack> #include<map> using namespace std; int main(){ map<char
阅读全文
摘要:#include<stdio.h> int main(){ char s[101]; while(scanf("%s",s)!=EOF){ printf("%s\n",s); char tem[101]; int a[101]={0}; int top=0; int i=0; for(;s[i]!=
阅读全文
摘要:用一个vector来记录每个位置打印打印情况, 结果:
阅读全文
摘要:写个数组当作栈用就行了。 bool isValid(char* s) { int t[100000]={0}; int top=0; int flag=1; for(int i=0;i<strlen(s);i++){ if(s[i]=='('){ t[top++]=1; }else if(s[i]=
阅读全文
摘要:括号匹配用栈是解决是最简单那的。 遇到左括号就入栈。遇到右括号就出栈,然后看是否匹配。这里再用一个map把括号数字化会更简单。 class Solution { public: bool isValid(string s) { map<char,int> m={ {'(',1},{')',-1},
阅读全文
摘要:用数组倒序输出就行了。 #include<stdio.h> #include<stdlib.h> int main(){ int n; while(scanf("%d",&n)!=EOF){ long* a=(long*)malloc(sizeof(long)*n); for(int i=0;i<n
阅读全文
摘要:h 很简单的题目,不管是用数组还是用栈都非常简单。 #include<iostream> #include<stack> using namespace std; int main(){ int n; while(cin >> n){ stack<long> s; while(n!=0){ int
阅读全文
摘要:#include<iostream> #include<queue> using namespace std; void delect(queue<int>* q,int x){ if(q->empty()) return; for(int i=0;i<q->size();i++){ int t=q
阅读全文
摘要:做个循环列表就行了。 逻辑上想想还是很简单的。 然而在实践的时候需要考虑许多边界情况。每次循环的时候要考虑头节点的问题。 #include<stdio.h> #include<stdlib.h> struct node{ int data; struct node* next; }; typedef
阅读全文
摘要:\ 这题思路还是挺多的。 如果用数学的角度考虑。知道了n,p,m自然就知道下一个要出队的人的编号。然后一个个输出就行了。 还可以用循环链表做。 还可以用队列。出队在入队。 #include<iostream> #include<queue> using namespace std; int main
阅读全文
摘要:、 #include<stdio.h> int sum(int x){ int sum = 0; for(int i = 1; i < x ;i++){ if(x%i==0) sum+=i; } return sum ; } int main(){ int A[60]={0}; int B[60]=
阅读全文
摘要:练习使用向量vector容器。 遍历每个数取余就好了。然后记录下来。 #include<iostream> #include<vector> using namespace std; int main() { vector<int> B ; vector<int> C ; for (int i =
阅读全文
摘要:C写个快排就行了。 #include<stdio.h> #include<stdlib.h> #include<stdbool.h> int divide(int* A,int head, int tail){ if(head==tail) return head ; int x =A[head];
阅读全文
摘要:二分查找,没什么好说的。关键在于排成有序数组。然而C++调用sort就可以了。 #include<iostream> #include<algorithm> #include<cstdlib> using namespace std; bool judge(int* A, int n ,int t)
阅读全文
摘要:#include<stdio.h> #include<stdlib.h> int main(){ int n = 0; while(scanf("%d",&n)!=EOF){ int* A=(int*)malloc(sizeof(int )*n); for(int i = 0 ; i < n ;i+
阅读全文
摘要:摆了几天,重新再来学习。 ‘ 把数据输入数组,然后遍历数组就行了,没什么难度。 #include<iostream> #include<cstdlib> using namespace std; int main(){ int n; while(cin >> n){ int* A=(int*)mal
阅读全文
摘要:创建一个结构体,然后按要求快排就行了。 #include <stdio.h> #include <stdlib.h> typedef struct node{ int num; char S[100]; int score; }student; int divide1(student* A,int
阅读全文
摘要:用C++库函数sort秒杀了,建一个结构体就好了,同时储存输入次序。 #include<iostream> #include<algorithm> #include<cstdlib> using namespace std; struct node{ int num; char x[20]; int
阅读全文
摘要:#include<stdio.h> typedef struct node{ int num; int data; }student; int divide1(student A[],int head,int tail){ if(head==tail) return head; int t=A[he
阅读全文
摘要:sort实在是太好用了。活用sort,一切排序题目都可以秒杀。 #include <iostream> #include <algorithm> using namespace std; struct node{ int num; int date; }; typedef struct node s
阅读全文
摘要:#include <stdio.h> int divide(int A[],int head,int tail){ if(head==tail) return head; int t=A[head]; while(head<tail){ while(head<tail && A[tail]>t )
阅读全文
摘要:建两个数组就好了,一个存奇数一个存偶数,然后sort一下,最后输出。 #include <iostream> #include <algorithm> using namespace std; bool comp(int left,int right){ if(left > right) retur
阅读全文
摘要:写个快排就完事了。实在不行,写个选择排序也很简单。 #include<stdio.h> int devide(int A[],int head,int tail){ if(head==tail) return head; int t = A[head] ; while(head < tail){ w
阅读全文
摘要:c++还是方便啊,直接调用库函数就可以实现排序了。不用自己实现排序函数了。 #include<iostream> #include<algorithm> using namespace std; int main(){ int A[101]={0}; int n = 0 ; while(cin >>
阅读全文
摘要:#include<stdio.h> int main(){ char A[4][8]={ 'a','d','g','j','m','p','t','w', 'b','e','h','k','n','q','u','x', 'c','f','i','l','o&
阅读全文
摘要:这题出的只能说是无语。思路还是很简单的。 只要用一个的tag标记上次是哪个按键即可,然后tag和现在对比,要是相同就多加2。 #include<iostream> #include <map> using namespace std; int main(){ map<char,int>Map={ {
阅读全文
摘要:#include<iostream> using namespace std; void move (int A[],int head, int tail){ for( ; head <= tail ; head++ ){ A[head]=0; } } int main( ){ int l = 0
阅读全文
摘要:这题思路可以说是太简单了。 用一个数组表示树,值为1表示有树,值为零表示无就行。、 最后统计1的个数即为剩下的树。 #include <stdio.h> #include <malloc.h> void move (int A[],int head,int tail){ for( ; head <=
阅读全文
摘要:因为考研机试的原因,C和C++最好都准备一下,所以有C++版本。 #include <iostream> #include <cstring> #include <map> using namespace std ; int cmp(int year,int mouth,int day){ if(y
阅读全文
摘要:这题其实就是多了一个字符串转化成数字而已。 用一个字符串数组和字符串比较函数就可以得出数字月份然后就简单了。 然后最后一个难点就是确定是星期几,可以根据今天的日期的星期当作固定点,找相差几天然后得出具体星期。 #include <stdio.h> #include <stdbool.h> #incl
阅读全文
摘要:这题有一个前人总结的终极优化版本。 思想其实也很简单。学起来也很快。优化了如果变成下一年的情况和闰年判断 #include <stdio.h> #include <stdbool.h> int main( ) { int A[13] = {0,31,28,31,30,31,30,31,31,30,3
阅读全文
摘要:这也是一道经典的算法题。 其实也是用两个数组。还有判断是否闰年。 两个个循环,外面一个是月份循环,内部一个是每个月的天数循环,然后计数器Count++就行,直到和天数相同就跳出循环,打印就行。 #include <stdio.h> int judge ( int year ) { if (year
阅读全文
摘要:这题算是非常经典的题目了。 无非就是判断闰年然后计算天数而已。 用两个month数组记录月份天数 一三五七八十腊是31天,二月份非闰年28天,闰年29 天,其余都是30天就好了。 #include <stdio.h> int judge( int year ) { if( year % 400 ==
阅读全文
摘要:图形问题的万金解决方法就是创建一个二维数组,然后将填数组,最后打印数组就行了。其本质还是找出图形的规律。 首先来找规律,先从外形上来找。 奇数高,看图形,是上下左右对称的。所以只找上半区的规律。 然后首行比其他行少两个字符也就是多两个空格,最外层都是A,数组可以提前都赋值。只需要管中间部分 从中间部
阅读全文
摘要:这题还是挺就经典的,我的解决思路如下: 高度为H,则有H行需要打印,每行打印的内容大同小异,所以只需要H次循环就好了。大体框架就解决了,然后考虑细节。 第一行有H个星星 第二行有H+2个星星 第三行有H+4个星星 可知 第X行有H+(X-1)*2个星星 第H行有H+(H-1)*2个星星,也就是3H-
阅读全文