Chap3: question: 11 - 18
11. double 数值的整数次方
note: 浮点数表示时有误差,判等时必须自己根据精度要求实现。
#include <iostream>
#include <ctime>
using namespace std;
bool Power_Input_Invalid = false; // set a Global variant to check the input. bool equal(double num1, double num2) // key 1
{
if(num1 - num2 >= -0.0000001 && num1 - num2 <= 0.0000001)
return true;
return false;
} double Power(double base, int exponent)
{
Power_Input_Invalid = false;
if(equal(base,0.0) && exponent <= 0) /* 无意义 */
{
Power_Input_Invalid = true;
return 0.0;
}
if(exponent == 0) return 1.0;
bool sigmal = false; // 0 denote positive exp
bool overflow = false;
if(exponent < 0)
{
if(exponent == INT_MIN) { overflow = true; exponent++;}
exponent *= -1;
sigmal = true; // 1 denote negative exp
} double result = base, tem = 1.0;
while(exponent - 1 != 0) // key 2
{
if(exponent & 1) tem *= result;
result *= result;
exponent >>= 1;
}
result *= tem;
if(overflow) result *= base;
if(sigmal) result = 1.0 / result;
return result;
} int main()
{
double d;
int v;
clock_t start;
int k = 0;
while(cin >> d >> v)
{
start = clock();
double result = Power(d, v);
if(!Power_Input_Invalid)
cout << "Case " << k++ << ": " << Power(d, v) <<
" time:" << (clock()-start) << "ms" << endl;
else
cout << "Invalid input." << endl;
}
return 0;
}
12.打印 1 到最大的 n 位数
note: n 可能很大,如 100,就容易溢出
a. 字符串模拟法
#include <stdio.h>
#include <string.h>
bool Increment(char *);
void printNumber(char *); void print1ToMax(int n)
{
if(n <= ) return;
char *number = new char[n+];
memset(number, '', n);
number[n] = '\0';
while(!Increment(number)) // Increment() return true denote overflow
{
printNumber(number);
}
delete[] number;
} bool Increment(char *number)
{
bool overflow = false;
int carry = , len = strlen(number);
for(int i = len -; i >= ; --i) /* note: 从最右端开始增 is more easy */
{
number[i] += carry;
if(i == len -) ++number[i];
if(number[i] > '')
{
if(i == )
overflow = true;
else
{
number[i] -= ;
carry = ;
}
}
else break;
}
return overflow;
} void printNumber(char *number)
{
if(number == NULL) return;
bool begin = false;
for(int i = ; number[i] != '\0'; ++i)
{
if(begin)
{
printf("%c", number[i]);
}
else if(number[i] != '')
{
begin = true;
printf("%c", number[i]);
}
}
printf("\t");
} int main()
{
int N;
while(scanf("%d", &N) == )
{
print1ToMax(N);
}
return ;
}
Code
b. 数字排列法(由后到前,递归增加)
void printNumber(char *);
void AddRecursively(char *, int, int); void print1ToMax(int n)
{
if(n <= 0) return;
char *number = new char[n +1];
memset(number, '0', n);
number[n] = 0;
AddRecursively(number, n, 0);
delete[] number;
} void AddRecursively(char *number, int length, int begin)
{
if(begin == length)
{
printNumber(number);
return;
}
for(int i = '0'; i <= '9'; ++i)
{
number[begin] = i;
AddRecursively(number, length, begin +1);
}
} void printNumber(char *number)
{
if(number == NULL) return;
bool begin = false;
for(int i = 0; number[i] != '\0'; ++i)
{
if(begin)
{
printf("%c", number[i]);
}
else if(number[i] != '0')
{
begin = true;
printf("%c", number[i]);
}
}
if(!begin) return;
printf("\t");
}
运行结果如上。
13. O(1) 时间删除链表结点
其值赋为后继的值,删除后继结点。时间:[O(1) * (n-1) + O(n)] / n ≈ O(1). // 若检测欲删除结点是否在链表内,则 O(n) .
#include <stdio.h>
struct ListNode{
int v;
ListNode * next;
};
enum{ N = }; // set the number of Node in ListNode
/*************************************************************************/
/******** Basic function which were needed ******************************/
/* creat a List with the value of Node is 1, 2, …,N-1. */
ListNode* creatList(){
ListNode * pHead = NULL, *p;
for(int i = ; i < N; ++i){
ListNode *s = new ListNode;
s->v = i;
s->next = NULL;
if(pHead != NULL){
p->next = s;
p = p->next;
}else{
pHead = s;
p = pHead;
}
}
return pHead;
}
void printList(ListNode *pHead){
if(pHead == NULL) { printf("NULL"); return; }
printf("%d —> ", pHead->v);
printList(pHead->next);
}
void release(ListNode *pHead) {
if(pHead == NULL) return;
release(pHead->next);
delete[] pHead;
pHead = NULL;
}
/****************************************************************************************/
void deleteNode(ListNode *pHead, ListNode * pToBeDeleted){
if(pHead == NULL || pToBeDeleted == NULL) return;
/* not the last one */
if(pToBeDeleted->next != NULL){
ListNode *pNext = pToBeDeleted->next;
pToBeDeleted->v = pToBeDeleted->next->v;
pToBeDeleted->next = pToBeDeleted->next->next;
delete[] pNext;
pNext = NULL;
}else{
if(pToBeDeleted == pHead) { // List only has one Node
delete [] pHead;
pHead = NULL;
return;
}else { // the last one && not the head
ListNode *p = pHead;
while(p->next != pToBeDeleted && p->next != NULL) p = p->next;
if(p->next == NULL) return;
else{
ListNode *s = p->next;
p->next = NULL;
delete[] s;
}
}
}
} int main()
{
ListNode *pHead = creatList();
printList(pHead);
printf("\ndelete 3: \n"); deleteNode(pHead, pHead->next->next); // delete 3 (among)
printList(pHead);
printf("\ndelete 1: \n"); deleteNode(pHead, pHead); // delete 1 (head)
printList(pHead);
printf("\ndelete 4: \n"); deleteNode(pHead, pHead->next); // delete 4 (tail)
printList(pHead);
printf("\n");
/*
ListNode *s = new ListNode; // not in the list, if wanted more robust , need check before deleteNode
deleteNode(pHead, s);
printList(pHead);
printf("\n");
delete[] s;
*/
release(pHead);
return ;
}
Code
14. 使整数数组中奇数位于前部分,偶数位于后部分。
解耦合,使 Reorder函数可以复用。如代码:
#include <stdio.h> bool isEven(int v)
{
return (v & ) == ;
} bool isPositive(int v)
{
return v > ;
} void Reorder(int data[], int length, bool (*fcn)(int))
{
if(length <= ) return;
int low = , high = length - ;
while(low < high)
{
while(low < high && fcn(data[low])) ++low;
while(low < high && !fcn(data[high])) --high;
if(low < high)
{
int tem = data[low];
data[low] = data[high];
data[high] = tem;
}
}
} void printf(int data[], int length)
{
for(int i = ; i < length; ++i)
{
printf("%-3d", data[i]);
}
printf("\n");
}
int main()
{
printf("Test 1: odd element before even element. \n");
int test1[] = {, , , , , , };
printf(test1, sizeof(test1)/sizeof(int));
Reorder(test1,sizeof(test1)/, isEven); /* 奇数放前面 */
printf(test1, sizeof(test1)/); printf("Test 2: positive before Non-positive. \n");
int test2[] = {-, , -, , , -, };
printf(test2, sizeof(test2)/sizeof(int));
Reorder(test2,sizeof(test2)/, isPositive); /* 正数放前面 */
printf(test2, sizeof(test2)/); return ;
}
Code
15. 链表中倒数第 k 个结点
note: k = 0 或 k > LinkList.length().
#include <stdio.h>
struct ListNode{
int v;
ListNode * next;
};
enum{ N = 5}; // set the number of Node in ListNode
/*************************************************************************/
/******** Basic function which were needed ******************************/
/* creat a List with the value of Node is 1, 2, …,N-1. */
ListNode* creatList(){
ListNode * pHead = NULL, *p;
for(int i = 1; i < N; ++i){
ListNode *s = new ListNode;
s->v = i;
s->next = NULL;
if(pHead != NULL){
p->next = s;
p = p->next;
}else{
pHead = s;
p = pHead;
}
}
return pHead;
}
void printList(ListNode *pHead){
if(pHead == NULL) { printf("NULL"); return; }
printf("%d —> ", pHead->v);
printList(pHead->next);
}
void release(ListNode *pHead) {
if(pHead == NULL) return;
release(pHead->next);
delete[] pHead;
pHead = NULL;
}
/****************************************************************************************/ ListNode* FindKthTOTail(ListNode *pListHead, unsigned int k)
{
if(pListHead == NULL || k == 0) return NULL; // Note 1: for robust
ListNode *pA, *pB;
pA = pB = pListHead;
for(unsigned int i = 0; i < k-1; ++i) // go ahead k-1 steps
{
if(pB->next == NULL) return NULL; // Note 2: the length of LinkList if less than k
else pB = pB->next;
}
while(pB->next != NULL)
{
pA = pA->next;
pB = pB->next;
}
return pA;
} int main()
{
ListNode *pHead = creatList();
printf("LinkList: ");
printList(pHead);
printf("\nTest: \n"); ListNode *p;
for(int k = 0; k <= N; ++k)
{
p = FindKthTOTail(pHead, k);
if(p != NULL) printf("倒数第 %d 个数: %d\n", k, p->v);
else printf("倒数第 %d 个数: Not Exist.\n", k);
} release(pHead);
return 0;
}
16. 反转链表
#include <stdio.h>
struct ListNode{
int v;
ListNode * next;
};
enum{ N = 10}; // set the number of Node in ListNode
/*************************************************************************/
/******** Basic function which were needed ******************************/
/* creat a List with the value of Node is 1, 2, …,N. */
ListNode* creatList(){
ListNode * pHead = NULL, *p;
for(int i = 1; i < N; ++i){
ListNode *s = new ListNode;
s->v = i;
s->next = NULL;
if(pHead != NULL){
p->next = s;
p = p->next;
}else{
pHead = s;
p = pHead;
}
}
return pHead;
}
void printList(ListNode *pHead){
if(pHead == NULL) { printf("NULL"); return; }
printf("%d —> ", pHead->v);
printList(pHead->next);
}
void release(ListNode *pHead) {
if(pHead == NULL) return;
release(pHead->next);
delete[] pHead;
pHead = NULL;
}
/******************************************************/ ListNode* ReverseList(ListNode *pListHead)
{
ListNode *pPre, *pCur, *pPost;
pPre = NULL, pCur = pListHead;
while(pCur != NULL)
{
pPost = pCur->next;
pCur->next = pPre;
pPre = pCur;
pCur = pPost;
}
/* pCur == pPost == NULL, pPre point to the new ListHead */
return pPre;
} int main()
{
ListNode *pHead = creatList();
printf("LinkList: ");
printList(pHead);
printf("\n"); pHead = ReverseList(pHead);
printf("Reverse1: ");
printList(pHead);
printf("\n"); pHead = ReverseList(pHead);
printf("Reverse2: ");
printList(pHead);
printf("\n"); release(pHead);
return 0;
}
a. b. c.
17.合并两个排序的链表(递归)
#include <stdio.h>
struct ListNode{
int v;
ListNode * next;
};
/*************************************************************************/
/******** Basic functions which were needed ******************************/
/* creat a List with the value of Node is 1, 2, …,N. */
ListNode* creatList(int begin, int N){
ListNode * pHead = NULL, *p;
for(int i = begin; i <= N; ++i){
ListNode *s = new ListNode;
s->v = i;
s->next = NULL;
if(pHead != NULL){
p->next = s;
p = p->next;
}else {
pHead = s;
p = pHead;
}
}
return pHead;
}
void printList(ListNode *pHead){
if(pHead == NULL) { printf("NULL"); return; }
printf("%d —> ", pHead->v);
printList(pHead->next);
}
void release(ListNode *pHead) {
if(pHead == NULL) return;
release(pHead->next);
delete[] pHead;
pHead = NULL;
}
/********************************************************************************/ ListNode* Merge(ListNode *pHead1, ListNode *pHead2) //not create any new Node again
{
if(pHead1 == NULL) return pHead2;
if(pHead2 == NULL) return pHead1;
ListNode *pMergedHead = NULL;
if(pHead1->v <= pHead2->v)
{
pMergedHead = pHead1;
pMergedHead->next = Merge(pHead1->next, pHead2);
}
else
{
pMergedHead = pHead2;
pMergedHead->next = Merge(pHead1, pHead2->next);
}
return pMergedHead; // key point
} int main()
{
ListNode *pHead1 = creatList(, );
printf("LinkList1: ");
printList(pHead1);
printf("\n");
ListNode *pHead2 = creatList(, );
printf("LinkList1: ");
printList(pHead2);
printf("\n"); ListNode *pHead3 = Merge(pHead1, pHead2);
printf("MergeList: ");
printList(pHead3);
printf("\n"); release(pHead3);
return ;
}
Code
18. 判断树 B 是否为树 A 的子结构(递归)
#include <iostream>
using namespace std;
struct BTNode{
int v; // default positive Integer.
BTNode *pLeft;
BTNode *pRight;
BTNode(int x) : v(x), pLeft(NULL), pRight(NULL) {}
};
/********************************************************/
/***** Basic functions ***********/
BTNode* createBinaryTree(int r)
{
BTNode *pRoot = new BTNode(r);
int u, v;
cin >> u >> v;
if(u != 0)
pRoot->pLeft = createBinaryTree(u);
if(v != 0)
pRoot->pRight = createBinaryTree(v);
return pRoot;
}
void release(BTNode *root){
if(root == NULL) return;
release(root->pLeft);
release(root->pRight);
delete[] root;
root = NULL;
}
/******************************************************************/ bool treeAHasTreeB(BTNode *pRootA, BTNode *pRootB){ /* check from every node in the BinaryTeee pRoot1 */
if(pRootB == NULL) return true;
if(pRootA == NULL) return false;
if(pRootA->v != pRootB->v) return false;
return treeAHasTreeB(pRootA->pLeft, pRootB->pLeft) && treeAHasTreeB(pRootA->pRight, pRootB->pRight);
} bool isSubTree(BTNode *pRoot1, BTNode *pRoot2){
bool result = false;
/* define the output when A or B is NULL */
if(pRoot2 == NULL) return true;
if(pRoot1 == NULL) return false;
result = treeAHasTreeB(pRoot1, pRoot2);
if(!result) result = treeAHasTreeB(pRoot1->pLeft, pRoot2);
if(!result) result = treeAHasTreeB(pRoot1->pLeft, pRoot2); return result;
} int main(){
int TestTime = 3, k = 1;
while(k <= TestTime)
{
cout << "Test " << k++ << ":" << endl;
cout << "Create Tree A: " << endl;
BTNode *pRoot1 = createBinaryTree(8);
cout << "Create Tree B: " << endl;
BTNode *pRoor2 = createBinaryTree(8);
if(isSubTree(pRoot1, pRoor2))
cout << "Tree A has Tree B." << endl;
else
cout << "Tree A has not Tree B." << endl; release(pRoot1);
release(pRoor2);
}
return 0;
}
Chap3: question: 11 - 18的更多相关文章
- github javascript相关项目star数排行榜(前30,截止2016.11.18):
github javascript相关项目star数排行榜(前30,截止2016.11.18): 前端开源框架 TOP 100 前端 TOP 100:::::https://www.awesomes. ...
- 日常Java 2021/11/18
用idea实现Javaweb登录页面 <%-- Created by IntelliJ IDEA. User: Tefuir Date: 2021/11/18 Time: 18:14 To ch ...
- #VSTS日志# 15/11/18 插件应用市场,RM,包管理器等
[小编]从今天开始,我将在这个博客上连载Visual Studio Team Service的定期更新.VSTS是Team Foundation Server 的在线版本,微软每3周会对这个服务进行更 ...
- 【VSTS 日志 15/11/18】 – 插件应用市场,RM,包管理器等
[小编]从今天开始,我将在这个博客上连载Visual Studio Team Service的定期更新.VSTS是Team Foundation Server 的在线版本,微软每3周会对这个服务进行更 ...
- 2017.11.18 手把手教你学51单片机-点亮LED
In Doing We Learning 在操作中学习.如果只是光看教程,没有实际的操作,对编程语言的理解很空泛,所以决定从单片机中学习C语言. #include<reg52.h> ...
- Sprint 1 Review & Daily Scrum - 11/18
今天我们组利用课后的时间对Sprint 1阶段进行了回顾,并对接下来的工作进行了安排. Sprint 1阶段我们开始定的计划是完成最基础的背单词功能,可以让用户可以完整地走一遍背单词流程.回顾上周,我 ...
- 团队作业4——第一次项目冲刺(Alpha版本)2017.11.18
1.当天站立式会议照片 本次会议在5号公寓312召开,本次会议内容:①:熟悉每个人想做的模块.②:根据老师的要求将项目划分成一系列小任务.③:在上次会议内容完成的基础上增加新的任务. 2.每个人的工作 ...
- 第11.18节 Python 中re模块的匹配对象
匹配对象是Python中re模块正则表达式匹配处理的返回结果,用于存放匹配的情况.老猿认为匹配对象更多的应该是与组匹配模式的功能对应的,只是没有使用组匹配模式的正则表达式整体作为组0. 为了说明下面的 ...
- 【Alpha版本】 第十天 11.18
一.站立式会议照片: 二.项目燃尽图: 三.项目进展: 成 员 昨天完成任务 今天完成任务 明天要做任务 问题困难 心得体会 胡泽善 完成管理员的三大功能界面框架, 我要招聘查看报名者的列表显示 完成 ...
随机推荐
- echart------属性详细介绍
theme = { // 全图默认背景 // backgroundColor: 'rgba(0,0,0,0)', // 默认色板 color: ['#ff7f50','#87cefa','#da70d ...
- HBase with MapReduce (MultiTable Read)
hbase当中没有两表联查的操作,要实现两表联查或者在查询一个表的同时也需要访问另外一张表的时候,可以通过mapreduce的方式来实现,实现方式如下:由于查询是map过程,因此这个过程不需要设计re ...
- 【Python】:简单爬虫作业
使用Python编写的图片爬虫作业: #coding=utf-8 import urllib import re def getPage(url): #urllib.urlopen(url[, dat ...
- C# 代码示例_结构/数组/枚举...
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- anjularjs 路由
在多视图单页面web应用中,angularjs使用路由‘#+标记’来区别不同的逻辑页面并将不同的页面绑定到对应的控制器上.通过一个简单的实例来深入理解: 1.index.html 主页面中插入代码: ...
- Bug严重级别分类
BUG等级划分,一般划分为:严重BUG.较严重BUG.一般性BUG.建议性BUG A类—严重错误,包括以下各种错误: 1. 由于程序所引起的死机,非法退出 2. 死循环 3. 数据库发生死锁 4. 因 ...
- 数据获取以及处理系统 --- 功能规格说明书V2.0
产品规格说明书: 版本号: V2.0 版本说明: Version 1.0 简单得需求分析以及构思,初稿形成 Version 2.0 细化beta阶段设计,增加典型用户尝尽以及功能罗列 1. 引言 1. ...
- 【NOIP2012】借教室
因为本校OJ+1s所以用线段树水过了,不去syz的水库水这题还真不知道线段树过不了= = 原题: 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要 向学校申请借教室.教室的 ...
- <script>标签应该放到</body>标签之前
著作权归作者所有. 商业转载请联系作者获得授权,非商业转载请注明出处. 作者:贺师俊 链接:http://www.zhihu.com/question/20027966/answer/13727164 ...
- RequestContextListener作用
spring IOC容器实例化Bean的方式有: singleton 在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. prototype ...