ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

INT-102_Lecture_01

2022-03-02 09:32:27  阅读:202  来源: 互联网

标签:do product 01 INT sum statement 102 output Loop


Part I

  • Some Well-known Computational Problems

    • Sorting
    • Searching
    • Graph and Combinatorial Problems
      • Shortest paths in a graph---【最短路】
      • Minimum spanning tree
      • Traveling salesman problem
      • Knapsack problem
      • ...
    • Problem in Number Theory
      • Primality testing
      • ...
  • What is an algorithm?

    • A sequence of precise and concise 【精确且简洁】instructions that guide you (or a computer) to solve a class of specific problem
  • Learning Aims

    • To give an overview of the study of algorithms in terms of their efficiency
    • To introduce the standard algorithmic design patterns 【设计模式】employed in the development of efficient algorithmic solutions
    • To describe the analysis of algorithms in terms of the use of formal models of Time and Space
    • To give a brief introduction to the subject of computational complexity theory 【计算复杂度】and its use in classifying computational problems

Part II: How to represent algorithms

  • Pseudo Code

    • Trace Table & Statement

      • p = 1
        for i = 1 to n do
            p = p * x
        output p
        
    •  Statement
      • Assignment statement【赋值语句】 
        • Variable = Expression
        • e.g., assign the expression 3 * 4 to the variable result :
          • result = 3 * 4
      • Compound statements 【复合语句】
        • begin
              statement1
              statement2
          end
      • Conditional statement 【条件语句】
        • if condition then
              statement
        • if condition then
              statement1
          else
              statement2
        • Example:
          • if a < 0 then    
                a = -a
                abs = a
            output abs
          • if a > 0 then
                abs = a
            else
                abs = -a 
            output abs
      • Iterative statement 【迭代语句】
        • For Loop 【For 循环】
          • for var = start_value to end_value do
                statement
          • Example: Computing sum of the first n numbers
            • input n
              sum = 0
              for i = 1 to n do
              begin
                  sum = sum + i
              end
              output sum
            • The loop is executed for n times
        • While Loop 【While循环】
          • while condition do
                statement
          • Condition__CONTINUE the loop
          • Example_01: Computing sum of the first n numbers
            • input n
              sum = 0
              i = 1 
              while i <= n do
              begin
                  sum = sum + i
                  i = i + 1
              end
              output sum
          • Example_02: Computing sum of all (keyboard) input numbers
            • sum = 0
              while (user wants to continue) do
              begin
                  ask for a number
                  sum = sum + number
              end
              output sum
            • This loop is executed for an undetermined number of times
        • Repeat-until Statement 【Repeat循环】
          • repeat
                statement
            until condition
          • Condition__STOP the loop
          • Example: Computing sum of all (keyboard) input numbers
            • sum = 0
              repeat
                  ask for a number
                  sum = sum + number
              until (user wants to stop)
              output sum
  • Computing the n-th power

    • Input
      • A number x & A non-negative integer n
    • Output
      • The n-th power of x
    • Algorithm
      • Set a temporary variable p to 1
      • Repeat the multiplication p = p * x for n times
      • Output the result p
  • Algorithm vs Program

    • Algorithms
      • Free from grammatical rules
      • Content is more important than form
      • Acceptable as long as it tells people how to perform a task
    • Programs
      • Must follow some syntax rules
      • Form is important
      • Even if the idea is correct, it is still not acceptable if there is a syntax error
    • More Generally: Program = Data Structure + Algorithm

Part III: Pseudo Code_Exercise

  • Ex_01:List all factors of a given positive integer x
    •  For Loop
      • for i = 1 to x do
        begin
            if (x%i == 0) then
                output i
        end
    • While Loop
      • i = 1
        while i <= x do
        begin
            if x%i == 0 then
                output i
            i = i+1
        end
        
        
    • Repeat Loop
      • i = 1
        repeat
            if x%i == 0 then
                output i
            i = i+1
        until i > x
  • Ex_02:Find the product of all integers in the interval [x, y], assuming that x and y are both integers
    • For Loop
      • product = 1
        for i = x to y do
        begin
            product = product * i
        end
        output product
        
    • While Loop
      • product = 1
        i = x
        while i <= y do
        begin
            product = product * i
            i = i+1
        end
        output product
        
    • Repeat Loop
      • product = 1
        if x <= y then begin
            i = x
            repeat
                product = product * i
                i = i+1
            until i > y
            output product
        end

Part IV: Summary

  • While
    • 判断==>执行
    • 满足条件,执行,继续循环
  • Repeat
    • 执行==>判断
    • 满足条件,不执行,结束循环
  • Loop
    • 执行==>判断
    • 满足条件,不执行,结束循环
  • 当条件为False时,Repeat循环也能执行一次,类似于 java中的 Do While 循环; 而 While、Loop 循环无法执行

标签:do,product,01,INT,sum,statement,102,output,Loop
来源: https://blog.csdn.net/Astronaut_WHY/article/details/123221502

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有