1、重写折半查找,使得在循环内部只执行一次测试

传统的非递归式的折半查找的例子中,while循环语句内部共执行了两次测试,其实只要一次就足够(代价是将更多的测试在循环外执行)。重写该函数,使得在循环内部只执行一次测试,比较两种版本函数的运行时间。

/* binsearch: find x in v[0] <= v[1] <= ... <= v[n-1] */
int binsearch(int x, int v[], int n)
{
int low, high, mid; low = 0;
high = n - 1;
mid = (low+high) / 2;
while(low <= high && x != v[mid])
{
if(x < v[mid])
high = mid - 1;
else
low = mid + 1;
mid = (low+high) / 2;
}
if(x == v[mid])
return mid; // found match
else
return -1; // no match
}

两种方案的执行时间几乎没有差异,我们并没有得到多大的性能改进,反而失掉了代码的可读性。

2、可视化转义字符与实际字符的双向转换

编写一个函数escape(s, t),将字符串t 复制到字符串s 中,并在复制的过程中将换行符、制表符等等不可显地字符分别转换为\n、\t等相应的可显示的转义字符序列。要求使用switch语句,再编写一个具有相反功能的函数,在复制过程中将转义字符序列转换为实际字符。

/* escape: expand newline and tab into visible sequences while copying the string t to s */
void escape(char s[], char t[])
{
int i, j; for(i = j = 0; t[i] != '\0'; i++)
{
switch(t[i])
{
case '\n': // newline
s[j++] = '\\';
s[j++] = 'n';
break;
case '\t': // tab
s[j++] = '\\';
s[j++] = 't';
break;
default: // all other chars
s[j++] = t[i];
break;
}
}
s[j] = '\0';
}

unescape函数与escape函数很相似,如下:

/* unescape: convert escape sequences into real characters while copying the string t to s */
void unescape(char s[], char t[])
{
int i, j; for(i = j = 0; t[i] != '\0'; i++)
{
if(t[i] != '\\')
s[j++] = t[i];
else // it is a backslach
switch(t[++i])
{
case 'n': // real newline
s[j++] = '\n';
break;
case 't': // real tab
s[j++] = '\t';
break;
default: // all other chars
s[j++] = '\\';
s[j++] = t[i];
break;
}
}
s[j] = '\0';
}

switch语句是允许嵌套的,因此unescape 函数也可以这样写:

/* unescape: convert escape sequences into real characters while copying the string t to s */
void unescape(char s[], char t[])
{
int i, j; for(i = j = 0; t[i] != '\0'; i++)
{
switch(t[i])
{
case '\\': // backslach
switch(t[++i])
{
case 'n': // real newline
s[j++] = '\n';
break;
case 't': // real tab
s[j++] = '\t';
break;
default: // all other chars
s[j++] = '\\';
s[j++] = t[i];
break;
}
break;
default: // not a backslach
s[j++] = t[i];
break;
}
}
s[j] = '\0';
}

3、正如TCPL读书笔记&心得中第33条阐述的那样,我们先前编写的itoa 函数不能处理绝对值最大的负数,因为在整形32位二进制数字的表示中,最大的负数取负,在机器底层上得到的结果仍然为最大的负数(也就是相应正数溢出的结果),这里我们的想法是把取模操作的结果转换为正数,从而绕过无法把最大的负数转换为正数的问题。

#include <stdio.h>
#define abs(x) ((x) < 0 ? -(x) : (x)) // itoa: convert n to characters in s - modified
void itoa(int n, char s[])
{
int i, sign;
void reverse(char s[]); sign = n; // record sign
i = 0;
do{ // generate digits in reverse order
s[i++] = abs(n % 10) + '0'; // get next digit
}while((n /= 10) != 0); // delete it
if(sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}

当然了,还可以采取一种措施,就是先判断该数字是不是最大的负数,如果是就进行特殊处理,不过这样做比较浪费资源,要额外多进行一次比较操作。

4、编写函数,将字符串s1中的速记符号在s2中扩展为等价的完整列表

编写函数expand(s1, s2),将字符串s1 中类似于a-z 一类的速记符号在字符串s2 中扩展为等价的完整列表abc···xyz。该函数可以处理大小写字母和数字,并可以处理a-b-c、a-z0-9 与a-z 等类似的情况。作为前导和尾随的字符原样复制。

/* expand: expand shorthand notation in s1 into string s2 */
void expand(char s1[], char s2[])
{
char c; int i, j;
while((c = s1[i++]) != '\0') // fetch a char from s1[]
{
if(s1[i] == '-' && s1[i+1] >= c)
{
i++;
while(c < s1[i]) // expand shorthand
s2[j++] = c++;
}
else
s2[j++] = c; // copy the character
}
s2[j] = '\0';
}

5、编写函数itob(n, s, b),将整数n转换为以b为底的数,并将结果以字符的形式保存到字符串s中。例如,itob(n, s, 16)把整数n格式化成十六进制整数保存在s中。

思路:先按逆序生成b进制数的每一位数字,再用函数reverse(自行编写,非库函数)对字符串s中的字符做一次颠倒而得到最终的结果。

/* itob: convert n to characters in s - base b */
void itob(int n, char s[], int b)
{
int i, j, sign;
void reverse(char s[]); if((sign = n) < 0) // record sign
n = -n; // make n positive
i = 0;
do // generate digits in reverse order
{
j = n % b; // get next digit
s[i++] = (j <= 9) ? j+'0' : j+'a'-10;
}while((n /= b) > 0); // delete it
if(sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}

Control Flow的更多相关文章

  1. SSIS的 Data Flow 和 Control Flow

    Control Flow 和 Data Flow,是SSIS Design中主要用到的两个Tab,理解这两个Tab的作用,对设计更高效的package十分重要. 一,Control Flow 在Con ...

  2. Control Flow 如何处理 Error

    在Package的执行过程中,如果在Data Flow中出现Error,那么Data Flow component能够将错误行输出,只需要在组件的ErrorOutput中进行简单地配置,参考<D ...

  3. 关于Control flow

    1.一个package包含一个control flow并且一个或多个data flow. (这个项目叫做 Integration services project,提供了三种不同类型的control  ...

  4. Core Java Volume I — 3.8. Control Flow

    3.8. Control FlowJava, like any programming language, supports both conditional statements and loops ...

  5. SSIS ->> Control Flow And Data Flow

    In the Control Flow, the task is the smallest unit of work, and a task requires completion (success, ...

  6. Control Flow in Async Programs

    Control Flow in Async Programs You can write and maintain asynchronous programs more easily by using ...

  7. A swift Tour(2) Control Flow

    Control Flow 用 if 和 switch 来做条件语句,并且用for-in,for,while,和do-while做循环,条件和循环的括号是可以不写的,但是body外面的括号是必须写的 l ...

  8. [译]Stairway to Integration Services Level 9 - Control Flow Task Errors

    介绍 在本文中,我们会实验 MaximumErrorCount和ForceExecutioResult 故障容差属性,并且还要学习Control Flow task errors, event han ...

  9. 《CS:APP》 chapter 8 Exceptional Control Flow 注意事项

    Exceptional Control Flow The program counter assumes a sequence of values                            ...

  10. Asynchronous JS: Callbacks, Listeners, Control Flow Libs and Promises

    非常好的文章,讲javascript 的异步编程的. ------------------------------------------------------------------------- ...

随机推荐

  1. mysql 存储过程,搞死人的语法

    MySQL 真心不如sqlserver灵活 存储过程注意事项: 1.declare 依次声明 DECLARE MyAccountID VARCHAR (36); DECLARE Balance DEC ...

  2. ArcGIS 裁剪地图显示范围

    在argmap工具中,图层属性中,数据框选择“裁剪选项”,“指定范围”,根据一个要素的轮廓,即可以选择需要全屏显示的图层“要素的轮廓”,确定以后地图就自动居中显示,请注意要排除掉超出范围的图层,否则发 ...

  3. svn 回滚到上一个版本shell 脚本

    #!/bin/sh ############################## # -- # # author jackluo # # Email net.webjoy@gmail.com # ## ...

  4. PHP zendframework phpunit 深入

    安装包管理 curl -sS https://getcomposer.org/installer | /usr/local/php/bin/php 将证书安装到 ~$ mkdir ~/tools/ht ...

  5. CCS样式表

    一.css样式表 1.样式表分类 1.内联式 <p style="font-size:18px;">This is an apple</p> 2.内嵌样式表 ...

  6. bzoj4562: [Haoi2016]食物链--记忆化搜索

    这道题其实比较水,半个小时AC= =对于我这样的渣渣来说真是极大的鼓舞 题目大意:给出一个有向图,求入度为0的点到出度为0的点一共有多少条路 从入读为零的点进行记忆化搜索,搜到出度为零的点返回1 所有 ...

  7. Java面试题大全(一)

    JAVA相关基础知识 1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分, ...

  8. 解决谷歌浏览器和360浏览器 input 自动填充淡黄色背景色的问题

     input:-webkit-autofill {-webkit-box-shadow: 0 0 0px 1000px white inset;}

  9. Hibernate简单实例

    1.配置hibernate.cfg.xml文件(名字必须这么写): <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernat ...

  10. 4s使用iOS 8的一些真實感受

    iPhone永遠離不開史上手機的爭論!你是否也在用呢? 今年iPhone 6/6Plus的發佈和上市可以說是振奮人心,大螢幕的升級.圓潤的外觀改變.全新的iOS 8系統,都是極具吸引力的.作為一名互聯 ...