ARMSTRONG NUMBERS。我的程序出现问题 - 循环一直工作到149但是一旦达到150就会产生问题。 [英] ARMSTRONG NUMBERS. Problem with my program -- the loop works till 149 but creates problem as soon as it gets to 150.

查看:144
本文介绍了ARMSTRONG NUMBERS。我的程序出现问题 - 循环一直工作到149但是一旦达到150就会产生问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阿姆斯壮的数字是一个数字,它等于数字的第n个幂的总和。

所以,我必须打印1到500之间的阿姆斯壮数字。有12个它们总是介于这个范围之间。



我编写的程序,它实际上并没有打印出超过9的Armstrong数字。所以我决定通过打印数字来测试它我得到了上面提到的'sum'。

这就是我发现循环在149以上的工作方式。每当它达到150时,程序以某种方式从'sum'中减去1。 <罢工>请帮助,因为我必须在一天中提交它。



我尝试过: < br $> b $ b

An Armstrong number is a n digit number, which is equal to the sum of the nth powers of its digits.
So, i have to print Armstrong numbers between 1 and 500. there are 12 of them in total between the range.

the program i have written, it actually was not printing Armstrong numbers over 9. so i decided to test it by printing the numbers that i get from the 'sum' mentioned above.
this is how i found out that the loop was working above 149. Whenever it reaches 150, the program somehow subtracts 1 from the 'sum'. PLEASE HELP AS I HAVE TO SUBMIT IT IN A COUPLE OF DAYS.

What I have tried:

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    int di;
    for(int a = 1, b = a, c = a, sum = 0, digi = 0;a <= 150;a++, b = a, digi = 0, c = a, sum = 0)
    {
        for(b;b != 0;b /= 10, digi++);
        do
        {
            di = c % 10;
            sum += pow(di, digi);
            c /= 10;
        }while(c != 0);
        cout << sum << " ";
        // if(sum == a) cout << sc << " ";
    }
}

推荐答案

Quote:

所以,我必须打印1到500之间的Armstrong数字。

So, i have to print Armstrong numbers between 1 and 500.



整数变量无法容纳这么多数字。 int 不能超过10位数。

您应该研究不同的C数据类型。


哎呀,在数字和位数之间出错了




There is no way an integer variable can hold so much digits. An int can't hold much more than 10 digits.
You should study the different C data types.
Oops, made a mistake between the values and number of digits

for(int a = 1, b = a, c = a, sum = 0, digi = 0;a <= 150;a++, b = a, digi = 0, c = a, sum = 0)



没有必要输入这样的代码,没有任何收获,只会让它更难阅读和理解。

任何收益代码行数以牺牲可读性为代价。



[更新]

建议:让代码更容易阅读。

然后使用调试器查看代码执行情况,它将帮助您理解它出错的原因。


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

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

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



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



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

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

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。如果代码没有达到预期的效果,则接近错误。


There is no point to type such code, there is no gain, it only make it more difficult to read and to understand.
Any gain in number of lines of code is at the expense of readability.

[Update]
Advice: make your code easier to read.
Then use the debugger to see your code perform, it will help you to understand why it go wrong.

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.

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.


您可以使用sprintf()将每个值(从1到500)转换为可显示的字符串。字符串长度将是位数(即指数),并且从数组中的每个字符减去0(使用循环)将为您提供数字值,并且您已经拥有指数。

以这种方式编码应该更容易,你应该能够提交几个小时。
You can use sprintf() to convert each value (from 1 to 500) to a displayable string. The string length will be the number of digits (i.e. exponent) and subtracting '0' from each char in the array (using a loop) will give you the digit value and you already have the exponent.
Coding this way should be easier and you should be able to 'submit in a couple of days hours'.


这篇关于ARMSTRONG NUMBERS。我的程序出现问题 - 循环一直工作到149但是一旦达到150就会产生问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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