首页 > 其他分享 >The C Learning Note - 1

The C Learning Note - 1

时间:2022-10-29 19:00:36浏览次数:34  
标签:prime code programming problems Note Learning problem class

Think before you code

Many novice programmers attempt to dive right into writing the code (in the programming language) as the first step. However, writing the code is actually a much later step in the process.
A good programmer will plan first and write second, possibly breaking down a large programming task into several smaller tasks in the process. Even when cautioned to plan first and code second, many programming students ignore the advice—after all, why “waste” 30 minutes planning when you are time-crunched from all the work you have to do.
This tradeoff, however, presents a false economy—30 minutes planning could save hours of trying to make the code work properly. Well planned code is not only more likely to be correct (or at least closer to correct), but is also easier to understand—and thus fix.

Why?

To try to better understand the importance of planning before you write, imagine an analogy to building a house or sky scraper. If you were tasked with building a sky scraper, would you break ground and start building right away, figuring out how the building is designed as you go?
Hopefully not. Instead, you (or an architect) would design blueprints for the building first. These blueprints would be iteratively refined until they meet everyone’s specifications—they must meet the requirements of the building’s owner, as well as be possible to build reasonably. Once the blueprints are completed, they must be approved by the local government. Actual construction only begins once the plans are fully completed.
Programming should be done in a similar manner— come up with a complete plan (algorithm) first and build (implement in code) second.

The core of Programing

We said that the heart of programming is to figure out how to solve a class of problems—not just one particular problem. The distinction here is best explained by an example. Consider the task of figuring out if a particular number (e.g., 7) is prime. With sufficient knowledge of math (i.e., the definition of a prime number and the rules of division), one can solve this problem—determining that 7 is in fact prime. 
However, a programming problem typically looks at a more general class of problems. We would typically not write a program to determine if 7 is prime, but rather a program which, given a number N, determines if N is prime.
Once we have an algorithm for this general class of problems, we can have the computer solve any particular instance of the problem for us.

The road to solving——parameters(Why do we define the data type)

When we examine a class of problems, we have parameters which tell us which particular problem in the class we are solving.
In the previous example, the class of problems is parameterized by N—the number we want to test for primality. To develop an algorithm for this class of problems, we must account for all possible legal values of the parameters.
As we will see later, programming languages let us restrict what type of information a parameter can represent, to limit the legal values to those which make sense in the context of the problem. For primality testing, we would want our parameter N to be restricted such that it can only hold integer numbers. It would not make any sense to check if letters, words, or files are prime.

Right Way

To write a program which takes any number N and determines if N is prime, we must first figure out the algorithm for this class of problems.
As we said before, if we attack the problem by blindly writing code, we will end up with a mess—much like constructing a sky scraper with no plan. Coming up with the appropriate algorithm for a class of problems is a challenging task, and typically requires significant work and thought.

 

标签:prime,code,programming,problems,Note,Learning,problem,class
From: https://www.cnblogs.com/Xweop/p/16839406.html

相关文章

  • Bitcoin Notes
    Abstract.Apurelypeer-to-peerversionofelectroniccashwouldallowonlinepaymentstobesentdirectlyfromonepartytoanotherwithoutgoingthroughaf......
  • Bookxnote+坚果云实现双PC同步
    Bookxnote软件介绍坚果云介绍正文开始总体思路:一台电脑为A,另一台电脑为B,将A电脑本地的文件夹关联坚果云同步文件夹,并在B电脑中重复此操作坚果云上新建一个同步文件夹......
  • 通过SSH远程使用jupyter notebook
    1.背景一直苦恼于本地机器和服务器上都要配置一些机器学习方面的环境,今天花了点时间研究了下Jupternotebook远程访问服务器,所以记录一下。有些步骤非必须,这里尽量写清楚......
  • 【ACMMM 2022】Learning Hierarchical Dynamics with Spatial Adjacency for Image En
    【ACMMM2022】LearningHierarchicalDynamicswithSpatialAdjacencyforImageEnhancement代码:https://github.com/DongLiangSXU/HDM该论文的研究动机:近年来动态网......
  • notepad++格式化json
    1、JsonView插件地址:https://百度网盘/s/11LJU_9ZI0H5TUOPMbE7jRg   验证码:rsjk​2、使用插件把插件放入到notepad++的安装目录下的plugin下3、使用notepad++格式化json......
  • JavaScript进阶(Learning Records)
    背景:对JavaScript的深入学习参考:《JavaScript高级程序设计》《冴羽JavaScript深入》从原型到原型链prototypeprototype是每个函数都会有的属性functionPerson(){......
  • OpenCV-Python learning-13.人脸检测
    如下,调用opencv使用摄像头或视频进行人脸检测,也可以在函数​​recognize(img)​​​传入​​img=cv2.imread('face.jpg')​​​。其中,人脸级联分类器xml文件我引用的是anaco......
  • Codeforces 1672 E. notepad.exe
    题意这是一道交互题,有n个字符串,每个字符串长度:0-2000,n:0-2000有一个机器对他进行排版,你可以给他一个每行的最大宽度w,那么每行只能放长度为w的字符;每行相邻两个字符......
  • JAVA DAY1 LEARNING NOTE- Markdown-2022-10-26
    Markdown学习标题三级标题字体Helloworld!Helloworld!Helloworld!Helloworld!引用选择Java走向人生巅峰分割线图片超链接点击跳转到博客列表A......
  • OpenCV-Python learning-9.图像阈值处理
    你也可以​​iframe外链​​查看。本节内容包括:常用阈值方法自适应阈值Otsu(大津法)自适应阈值​​github地址​​......