算法之美_源码公布(1)

本文辑录了《算法之美——隐匿在数据结构背后的语言》(电子工业出版社2016年出版)一书第1~2章之代码(P1~P61)。全文文件夹、“45个算法”文件夹“22个经典问题文件夹”,以及有奖捉虫活动详情请见例如以下链接:http://blog.csdn.net/baimafujinji/article/details/50484348

附录中的经典笔试、面试问题參考答案请见:

http://blog.csdn.net/baimafujinji/article/details/50484683

探秘算法世界。求索数据结构之道;汇集经典问题,畅享编程技法之趣;点拨求职热点,敲开业界名企之门。

网上书店:

京东链接:http://item.jd.com/10111000454.html

http://item.jd.com/10111372484.html

China-pub中国互动出版网:http://product.china-pub.com/4911922

亚马逊:http://www.amazon.cn/%E7%AE%97%E6%B3%95%E4%B9%8B%E7%BE%8E-%E9%9A%90%E5%8C%BF%E5%9C%A8%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E8%83%8C%E5%90%8E%E7%9A%84%E5%8E%9F%E7%90%86-%E5%B7%A6%E9%A3%9E/dp/B01AGNUIE8/ref=sr_1_8?ie=UTF8&qid=1453527399&sr=8-8&keywords=%E5%B7%A6%E9%A3%9E

电子工业出版社官方链接:http://www.phei.com.cn/module/goods/wssd_content.jsp?bookid=44441

假设你是该书的读者。请务必加算法学习群(495573865),内有很多其它资源等你,而你在读书中遇到的疑问也将得到我第一时间的解答。

很多其它关注本博客,我将陆续公布该书所有源码至本博客。

P2:C++中的原子数据类型与它们所占的存储空间

#include <iostream>using namespace std;int main(int argc, char** argv) {cout << "bool: " << sizeof(bool) << endl;cout << "char: " << sizeof(char) << endl;cout << "short: " << sizeof(short) << endl;cout << "int: " << sizeof(int) << endl;cout << "long: " << sizeof(long) << endl;cout << "float: " << sizeof(float) << endl;cout << "double: " << sizeof(double) << endl;return 0;
}

P25:变量与地址

#include "stdafx.h"
#pragma runtime_checks( "", off )int _tmain(int argc, _TCHAR* argv[])
{int a = 97;int b = 98;int c = 99;return 0;
}

P29:使用指针变量

#include "stdafx.h"
#include "iostream"
#pragma runtime_checks( "", off )using namespace std;int _tmain(int argc, _TCHAR* argv[])
{unsigned int a = 5;unsigned int *pint = NULL;cout << "&a = " << &a << endl << " a = " << a << endl;cout << " &pint = " << &pint << endl << " pint = " << pint << endl;cout << " &(*pint) = " << &(*pint) << endl << endl;pint = &a;cout << "&a = " << &a << endl << " a = " << a << endl;cout << " &pint = " << &pint << endl << " pint = " << pint << endl;cout << " &(*pint) = " << &(*pint) << endl << endl;*pint = 10;cout << "&a = " << &a << endl << " a = " << a << endl;cout << " &pint = " << &pint << endl << " pint = " << pint << endl;cout << " &(*pint) = " << &(*pint) << endl << endl;system("PAUSE");return 0;
}

P32:函数的參数传递——按值传递

#include <iostream>using namespace std;void fun(int x)
{cout<< “x = ”<<x<<endl;x++;cout<< “x = ”<<x<<endl;
}int main(int argc, char** argv) {int x = 0;cout<<"x = " <<x<<endl;fun(x);cout<<"x = " <<x<<endl;return 0;
}

P33:按值传递对象

#include <iostream>using namespace std;class Student {string name;int age;public:Student() : name(""), age(0) {}void set_age(int age) {this->age = age;}int get_age() { return age;}void set_name(string name) {this->name = name;}string get_name() { return name;}
};void increment_age(Student s) {s.set_age(s.get_age() + 1);
}int main(int argc, char** argv) {Student s;s.set_name("Wumingshi");s.set_age(20);increment_age(s);cout << s.get_age() << endl;return 0;
}

P34:函数的參数传递——指针传递

#include <iostream>using namespace std;void fun(int* p)
{cout<< "*p = "<<*p<<endl;(*p)++;cout<< "*p = "<<*p<<endl;
}int main(int argc, char** argv){int x = 0;cout<<"x = " <<x<<endl;fun(&x);cout<<"x = " <<x<<endl;return 0;
}

P35:函数的參数传递——引用传递

#include <iostream>using namespace std;void fun(int& r)
{cout<< "r = "<<r<<endl;r++;cout<< "r = "<<r<<endl;
}int main(int argc, char** argv){int x = 0;cout<<"x = " <<x<<endl;fun(x);cout<<"x = " <<x<<endl;return 0;
}

P36:採用引用传递方式来传递指针

#include <iostream>using namespace std;void first_bigger(int*& p, int threshold) {while (*p <= threshold) {p++;}
}int main(int argc, char** argv) {int numbers[] = {0, 12, 32, 44, 33, 5, 85, 45, 100, 75};int* result = &numbers[0];cout <<"Begin at: "<< *result << endl;first_bigger(result, 60);cout <<"Result is: "<< *result << endl;return 0;
}

P39-1:数组的使用(数组元素求和)

#include <iostream>using namespace std;int main(int argc, char* argv[]) {int a[10] = {1, 32, 65, 2, 100, 34, 33, 21, 10, 1};int sum = 0;for(int i = 0; i < 10; i++){sum = sum + a[i];}cout << "数组中元素的和为 " << sum<< endl;return 0;
}

P39-2:数组的越界訪问

#include <iostream>using namespace std;int main(int argc, char* argv[]) {int a[5] = {1, 2, 3, 4, 5};int b[5] = {6, 7, 8, 9, 10};cout << b[6] << endl;
return 0;
}

P41:矩阵转置

#include <iostream>using namespace std;int main(int argc, char** argv) {int a[4][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}};int i = 0;int j = 0;int tmp = 0;for(i =0; i < 4; i++){for(; j < 4; j++){tmp = a[i][j];a[i][j] = a[j][i];a[j][i] = tmp;}j=i+1;}for(i = 0; i < 4; i++){for(j = 0; j < 4; j++){cout<<a[i][j]<<" ";}cout<<endl;}return 0;
}

P42:数组与指针的关系

#include <iostream>using namespace std;int main(int argc, char* argv[]) {int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};int * p;p = &a[0];cout<< a[0] <<endl;cout<< &a[0]<<endl;cout<< &a<<endl;cout<< a <<endl;cout<< p <<endl;cout<< *p<<endl;return 0;
}

P43:自加运算符与指针

#include <iostream>
using namespace std;int main(int argc, char* argv[]) {int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};int * p;p = &a[0];cout<< *(p++)<<endl;p = &a[0];cout<< *p++<<endl;p = &a[0];cout<< *(++p)<<endl;return 0;
}

P44:指针訪问多维数组

#include <iostream>using namespace std;int main(int argc, char* argv[]){int a[3][3] = {0, 1, 2, 3, 4, 5, 6, 7, 8};int * p;p = &a[0][0];for(int i = 0; i<9; i++)cout<<*p++<<endl;return 0;
}

P46:数组的抽象数据类型

头文件

#ifndef ARRAY_H
#define ARRAY_H#include <iostream>using namespace std;const int DefaultSize = 20;template <class Type>
class Array
{Type *elements;            //数组存放空间 int ArraySize;         //当前长度
public: Array(int Size=DefaultSize);                   //构造函数Array(const Array<Type>& x);                        //拷贝构造函数~Array() {delete []elements;}                   //析构函数Array<Type> & operator = (const Array<Type> & rhs);      //数组复制Type& operator [] ( int i );                  //取元素值int Length () const { return ArraySize; }         //取数组长度void ReSize (int sz);                            //扩充数组
};template <class Type>
Array<Type>& Array<Type >::operator= (const Array<Type >& rhs)
{int n = rhs. ArraySize;       // 取rhs的数组大小if (ArraySize != n){delete [] elements;        // 删除数组原有内存elements = new Type[n];     // 又一次分配n个元素的内存if (elements == NULL)      //假设分配内存不成功,输出错误信息{ArraySize = 0; cerr << "存储分配错误!

" << endl; exit(1); } ArraySize = n; //记录本对象的数组大小 } // 从rhs向本对象复制元素 Type* destptr = elements; Type* srcptr = rhs. elements; while (n--) *destptr++ = *srcptr++; return *this; // 返回当前对象的引用 } template <class Type> Array<Type> :: Array (int sz) { if ( sz <= 0 ) { ArraySize = 0; cerr << "非法数组大小" << endl; return; } elements = new Type[sz]; if (elements == NULL) { ArraySize = 0; cerr << "存储分配错误!" << endl; exit(1); } ArraySize = sz; } template <class Type> Array<Type> :: Array (const Array<Type> & x ) { int n = x.ArraySize; ArraySize = n; elements = new Type[n]; if ( elements == NULL ) { ArraySize = 0; cerr << "存储分配错" << endl; exit(1); } Type *srcptr = x.elements; Type *destptr = elements; while ( n-- ) * destptr++ = * srcptr++; } template <class Type> Type& Array<Type> :: operator [] (int i){ if ( i < 0 || i > ArraySize-1 ) { cerr << "数组下标超界" << endl; exit(1); } return elements[i]; } template <class Type> void Array<Type> :: ReSize (int sz) { if (sz >= 0 && sz != ArraySize) { Type * newarray = new Type[sz]; //创建新数组 if (newarray == NULL) { cerr << "存储分配错" << endl; return; } int n = (sz <= ArraySize) ? sz : ArraySize; //按新的大小确定传送元素个数 Type *srcptr = elements; //源数组指针 Type *destptr = newarray; //目标数组指针 while (n--) * destptr++ = * srcptr++; delete [] elements; elements = newarray; ArraySize = sz; } } #endif

測试程序

#include <iostream>
#include <iomanip>
#include "Array.h"using namespace std;int main(int argc, char** argv) {Array<int>  A(10);int n;int primecount = 0, i, j;cout << "Enter a value >= 2 as upper limit for prime numbers: ";cin >> n;A[primecount++] = 2;    // 2是一个质数,[]下标运算for(i = 3; i < n; i++) {if (primecount == A.Length()) A.ReSize(primecount + 10);if (i % 2 == 0) continue;j = 3;while (j <= i/2 && i % j != 0) j += 2;if (j > i/2) A[primecount++] = i;}for (i = 0; i < primecount; i++) {cout << setw(5) << A[i];if ((i+1) % 10 == 0)  cout << endl;}cout << endl;return 0;
}

P49:Z字形编排问题

请參见博文——ZigZag排列问题与经典笔试面试题目解析

(链接地址:http://blog.csdn.net/baimafujinji/article/details/50388584)

P51:大整数乘法问题

#include <iostream>
#include <memory.h>#define SIZE 14using namespace std;// 返回位数为size1+size2
int* multi(int* num1, int size1, int* num2, int size2)
{int size = size1 + size2;int* ret = new int[size];int i = 0;memset(ret, 0, sizeof(int) * size);for (i = 0; i < size2; ++i){int k = i;for (int j = 0; j < size1; ++j)ret[k++] += num2[i] * num1[j];}for (i = 0; i < size; ++i) {if (ret[i] >= 10){ret[i+1] += ret[i] / 10;ret[i] %= 10;}}return ret;
}int main(int argc, char** argv) {int num1[SIZE] = {1,2,3,4,5,6,7,8,9,1,1,1,1,1};//第一个大整数 11111987654321int num2[SIZE] = {1,1,1,2,2,2,3,3,3,4,4,4,5,5};//第二个大整数 55444333222111int* ret = multi(num1, SIZE, num2, SIZE);for (int i = SIZE*2-1; i >= 0; i--)cout << ret[i];delete[] ret; //释放内存return 0;
}

P52:九宫格问题

#include <iostream>
#include <iomanip>
#include <memory.h>using namespace std;int main()
{cout<<"请输入幻方的大小n(n是一个大于1的奇数):";int n=1;cin>>n;cout<<endl;int **a = new int*[n];for(int i=0; i<n; ++i){a[i]=new int[n];memset(a[i], 0, n*sizeof(int));    }int row=0;int col=n/2;for(int i=1; i<=n*n; i++) {a[row][col]=i;row--;col++;if(row<0&&col>=n){col--;row+=2;}else if(row<0){row=n-1;}else if(col>=n){col=0;}else if(a[row][col]!=0){col--;row+=2;}}for(int i=0;i<n;i++){for(int j=0;j<n;j++){cout<<setw(6)<<a[i][j];}cout<<endl;}for(int i = n; i > 0;)delete[] a[--i];delete[] a;return 0;
}

P55:用new来创建对象

#include <iostream>using namespace std;class Point
{int x;int y;public:Point(){ x = 0;y = 0;};Point(int xx, int yy){x = xx;y = yy;};~Point(){cout<<"END"<<endl;};int getX(){return x;};int getY(){return y;};void setX(int newX){x = newX;};void setY(int newY){y = newY;};
};int main(int argc, char** argv) {Point * point1 = new Point;Point * point2 = new Point(3, 5);cout<<point1->getX()<<endl;cout<<point2->getX()<<endl;delete point1;delete point2;return 0;
}

posted on 2017-07-02 08:41 mthoutai 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/mthoutai/p/7105082.html

算法之美_源码公布(1)相关推荐

  1. python 网站 源码_在线浏览美图源码,附带python源码

    源码介绍 本源码由@香谢枫林 开发,首页图片做了浏览器窗口自适应,最大化占满PC浏览器和移动浏览器的窗口,并且防止出现滚动条. 源码截图 美图源码1 美图源码2 功能介绍 首页图片设置了4个点击功能区 ...

  2. InstallShield打包源码公布

    InstallShield打包源码公布包含重量级别的内容. Setup.rul 内容 #include "ifx.h" #include "global.h" ...

  3. GAT 算法原理介绍与源码分析

    GAT 算法原理介绍与源码分析 文章目录 GAT 算法原理介绍与源码分析 零. 前言 (与正文无关, 请忽略) 广而告之 一. 文章信息 二. 核心观点 三. 核心观点解读 四. 源码分析 4.1 G ...

  4. 模仿Hibernate的逆向工程_java版_源码下载

    在这篇blog:"Hibernate逆向工程原理_java版本"中谈到了Hibernate逆向工程原理. 我喜欢理论和实践相结合....so,今天我试着模仿hibernate的逆向 ...

  5. 较高人工智能的人机博弈程序实现(多个算法结合)含C++源码

    较高人工智能的人机博弈程序实现(多个算法结合)含C++源码 本文由恋花蝶最初发表于http://blog.csdn.net/lanphaday 上,您可以转载.引用.打印和分发等,但必须保留本文完整和 ...

  6. JavaScript实现唯一路径问题的动态编程方法的算法(附完整源码)

    JavaScript实现唯一路径问题的动态编程方法的算法(附完整源码) dpUniquePaths.js完整源代码 dpUniquePaths.test.js完整源代码 dpUniquePaths.j ...

  7. JavaScript实现唯一路径问题的回溯方法的算法(附完整源码)

    JavaScript实现唯一路径问题的回溯方法的算法(附完整源码) btUniquePaths.js完整源代码 btUniquePaths.test.js完整源代码 btUniquePaths.js完 ...

  8. JavaScript实现squareMatrixRotation方阵旋转算法(附完整源码)

    JavaScript实现squareMatrixRotation方阵旋转算法(附完整源码) squareMatrixRotation.js完整源代码 squareMatrixRotation.test ...

  9. JavaScript实现递归楼梯问题(带记忆的递归解决方案)算法(附完整源码)

    JavaScript实现递归楼梯问题(带记忆的递归解决方案)算法(附完整源码) recursiveStaircaseMEM.js完整源代码 recursiveStaircaseMEM.test.js完 ...

  10. JavaScript实现递归楼梯问题(迭代解决方案)算法(附完整源码)

    JavaScript实现递归楼梯问题(迭代解决方案)算法(附完整源码) recursiveStaircaseIT.js完整源代码 recursiveStaircaseIT.test.js完整源代码 r ...

最新文章

  1. 程序员大神用 React “复刻”实现了一个 Windows 11
  2. sql注入学习——布尔盲注
  3. 小米拒绝权限_小米手机MIUI12真有那么好吗?
  4. 优秀学生专栏——董超
  5. 淘宝灵活的圆角框--通过一个圆形图片形成圆角原理
  6. Chrome浏览器扩展开发系列之一:初识Google Chrome扩展
  7. 适用于IE浏览器及非IE浏览器的xmlhttp脚本
  8. c语言程序设计答案四,C语言程序设计练习四(参考答案)
  9. ubb php论坛程序,论坛UBB代码 推荐
  10. 荔枝派通过usb烧录时出现ERROR: Allwinner USB FEL device not found!
  11. 加速Web开发的9款知名HTML5框架
  12. 【NLP】第 6 章:XGBoost 超参数
  13. 行人重识别论文阅读9-ViTAA:自然语言搜索中文本属性的视觉对齐
  14. C语言_printf
  15. 毕业季,让我来教你如何快速给论文进行排版
  16. Linux 中的rsh,ssh
  17. mobi文件怎么打开?
  18. C 彩色艺术化二维码样式设计(仅说思路)
  19. 【Python 骚操作】使用 Gitbook + Typora 打造一个属于自己的电子书网站
  20. Unreal Engin_画廊制作笔记 _004灯光处理,添加灯光

热门文章

  1. Android设置状态栏的字体颜色
  2. 深入了解一下C语言scanf()库函数
  3. 左手系与右手系 图片理解
  4. 程序员必学的计算机网络知识(数据链路层)
  5. 使用屏幕录制专家--录制视频技巧
  6. Predicting mRNA Abundance Directly from Genomic Sequence Using Deep Convolutional Neural Networks
  7. 一个数的所有连续自然数之和
  8. 华为云服务登录显示服务器异常,云服务器异常登录应该怎么处理
  9. 《惢客创业日记》2019.05.18(周六)视频通话后的一个创意
  10. View inflate 原理