c语言编程时碰到取整去不了_碰到编程墙时如何解开

c语言编程时碰到取整去不了

Getting stuck is part of being a programmer, no matter the level. The so-called “easy” problem is actually pretty hard. You’re not exactly sure how to move forward. What you thought would work doesn’t.

无论身在何处,陷入困境都是成为程序员的一部分。 所谓的“简单”问题实际上很难解决。 您不确定如何前进。 您认为可行的方法无效。

The other part of being a programmer is getting yourself unstuck.

成为程序员的另一部分是让自己陷入困境。

I’ve been getting stuck a lot recently, so finding ways to get unstuck has been ever-present on my mind. Here are a few tactics I’ve been using. Maybe they can help you, too.

我最近一直被困住,所以一直想着找到解困的方法。 这是我一直在使用的一些战术。 也许他们也可以帮助您。

使问题具体化 (Make the problem concrete )

Create a diagram, make a quick sketch, or use actual objects to give yourself a visual. It’ll make the problem a lot easier to think about.

创建图表,快速绘制草图,或使用实际对象为自己提供视觉效果。 这将使问题更容易思考。

One problem I faced asked me to find the absolute difference between the sums of a square matrix’s diagonals. That’s a mouthful and a lot to keep in my head. So I drew it out: I created a square matrix of numbers and circled the diagonals.  

我遇到的一个问题要求我找出方阵对角线之和之间的绝对差。 那真是令人mouth目结舌,还有很多事需要我注意。 因此,我将其绘制出来:我创建了一个由数字组成的方阵,并圈出对角线。

A simple sketch literally made the steps jump out at me: sum one diagonal (which is 15), then the other (which is 17), and then find the absolute difference between them (which is 2).

一个简单的草图从字面上使我跳了一步:将一个对角线(为15)求和,然后将另一个对角线(为17)求和,然后找出它们之间的绝对差(为2)。

This approach applies to other problems, too. When I was learning about for loops, I iterated through a pile of almonds. When I’m working on a recursive problem, I’ll make a diagram to see what’s happening on the call stack until I hit my base case.

这种方法也适用于其他问题。 当我学习循环时,我遍历了一堆杏仁。 当我处理递归问题时,我将制作一个图表,以查看调用堆栈上正在发生的事情,直到找到基本情况为止。

The commonality is this: make the abstract concrete.

共同点是这样的:将抽象具体化。

准确写下您要做什么 (Write out exactly what you’re trying to do)

Write down the specific step you’re working on when you feel the all-too-familiar “spinning your wheels” cycle come upon you.  

当您感觉到非常熟悉的“旋转轮子”周期来到时,写下您正在执行的特定步骤。

The five or ten seconds it takes to jot down a few words on a piece of paper will help you solidify your thought process and redirect your attention.

在一张纸上写下几句话需要五到十秒钟,这将帮助您巩固思考过程并转移注意力。

Here are some examples:

这里有些例子:

  • Store the course names as keys in the object

    将课程名称存储为对象中的键
  • Pass the argument to the callback function

    将参数传递给回调函数
  • Reset the “maxValue” variable to 0

    将“ maxValue”变量重置为0

Resetting the “maxValue” variable, for example, didn’t solve the problem. But it was an important step in the process. Writing this short phrase got me back on track: it was a reminder of what I’d set out to do. It also ensured I was focused on one thing, not many.

例如,重设“ maxValue”变量并不能解决问题。 但这是该过程中的重要一步。 写这个简短的短语让我回到了正轨:这提醒了我要去做的事情。 这也确保我专注于一件事,而不是很多。

So the next time you find yourself trying the same approach over and over again and getting the same result, stop yourself and ask: “What exactly am I trying to do here?”

因此,下次您发现自己一次又一次地尝试相同的方法并获得相同的结果时,停下来问自己:“我在这里到底想做什么?”

Then, write—yes, write—your answer down on a piece of paper.

然后,在一张纸上写下(是,写下)您的答案。

It’s not enough to think of your response. If I casually “think” to myself, I’ll rush the process and not much (if anything) is gained. I’ve got to write it down.

仅仅考虑您的回应是不够的。 如果我随便对自己“思考”,我会匆忙完成该过程,但不会获得太多(如果有的话)。 我必须把它写下来。

简化您的给定输入 (Simplify your given input)

It’s far less intimidating to work with a few things than many. That's why it's helpful to simplify your given input.

处理几件事情比许多事情少得多。 这就是为什么简化给定输入会有所帮助的原因。

One problem gave me a list of three dictionaries. It was only three dictionaries, but that was still two too many.

一个问题给我列出了三本词典。 只有三本字典,但那仍然是两本太多。

names = [
    {'first':'John', 'last':'Smith', 'email':'johns@example.com'},
    {'first':'Mary', 'last':'McDonald', 'email':'marym@example.com'},
    {'first':'Sam', 'last':'Davey', 'email':'samd@example.com'}
]

My job was to sort each dictionary by last name, then by first name (ie, Davey, Sam: samd@example.com). However, the problem was easier to think about when I made the list of three dictionaries a list of one.  

我的工作是按姓氏,然后按名字(即Davey,Sam:samd@example.com)对每本词典进行排序。 然而, 当我将三本词典的列表设为一本的列表时,这个问题更容易思考。

name = [
    {'first':'John', 'last':'Smith', 'email':'johns@example.com'}
]

I solved the problem using a single dictionary. Then, I applied the same logic to the larger problem at hand.

我用一个字典解决了这个问题。 然后,我对即将出现的较大问题应用了相同的逻辑。

When you simplify your given input, you make the problem much more manageable.

当您简化给定的输入时,将使问题更易于管理。

解决一个较小的问题 (Solve a smaller problem)

Spot patterns more easily and understand what you're really asked to do when you solve a smaller version of the problem.

更轻松地找出模式并了解解决较小版本问题时的真正要求。

Here’s an example problem from Reuven Lerner’s book, Python Workout:

这是Reuven Lerner的书Python Workout中的一个示例问题:

“Use a list comprehension to reverse the word order of lines in a text file. That is, if the first line is abc def and the second line is ghi jkl, then you should return the list ['def abc', 'jkl ghi'].”

“使用列表推导来反转文本文件中各行的单词顺序。 也就是说,如果第一行是abc def,第二行是ghi jkl,则应该返回列表['def abc','jkl ghi']。”

When solving a smaller version of a problem, I find it helpful to remove layers of complexity and use my ideal data structure. In this example, that meant ignoring the text file and list comprehension (layers of complexity) and using a list (my ideal data structure).

解决较小版本的问题时,我发现消除复杂性层并使用理想的数据结构会有所帮助。 在此示例中,这意味着忽略文本文件和列表理解(复杂性层),而使用列表(我理想的数据结构)。

Then I solved the problem. I opened up my editor and typed out my ideal data structure.

然后我解决了问题。 我打开编辑器,输入理想的数据结构。

letters = ['abc def', 'ghi jkl']

I reversed the order and got the expected result using a for loop.

我颠倒了顺序,并使用for循环获得了预期的结果。

reversed_letters = []
for letter in letters:
   letter_list = letter.split(" ")
   letter_list.reverse()
   reversed_letters.append(" ".join(letter_list))

Once I got that working, I added the layers of complexity back in one at a time until I solved the problem as the problem statement asked.

一旦工作完成,我就一次又一次添加了复杂性,直到按照问题陈述的要求解决了问题。

Solving a smaller version of the problem helps get you to the heart of what you need to do. It's also another way to make the complex simple.

解决问题的较小版本有助于您深入了解所需要做的事情。 这也是使复杂结构简单的另一种方法。

休息一下 (Take a break )

Your brain doesn’t stop thinking just because your fingers stop typing.

您的大脑不会因为手指停止打字而停止思考。

Has an idea ever “popped” into your head while you were doing something other than programming? Have you ever returned to a problem after a workout and the solution is staring you in the face? It’s happened to me.

当您进行编程以外的工作时,有没有一个主意“突然出现”? 运动后,您是否曾经遇到过问题,而解决方案却使您目不转睛? 这是发生在我身上。

It’s no coincidence that you arrived at your idea or solution when you were doing something else—when you weren’t deliberately working. “Epiphanies may seem to come out of nowhere,” explains Scientific American, “but they are often the product of unconscious mental activity during downtime.”

当您在做其他事情时(不是在刻意工作的时候)得出您的想法或解决方案,这并非巧合。 《 科学人杂志 》解释说:“突然显现似乎无处不在,但它们通常是停机期间无意识的精神活动的产物。”

If you feel like you’re running up against a brick wall, you very well could be. It may be best to take a break. Give your mind some time to digest what you’re working on and return to the problem renewed.

如果您感觉自己撞上了砖墙,那很可能是这样。 最好休息一下。 给您一些时间来消化您正在做的事情,然后重新解决问题。

与其他程序员配对 (Pair with another programmer)

Working with someone else can be a great way to generate ideas and see a problem from another perspective. But I highly recommend you do everything in your power to get yourself unstuck first.

与他人合作可能是产生想法并从另一个角度看问题的好方法。 但我强烈建议您尽一切可能先将自己粘在身上。

There will always be roadblocks. Learning how to troubleshoot your own problems is a critical skill to learn. It’s easy to say “I don’t get it. Let me ask this senior engineer.” Then, have the senior engineer solve the problem for you. It’s harder to figure it out yourself, but you need to at least try.

总会有障碍。 学习如何解决自己的问题是学习的一项关键技能。 容易说“我不明白。 让我问这个高级工程师。” 然后,请高级工程师为您解决问题。 自己弄清楚很难,但至少需要尝试一下。

If you’ve sincerely put your best foot forward, then reach out to a programmer with a specific question. This shows respect for the other programmer’s time and it’ll make your pairing session more effective.

如果您真诚地提出了自己最好的建议,那么请向程序员咨询特定的问题。 这表示尊重其他程序员的时间,这将使您的配对会话更加有效。

Then, ask for a hint—don’t have the programmer solve the problem for you. You’ll undoubtedly encounter a similar situation down the road, so use the pairing session as a learning opportunity. It’ll help you in the long run.

然后,寻求提示-不要让程序员为您解决问题。 毫无疑问,您将来会遇到类似的情况,因此请使用配对课程作为学习机会。 从长远来看,它将为您提供帮助。

结语 (Wrapping up)

Getting stuck is frustrating, to be sure. You may try just one of the above tactics and have a “light bulb” moment, or you may need to try a combination and find yourself simply inching along during the process.

当然,卡住令人沮丧。 您可能只尝试了上述一种策略,并有一个“灯泡”时刻,或者您可能需要尝试一种组合,并发现自己在此过程中只是微不足道。

But there are two things I’ve learned from embracing the struggle: I’m learning a lot and the breakthrough is coming. Keep at it.

但是,从这场斗争中我学到了两件事:我学到了很多东西,并且突破即将来临。 继续吧

I write about learning to program, and the best ways to go about it (amymhaddad.com).

我写了有关学习编程的文章,以及进行编程的最佳方法 ( amymhaddad.com )。

翻译自: https://www.freecodecamp.org/news/how-to-get-unstuck/

c语言编程时碰到取整去不了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值