摘要: 题目:数组中有一个数组出现的次数超过了数组长度的一半,找出这个数字。答:#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++) { ... 阅读全文
posted @ 2012-08-27 22:00 venow 阅读(388) 评论(0) 推荐(0)
摘要: 题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个排好序的数组的一个旋转。输出旋转数组的最小值。举例:输入数组{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 < 阅读全文
posted @ 2012-08-27 21:49 venow 阅读(469) 评论(1) 推荐(0)
摘要: 题目:输入一个正数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... 阅读全文
posted @ 2012-08-27 19:22 venow 阅读(335) 评论(0) 推荐(0)
摘要: 题目:输入一个整数数组,调整数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分。要求:时间复杂度为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 阅读全文
posted @ 2012-08-27 19:19 venow 阅读(293) 评论(0) 推荐(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, fstream &fin, ListNode 阅读全文
posted @ 2012-08-27 19:15 venow 阅读(538) 评论(0) 推荐(0)