08 2012 档案
摘要:题目:把一个有序整数数组放到二叉树。答:#include "stdafx.h"#include <iostream>using namespace std;struct TreeNode { int m_nValue; TreeNode *m_pLeft; TreeNode *m_pRight;};//把一个有序整数数组放到二叉树void RecurCreateTree(int *p, int length, TreeNode *&pHead){ if (length > 0) { pHead = new TreeNode; int m...
阅读全文
摘要:题目:从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的。2-10为数字本身,A为1,J为11,Q为12,K为13,而大小王可以看成任意数字。答:#include "stdafx.h"#include <iostream>#include <ctime>using namespace std;#define MAXVALUE 10000#define MINVALUE -1#define SIZE 14 #define NUMBER 5//扑克牌的顺子bool IsSort(int arr[], int length){ int pl
阅读全文
摘要:题目:二叉树的结点的定义如下:struct TreeNode { int m_nValue; TreeNode *m_pLeft; TreeNode *m_pRight;};输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点。答:#include "stdafx.h"#include <iostream>#include <fstream>#include <ctime>using namespace std;struct TreeNode { int m_nValue; TreeNode *m_pLeft; TreeNode
阅读全文
摘要:题目:我们把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第1500个丑数。答:#include "stdafx.h"#include <iostream>using namespace std;//1、从1开始穷举,直到第1500个丑数,效率太低//判断number是否为丑数bool IsUgly(int number){ while (number % 2 == 0) { number = number / 2; } while (num...
阅读全文
摘要:题目:写一个函数,它的原型是如下,在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串付给其中一个函数参数outputstr所指的内存。int continuemax(char *outputstr, char *inputstr)举例:intputstr被赋予"abcd12345ed125ss123456789",函数将返回9,outputstr所指的值为"123456789"。答:#include "stdafx.h"#include <iostream>using namespace std;//
阅读全文
摘要:题目:一个整数数组里除了两个数字之外,其他的数字都出现了两次,找出这两个只出现一次的数字。要求:时间复杂度是O(n),空间复杂度是O(1)。答:#include "stdafx.h"#include <iostream>using namespace std;bool isBitOne(int number, int index){ number = number >> index; if (number & 1) { return true; } return false;}//找出数组中两个只出现一次的数字void FindNumber(
阅读全文
摘要:题目:给定单链表,如果有环的话请返回从头结点进入环的第一个结点。答:#include "stdafx.h"#include <iostream>#include <fstream>#include <ctime>using namespace std;struct ListNode{ int m_nKey; ListNode* m_pNext;};//构造链表void CreateList(ListNode *&pHead, ListNode *&pFirstNode){ fstream fin("list.tx
阅读全文
摘要:题目:输入一个字符串,打印出该字符串的所有子集。举例:输入字符串ab,则输出ab所有的子集{a b},{a},{b},{}。答:#include "stdafx.h"#include <iostream>using namespace std;//获取n个元素的子集void GetAllSubset(char *a, int *mask, int size, int c){ if (size == c) { cout<<"{ "; for (int i = 0; i < size; i++) { if (mask[i]...
阅读全文
摘要:题目:输入一个字符串,打印该字符串的所有排列。举例:输入字符串abc,则输出由字符a、b、c所能排列出来的所有字符串abc、acb、bac、bca、cab和cba。答:#include "stdafx.h"#include <iostream>using namespace std;//交换两个数void swap(char &a, char &b){ char tmp = a; a= b; b = tmp;}//获取数组a的全排列void GetAllRange(char *a, int size, int c){ if (size == c)
阅读全文
摘要:题目:用递归颠倒一个栈,例如输入栈{1, 2, 3, 4, 5},1在栈顶。颠倒之后的栈为{5, 4, 3, 2, 1},5处在栈顶。答:#include "stdafx.h"#include <iostream>#include <stack>using namespace std;void AddStackBottom(stack<int> &s, int top){ if (s.empty()) { s.push(top); } else { int tmp = s.top(); s.pop(); Ad...
阅读全文
摘要:#include "stdafx.h"#include <iostream>using namespace std;#define MAXSIZE 20typedef struct{ int r[MAXSIZE + 1]; int length;}SqList;typedef SqList HeapType;//***********************************选择排序*************************************begin//查找最小值int SelectMinKey(const SqList& L, i
阅读全文
摘要:题目:编码完成处理函数:函数将字符串中的字符'*'移到串的前面,前面的非'*'字符后移,但不能改变非'*'字符的先后顺序,函数返回串中字符'*'的数量。举例:原始串:ab**cd**e*12,处理后为*****abcde12,函数返回值为5。要求:使用尽量少的时间和辅助空间。答:#include "stdafx.h"#include <iostream>using namespace std;//将字符串中的*移到串的前部分int RemoveStarToFront(char *str){ if (
阅读全文
摘要:题目:删除字符串中的数字并压缩字符串。举例:输入字符串"abc123de4fg56", 输出"abcdefg"。要求:不开辟新空间,时间复杂度为O(n)。答:#include "stdafx.h"#include <iostream>using namespace std;//删除字符串中的数字并压缩字符串void RemoveNumberChar(char *str){ if (NULL == str) { return; } char *p = str; char *pNewStr = str; while (*p !
阅读全文
摘要:题目:给定链表的头指针和一个结点指针,在O(1)时间内删除该结点,链表结点的定义如下:struct ListNode{ int m_nKey; ListNode* m_pNext;};函数的声明如下:void DeleteNode(ListNode *pListHead, ListNode *pToBeDeleted);答:#include "stdafx.h"#include <iostream>#include <fstream>#include <ctime>using namespace std;struct ListNode{
阅读全文
摘要:题目:输入两个字符串,从第一个字符串中删除第二个字符串中所有的字符。举例:输入"They are students."和"aeiou",则输出之后的第一个字符串变成"Thy r stdnts."。答:#include "stdafx.h"#include <iostream>using namespace std;//在字符串中删除特定的字符void DeleteSpecialChar(char *pStr, const char *pDelStr){ const int length = 256; i
阅读全文
摘要:题目:数组中有一个数组出现的次数超过了数组长度的一半,找出这个数字。答:#include "stdafx.h"#include <iostream>using namespace std;//查找数组中超过出现次数超过一半的数字int FindNumber(int arr[], int length){ if (NULL == arr || length <= 0) { return -1; } int nValue = arr[0]; int count = 1; for (int i = 1; i < length; i++) { ...
阅读全文
摘要:题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个排好序的数组的一个旋转。输出旋转数组的最小值。举例:输入数组{3, 4, 5, 1, 2}为{1, 2, 3, 4, 5}的一个旋转,该数组的最小值为1。答:#include "stdafx.h"#include <iostream>using namespace std;//查找旋转数组中的最小元素int FindMinValue(int arr[], int length){ int low = 0; int high = length - 1; while (low <
阅读全文
摘要:题目:输入一个正数n,输出所有和为n的连续正数序列举例:输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1,2,3,4,5;4,5,6;7,8。答:#include "stdafx.h"#include <iostream>using namespace std;//求和为n的连续正数序列void FindSequenceNumber(int n){ if (n <= 0) { return; } int first = 1; int last = 2; int end = (n + 1) / 2; int...
阅读全文
摘要:题目:输入一个整数数组,调整数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分。要求:时间复杂度为O(n)答:#include "stdafx.h"#include <iostream>using namespace std;bool IsOddNumber(int n){ return (n & 1) != 0;}void AdjustArray(int arr[], int length, bool (*Func)(int)){ int begin = 0; int end = length - 1; while (begi
阅读全文
摘要:题目:两个单链表,找出它们的第一个公共结点。链表的结点定义为:struct ListNode{ int m_nKey; ListNode* m_pNext;};答:#include "stdafx.h"#include <iostream>#include <fstream>using namespace std;struct ListNode{ int m_nKey; ListNode* m_pNext;};//构造链表void CreateList(ListNode *&pHead, fstream &fin, ListNode
阅读全文
摘要:题目:输入一个链表头结点,从尾到头反过来输出每个结点的值。链表结点定义如下:struct ListNode{ int m_nKey; ListNode* m_pNext;};答:1、可以先把链表逆置,然后再输出,具体参考 http://www.cnblogs.com/venow/archive/2012/08/26/2657559.html 这里我们使用另一种更为简单的方法:递归#include "stdafx.h"#include <iostream>#include <fstream>using namespace std;struct List
阅读全文
摘要:题目:输入一个单向链表,将该单链表逆置。举例:原来链表为1->2->3->4->5翻转为5->4->3->2->1链表结点定义如下:struct ListNode{ int m_nKey; ListNode* m_pNext;};答:#include "stdafx.h"#include <iostream>#include <fstream>using namespace std;struct ListNode{ int m_nKey; ListNode* m_pNext;};//构造链表void C
阅读全文
摘要:题目:输入一个整数n,求1到n这n个整数十进制表示中1出现的次数。举例:输入12,从1到12这些整数中包含1的数字有1, 10, 11,和12,1一共出现了5次。答:#include "stdafx.h"#include <iostream>using namespace std;int Count(int num){ int count = 0; while (num) { if (1 == num % 10) { count++; } num = num /10; } return ...
阅读全文
摘要:题目:输入一个整数,求该整数的二进制表达中有多少个1。举例:输入10,其二进制表示为1010,有两个1,因此输出2。答:#include "stdafx.h"#include <iostream>using namespace std;//只限于正数(输入负数会陷入死循环)int CountNonnegativeOneNumber(int n){ int count = 0; while (n) { if (n & 1) { count++; } n = n>>1; } return c...
阅读全文
摘要:题目:一个台阶总共有n级,如果一次可以跳1级,也可以跳2级。求总共有多少种跳法,并分析算法的时间复杂度。答:用一个函数f(n)来表示n级台阶总的跳法。 1、只有1个台阶,则f(1) = 1; 2、有2个台阶,则f(2) = 2; 3、当有n个台阶时,如果第一次跳1级,有f(n-1)种跳法,如果第一次跳2级,有f(n - 2)种跳法,即f(n) = f(n-1) + f(n-2)。即为Fibonacci序列。#include "stdafx.h"#include <iostream>using namespace std;//循环int TotalStep(int
阅读全文
摘要:题目:定义字符串的左旋转操作,把字符串前面的若干个字符移动到字符串的尾部。要求:对长度为n的字符串操作的时间复杂度为O(n),辅助内存为O(1)。举例:把字符串abcdef左旋转2位得到字符串cdefab。答:#include "stdafx.h"#include <iostream>using namespace std;void swap(char *str, int begin, int end){ char ch; while (begin < end) { ch = *(str + begin); *(str + begin) = *(s...
阅读全文
摘要:题目:在一个字符串中找到第一个只出现一次的字符。举例:输入abaccdeff,则输出b。答:假设字符占一个字节,则共有256不同的字符,开辟256空间,用查找表。#include "stdafx.h"#include <iostream>using namespace std;void FindFirstOneChar(char *str){ if (NULL == str) { return; } int count[256] = {0}; char *p = str; while (*p != '\0') { count...
阅读全文
摘要:题目:输入一个英文句子,翻转句子中单词的顺序,但单词内的字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和简单字母一样处理。举例:输入"I am a student.",则输出"student. a am I"。答:每个单词先自我翻转,然后整个句子翻转。#include "stdafx.h"#include <iostream>#include <string>using namespace std;/* 翻转 I am a student. --> student. a am I*/void
阅读全文
摘要:#include "stdafx.h"#include <iostream>#include <fstream>#include <queue>using namespace std;typedef struct _Node{ int data; struct _Node *left; struct _Node *right; _Node() { data = 0; left = NULL; right = NULL; }}Node, *_PNode;//创建二叉树利用先序创建/* ...
阅读全文
摘要:#include "stdafx.h"#include <iostream>#include <fstream>using namespace std;typedef struct _Node{ int data; struct _Node *left; struct _Node *right; bool isVisit; //后序遍历标志(非递归) _Node() { data = 0; left = NULL; right = NULL; isVisit = false; }}...
阅读全文
摘要:题目:输入一个整数和一棵二元树,从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。打印出和与输入整数相等的所有路径。答:二叉树的后序非递归遍历#include "stdafx.h"#include <iostream>#include <fstream>#include <queue>using namespace std;typedef struct _Node{ int data; struct _Node *left; struct _Node *right; bool isVisit; //后序遍历标志(非递归)
阅读全文
摘要:题目:输入一个单向链表,输出该链表中倒数第k个结点。链表的倒数第0个结点为链表的尾指针。链表结点定义如下:struct ListNode{ int m_nKey; ListNode* m_pNext;};答:#include "stdafx.h"#include <iostream>#include <fstream>using namespace std;struct ListNode{ int m_nKey; ListNode* m_pNext;};//构造链表void CreateList(ListNode *&pHead){ fstr
阅读全文
摘要:题目:输入一个已经按升序排序过得数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。要求:时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。举例:输入数组1、2、4、7、11、15和数字15.由于4 + 11 = 15,因此输出4和11。、答:#include "stdafx.h"#include <iostream>using namespace std;void FindNumber(int arr[], int length, int num){ if (NULL == arr || length <= 0)
阅读全文
摘要:#include "stdafx.h"#include <fstream>#include <iostream>using namespace std;typedef struct _Node{ int data; struct _Node *left; struct _Node *right; int bf; //平衡因子 _Node() { data = 0; left = NULL; right = NULL; bf = 0; }}Node, *_PNode;//创...
阅读全文
摘要:#include "stdafx.h"#include <iostream>using namespace std;//*****************************满二叉树先序、中序和后序之间的转换*****************************begin//先序序列转换为后序序列//参数说明: (in) pre ———— 先序数组// (out) post ———— 后序数组// (in) preLow ———— 先序的第一个结点的下标// (in) preH...
阅读全文
摘要:题目:输入一棵二元查找树,将该树转换为它的镜像,即在转换后的二元查找树中,左子树的结点都大于右子树的结点。要求:用递归和循环两种方法完成树的镜像转换。举例: 8 8 / \ 转换 / \ 6 10 --> 10 6 / \ / \ / \ / \ 5 7 9 11 11 9 7 5二叉树定义的结点为:struct BSTreeNode{ int m_nValue; BST...
阅读全文
摘要:题目:求1 + 2 + 3 + ... + n 的和要求:不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)答:#include "stdafx.h"#include <iostream>using namespace std;//1、函数查找表法typedef int (*Func)(int n);int Sum1(int n){ return 0;}int Sum2(int n){ Func sum[2] = {Sum1, Sum2}; return sum[n > 0](n - 1) +
阅读全文
摘要:题目:求子数组的最大和要求:1、输入一个整形数组,数组里有正数也有负数。 2、数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。 3、求所有子数组的和的最大值。要求时间复杂度为O(n)。举例:输入数组为1, -2, 3, 10, -4, 7, 2, -5。和的最大子数组为3, 10, -4, 7, 2。即输出和为18。答:#include "stdafx.h"#include <iostream>using namespace std;int FindMaxSubSum(int arr[], int length){ int sum = 0; in
阅读全文
摘要:题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历结果。如果是,返回true,否则返回false。举例:输入5、7、6、9、11、10、8,由于这个整数序列有如下的树的后序遍历结果: 8 / \5、7、6、9、11、10、8 -> 6 10 / \ / \ 5 7 9 11因此返回tr...
阅读全文
摘要:题目:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。要求:不能创建任何新的结点,只调整指针的指向。举例: 10 / \ 转变 6 14 --> 4=6=8=10=12=14=16 / \ / \ 4 8 12 16定义的二元查找树结点的数据结构如下:struct BSTreeNode { int m_nValue; BSTreeNode *m_pLeft; BSTreeNode *m_pRight;};答:用二叉树的中序遍历#include "std...
阅读全文
摘要:#include "stdafx.h"#include <iostream>#include <fstream>#include <queue>#include <stack>#include <Windows.h>using namespace std;typedef struct _Node{ int data; struct _Node *left; struct _Node *right; _Node() { data = 0; left = NULL; right = NULL; }}Node, *...
阅读全文
摘要:#include "stdafx.h"#include <iostream>#include <fstream>#include <Windows.h>using namespace std;#define INFINITY 65535#define MAX_VERTEX_NUM 20 //顶点最多个数#define LENGTH 5 //顶点字符长度//*********************************邻接矩阵***********************************begin//邻接矩阵typedef st
阅读全文
摘要:#include "stdafx.h"#include <iostream>#include <fstream>#include <Windows.h>using namespace std;#define INFINITY 65535#define MAX_VERTEX_NUM 20 //顶点最多个数#define LENGTH 5 //顶点字符长度//*********************************邻接表***********************************begintypedef char Vert
阅读全文
摘要:#include "stdafx.h"#include <iostream>#include <fstream>#include <Windows.h>#include <algorithm>using namespace std;#define INFINITY INT_MAX#define MAX_VERTEX_NUM 20 //顶点最多个数#define LENGTH 5 //顶点字符长度//********************************Kruskal(并查集实现)*******************
阅读全文
摘要:#include "stdafx.h"#include <iostream>#include <fstream>#include <queue>#include <stack>#include <Windows.h>using namespace std;#define INFINITY INT_MAX#define MAX_VERTEX_NUM 20 //顶点最多个数#define LENGTH 5 //顶点字符长度//邻接矩阵typedef struct _Graph{ int matrix[MAX_VERTE
阅读全文
摘要:#include "stdafx.h"#include <iostream>#include <fstream>#include <queue>#include <Windows.h>using namespace std;#define INFINITY INT_MAX#define MAX_VERTEX_NUM 20 //顶点最多个数#define LENGTH 5 //顶点字符长度//邻接矩阵typedef struct _Graph{ int matrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]
阅读全文
摘要:#include "stdafx.h"#include <iostream>#include <Windows.h>using namespace std;typedef struct _Node{ int data; struct _Node *left; struct _Node *right; bool isRed; //红黑树 _Node() { data = 0; left = NULL; right = NULL; isRed = true; }}Node, *_PN...
阅读全文
摘要:#include "stdafx.h"#include <iostream>#include <fstream>#include <queue>#include <Windows.h>using namespace std;#define INFINITY INT_MAX#define MAX_VERTEX_NUM 20 //顶点最多个数#define LENGTH 5 //顶点字符长度//*********************************邻接表*********************************
阅读全文
摘要:#include "stdafx.h"#include <iostream>#include <fstream>#include <queue>#include <Windows.h>using namespace std;#define INFINITY INT_MAX#define MAX_VERTEX_NUM 20 //顶点最多个数#define LENGTH 5 //顶点字符长度//邻接矩阵typedef struct _Graph{ int matrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]
阅读全文
摘要:#include "stdafx.h"#include <iostream>#include <iomanip>#include <stack>#include <queue>#include <Windows.h>using namespace std;typedef struct _Node{ int data; struct _Node *left; struct _Node *right; int bf; //平衡因子 _Node() { data = 0; left = NULL; ...
阅读全文
摘要:一般我们说虚函数,它的访问级别都是public的,用类对象可以直接调用,这样就可以实现运行时的类型绑定,那如果我们将虚函数私有化会出现什么情况呢?我们先来看一个非虚函数私有化的例子class Base{private: void PrintClassName () { cout<<"Base"<<endl; }public: void print() { PrintClassName(); }};class Derived : public Base{private: void PrintClassName() { ...
阅读全文
摘要:#include "stdafx.h"#include <iostream>#include <iomanip>#include <stack>#include <queue>#include <Windows.h>using namespace std;enum TYPE{ TYPE_LINK = 0, TYPE_CLUES,};typedef struct _Node{ int data; struct _Node *left; struct _Node *right; bool isVisit; //用于后序
阅读全文
摘要:#include "stdafx.h"#include <iostream>#include <iomanip>#include <stack>#include <queue>#include <Windows.h>using namespace std;typedef struct _Node{ int data; struct _Node *left; struct _Node *right; _Node() { data = 0; left = NULL; right = NULL; }}Node, *_PN
阅读全文
摘要:#include "stdafx.h"#include <iostream>#include <stack>#include <queue>#include <Windows.h>using namespace std;typedef struct _Node{ int data; struct _Node *left; struct _Node *right; bool isVisit; //用于后序非递归遍历,表示节点是否可以访问 _Node() { data = 0; left = NULL; r...
阅读全文
浙公网安备 33010602011771号