「遞增法」是符合電腦運作特性的方法。電腦執行程式,一次只做一個動作,完成了一件事才做下一件事。當一個問題太大太多時,化整為零、一個一個解決吧!

合抱之木,生於毫末;九層之臺,起於累土;千里之行,始於足下。謹以此句與大家共勉。

範例:加總數字

無論電腦再怎麼強,還是得一個一個累加數字。

  1. void summation()
  2. {
  3. int array[5] = {3, 6, 9, -8, 1};
  4. int sum = 0;
  5. for (int i=0; i<5; i++)
  6. sum += array[i];
  7. cout << "總和是" << sum;
  8. }
  1. int summation(int array[], int n)
  2. {
  3. int sum = 0;
  4. for (int i=0; i<n; i++)
  5. sum += array[i];
  6. return sum;
  7. }

範例:複製字串

無論電腦再怎麼強,還是得逐字複製。

  1. void copy()
  2. {
  3. char s[15] = "incremental";
  4. char t[15];
  5. int i;
  6. for (i=0; s[i] != '\0'; i++)
  7. t[i] = s[i];
  8. t[i] = '\0';
  9. cout << "原本字串" << s;
  10. cout << "複製之後的字串" << t;
  11. }
  1. void copy(char* s, char* t)
  2. {
  3. int i;
  4. for (i=0; s[i]; i++)
  5. t[i] = s[i];
  6. t[i] = '\0';
  7. }

範例:選擇排序法( Selection Sort )

找到第一小的數字,放在第一個位置;再找到第二小的數字,放在第二個位置。一次找一個數字,如此下去就會把所有數字按照順序排好了。

  1. void selection_sort()
  2. {
  3. int array[5] = {3, 6, 9, -8, 1};
  4. for (int i=0; i<5; i++)
  5. {
  6. // 從尚未排序的數字當中,找到第i小的數字。
  7. int min_index = i;
  8. for (int j=i+1; j<5; j++)
  9. if (array[j] < array[min_index])
  10. min_index = j;
  11. // 把第i小的數字,放在第i個位置。
  12. swap(array[i], array[min_index]);
  13. }
  14. // 印出排序結果。
  15. for (int i=0; i<5; i++)
  16. cout << array[i];
  17. }
  1. void selection_sort(int array[], int n)
  2. {
  3. for (int i=0; i<n; i++)
  4. {
  5. // 從尚未排序的數字當中,找到第i小的數字。
  6. int min_index = i;
  7. for (int j=i+1; j<n; j++)
  8. if (array[j] < array[min_index])
  9. min_index = j;
  10. // 把第i小的數字,放在第i個位置。
  11. swap(array[i], array[min_index]);
  12. }
  13. }

範例:印出直角三角形

多字成行,多行成直角三角形。由細微的東西開始,一件一件組起來。

  1. // 多字成行
  2. void print_line(int n)  // n 是一行的長度
  3. {
  4. for (int i=1; i<=n; i++) cout << '@';
  5. cout << '\n';
  6. }
  7. // 多行成直角三角形
  8. void print_triangle(int n)  // n 是行數
  9. {
  10. for (int i=n; i>=1; i--) print_line(i);
  11. }

Incremental Method的更多相关文章

  1. Oracle E-Business Suite Maintenance Guide Release 12.2(Patching Procedures)

    更多内容参考: http://docs.oracle.com/cd/E51111_01/current/acrobat/122ebsmt.zip Preparing for Patching For ...

  2. (三)ORB特征匹配

    ORBSLAM2匹配方法流程 在基于特征点的视觉SLAM系统中,特征匹配是数据关联最重要的方法.特征匹配为后端优化提供初值信息,也为前端提供较好的里程计信息,可见,若特征匹配出现问题,则整个视觉SLA ...

  3. Android API之android.provider.ContactsContract.RawContacts

    android.provider.ContactsContract.RawContacts Constants for the raw contacts table, which contains o ...

  4. 07-THREE.JS 各种形状的几何图形

    <!DOCTYPE html> <html> <head> <title>Example 02.04 - Geometries</title> ...

  5. Apache Spark 2.2.0 中文文档

    Apache Spark 2.2.0 中文文档 - 快速入门 | ApacheCN Geekhoo 关注 2017.09.20 13:55* 字数 2062 阅读 13评论 0喜欢 1 快速入门 使用 ...

  6. SEMAT[软件工程方法和理论 Software Engineering Method and Theory]

    Agile software development Agile software development is a group of software development methods bas ...

  7. 201904Online Human Action Recognition Based on Incremental Learning of Weighted Covariance Descriptors

    论文标题:Online Human Action Recognition Based on Incremental Learning of Weighted Covariance Descriptor ...

  8. 2.5 – Garbage Collection 自动垃圾回收 Stop-the-world vs. incremental vs. concurrent 垃圾回收策略

    2.5 – Garbage Collection  自动垃圾回收 Lua 5.3 Reference Manual http://www.lua.org/manual/5.3/manual.html# ...

  9. V4 Reduce Transportable Tablespace Downtime using Cross Platform Incremental Backup (Doc ID 2471245.1)

    V4 Reduce Transportable Tablespace Downtime using Cross Platform Incremental Backup (Doc ID 2471245. ...

随机推荐

  1. 学习mybatis-3 step by step 篇二

    Mapper XML 文件 基本的*Mapper.xml文件配置就不熬述了具体可参考: http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html 1.sq ...

  2. Java String 中的一些函数与正则的结合使用

    首先正则表达式在处理字符串问题时,真的非常强大. 正则可以帮助我们处理字符串的: 匹配, 选择, 编辑, 验证等问题. 正则中"\\"表示插入一个"\" 这里仅 ...

  3. hihocoder 九十八周 搜索一 24点

    题目1 : 搜索一·24点 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 周末,小Hi和小Ho都在家待着. 在收拾完房间时,小Ho偶然发现了一副扑克,于是两人考虑用这副 ...

  4. SQLServer中获取所有数据库名、所有表名、所有字段名的SQL语句

    ----1. 获取所有的数据库名----- SELECT NAME FROM MASTER.DBO.SYSDATABASES ORDER BY NAME -----2. 获取所有的表名------ S ...

  5. LINQ 系列

    C#图解教程 第十九章 LINQ   LINQ 什么是LINQLINQ提供程序 匿名类型 方法语法和查询语法查询变量查询表达式的结构 from子句join子句什么是联结查询主体中的from…let…w ...

  6. (GoRails )使用Vue.js制作拖拉list功能(v5-8)

    视频5 改进视觉效果,让list看起来更舒服.新增横向滚动功能. 参考我的trello:https://trello.com/b/BYvCBpyZ/%E6%AF%8F%E6%97%A5%E8%AE%B ...

  7. 3-18 关于namespace,双冒号::的用法; SelfYield.

    关于namespace,双冒号::的用法. 防止引用多个模块在一个文件/类中,有重名的对象.::可以调用类的类方法,和常量. class Foo   BAR = "hello"   ...

  8. oracle EBS上传和下载文件(转)

    最近一直在做一个工作流的项目,最终用户要求在发送消息的时候可以附带附件,这个又是给我的一个难题.在网上查了一下ORACLE上传资料,找到了黄建华前辈写的<Oracle EBS Forms开发指南 ...

  9. 10个CSS简写/优化技巧-摘自网友

    10个CSS简写/优化技巧23来源/作者:未知 类别:前端开发 字体大小:大|中|小 背景颜色:蓝|白|灰 ? ? CSS简写就是指将多行的CSS属性简写成一行,又称为CSS代码优化或CSS缩写.CS ...

  10. 快速切题 sgu119. Magic Pairs

    119. Magic Pairs time limit per test: 0.5 sec. memory limit per test: 4096 KB “Prove that for any in ...