编程以查找给定数字的尖叫声和数字总和,直到总和变为单个数字。 (例如,86 = 8 ^ 2 + 6 ^ 2 = 64 + 36 = 100 + 1 ^ 2 + 0 ^ 2 + 0 ^ 2 = 1))。 [英] Program to find the squears and sum of digits of a given number until the sum becomes a single digit. (e.g.86=8^2+6^2=64+36=100+1^2+0^2+0^2=1)) .

查看:71
本文介绍了编程以查找给定数字的尖叫声和数字总和,直到总和变为单个数字。 (例如,86 = 8 ^ 2 + 6 ^ 2 = 64 + 36 = 100 + 1 ^ 2 + 0 ^ 2 + 0 ^ 2 = 1))。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

eg1)86 = 8 ^ 2 + 6 ^ 2 = 64 + 36 = 100 + 1 ^ 2 + 0 ^ 2 + 0 ^ 2 = 1(如果我们得到单个数字为1那么它是完美数字)

例如2)2 = 2 ^ 2 = 4 = 4 ^ 2 = 16 = 1 ^ 2 + 6 ^ 2 = 1 + 36 = 37 = 3 ^ 2 + 7 ^ 2 = 9 + 49 = 58 = 5 ^ 2 + 8 ^ 2 = 25 + 64 = 89 = 8 ^ 2 + 9 ^ 2 = 64 + 81 = 145 = 1 ^ 2 + 4 ^ 2 + 5 ^ 2 = 1 + 16 + 25 = 42 = 4 ^ 2 + 2 ^ 2 = 16 + 4 = 20 = 2 ^ 2 + 0 ^ 2 = 4(它再次重复,因此它不是完美数字)

e.g.1)86=8^2+6^2=64+36=100+1^2+0^2+0^2=1 (If we get single digit as 1 then it is perfect number)
e.g2)2=2^2=4=4^2=16=1^2+6^2=1+36=37=3^2+7^2=9+49=58=5^2+8^2=25+64=89=8^2+9^2=64+81=145=1^2+4^2+5^2=1+16+25=42=4^2+2^2=16+4=20=2^2+0^2=4(it s repeating again so it is not perfect number)

推荐答案

转到尝试用伪代码定义算法,例如

Go and try to define the algorithm in pseudo code, e.g.
function test(int value) return void
begin
    attempts = 100
    while (attempts > 0 && value > 1) do
        write_line("value: {1}", value)
        attempts--
        text = as_string(value)
        value = 0;
        for each char c in split_into_chars(text) do
            value += char_square_to_int(c)
        end for each
    end while
    write_line("ended with value {0}", value)
end function

function char_square_to_int(char c) return int
begin
    return as_int(c-'0') * as_int(c-'0')
end function

转换为C#留作练习。

干杯

Andi

Transforming into C# is left as exercise.
Cheers
Andi

嗯,这就是家庭作业的气味:你应该这样做,我们可以帮助解决具体的问题。

此外,算法看起来很简单,你有什么疑问?
Well, that's smells of homework: you should do it, we can just help on specific problems.
Moreover, the algorithm looks pretty straighforward, what is your doubt about?


我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但我们不打算为你做这一切!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


这篇关于编程以查找给定数字的尖叫声和数字总和,直到总和变为单个数字。 (例如,86 = 8 ^ 2 + 6 ^ 2 = 64 + 36 = 100 + 1 ^ 2 + 0 ^ 2 + 0 ^ 2 = 1))。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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