我如何编写解决这个简单问题的程序? ps:我是c ++编程的新手 [英] How do i write a program that solve this easy question ? ps : i am new in c++ programming

查看:48
本文介绍了我如何编写解决这个简单问题的程序? ps:我是c ++编程的新手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找到数字'sqdnumber'的各个数字的平方和,并将该和存储在变量'sqdNumber_result'中。例如。如果数字是234,则总和计算为(2 * 2 + 3 * 3 + 4 * 4 = 4 + 9 + 16 = 29)



我尝试了什么:



i尝试使用循环,但我无法实现让这个程序活着的想法

Find the sum of squares of individual digits of a number 'sqdnumber' and store the sum in variable 'sqdNumber_result'. E.g. if the number is 234, the sum is computed as (2*2 + 3*3 + 4*4 = 4 + 9 + 16 = 29)

What I have tried:

i have tried using loops but i cant reach the idea to make this program alive

推荐答案

一旦你能够从数字中提取n th 数字,任务就很简单了。一种方法如下:

once you are able to extract the nth digit from the number, the task is trivial. One way is the following:
while (i)
{
  int d = i % 10; // at each iteration d is one digit extracted from i
  i = i / 10;
}


这里的难点是提取数字。

你可以这样做一个循环。



通过模式10提取单位中的数字 - > digit = number%10

要继续循环,请将数字除以10 - > number / = 10



我相信你可以自己解决剩下的问题。



提示:

使用调试器将是一个很好的帮助。

但是如果你这样做很麻烦,尝试输出循环中每个变量的值。
The difficult part here is to extract the digits.
You can do this in a single loop.

Extract the digit in the units place by doing a mod 10 -> digit = number % 10.
To continue the loop reduce the number by dividing it by 10 -> number /= 10.

I'm sure you can figure out the rest by yourself.

Tip:
Using the debugger will be a great help.
But if you have trouble doing that, try outputting the values of each variable inside the loop.


我们不做你的HomeWork。

建议:

- 首先要正确学习语言,结构,循环,分支...

- 学习一些分析方法, Dijkstra自上而下方法是一个好的开始。

https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design [ ^ ]

https://en.wikipedia.org/wiki/Structured_programming [ ^ ]

https: //en.wikipedia.org/wiki/Edsger_W._Dijkstra [ ^ ]

https://www.cs .utexas.edu / users / EWD / ewd03xx / EWD316.PDF [ ^ ]

- 学习调试器。
We don't do your HomeWork.
Advice:
- Start by learn properly the language, structures, loops, branch ...
- Learn some analyze methods, Dijkstra Top-Down method is a good start.
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]
- Learn the Debugger.


这篇关于我如何编写解决这个简单问题的程序? ps:我是c ++编程的新手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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