程序显示从1到500的所有阿姆斯特朗号码 [英] Program to display all armstrong numbers from 1 to 500

查看:130
本文介绍了程序显示从1到500的所有阿姆斯特朗号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能告诉我问题是什么,用我创建的代码,显示从1到500的Armstong数字?



我有什么试过:



Can anyone tell me what the problem is,with the code I have created,to display Armstong numbers from 1 to 500?

What I have tried:

#include <stdio.h>
#include <conio.h>

void main()
{
  int i,r,s=0,a;
  for(i=1;i<=500;i++)
  {
      a=i;
      while(i>0)
      {
          r=i%10;
          s=s+(r*r*r);
          i=i/10;
      }
      if(a==s)
      {
          printf(" %d ",a);
      }
  }
  getch();
}

推荐答案

这实际上非常明显,但只有当你看到它时才会这样。

使用调试器会直接显示问题所在。



在函数的第一行放置一个断点,然后通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


是的,我可以告诉你问题是什么,但这是一个非常简单的例子,也是一个非常简单的错误,所以这是一个很好的机会让你获得解决问题的经验 - 时间让你学习一个新的(并且非常,非常有用)技能:调试!



提示:密切关注s。
It's actually really obvious, but only when you look at it.
And using the debugger would show you exactly what the problem is straight away.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Yes, I could tell you what the problem is, but this is a very simple example, and a very simple bug, so it's a good opportunity for you to get experience in working out problems - time for you to learn a new (and very, very useful) skill: debugging!

Hint: keep a close eye on "s".


其中一个问题是OG建议的 s = 0 的位置。



有一个工具可以让你看到什么你的代码正在做,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里显示你的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
One of the problems is the position of s=0 as OG suggested.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于程序显示从1到500的所有阿姆斯特朗号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆