Prime numbers program

后端 未结 14 1645
无人及你
无人及你 2021-01-07 00:27

I\'m currently trying out some questions just to practice my programming skills. ( Not taking it in school or anything yet, self taught ) I came across this problem which re

14条回答
  •  一个人的身影
    2021-01-07 00:55

    Since your question is about programming rather than math, I will try to keep my answer that way too.

    The first glance of your code makes me wonder what on earth you are doing here... If you read the answers, you will realize that some of them didn't bother to understand your code, and some just dump your code to a debugger and see what's going on. Is it that we are that impatient? Or is it simply that your code is too difficult to understand for a relatively easy problem?

    To improve your code, try ask yourself some questions:

    1. What are a, b, c, etc? Wouldn't it better to give more meaningful names?
    2. What exactly is your algorithm? Can you write down a clearly written paragraph in English about what you are doing (in an exact way)? Can you modify the paragraph into a series of steps that you can mentally carry out on any input and can be sure that it is correct?
    3. Are all steps necessary? Can we combine or even eliminate some of them?
    4. What are the steps that are easy to express in English but require, say, more than 10 lines in C/C++?
    5. Does your list of steps have any structures? Loops? Big (probably repeated) chunks that can be put as a single step with sub-steps?

    After you have going through the questions, you will probably have a clearly laid out pseudo-code that solves the problem, which is easy to explain and understand. After that you can implement your pseudo-code in C/C++, or, in fact, any general purpose language.

提交回复
热议问题