如何编写代码来检查输入的数字是否为满意数字? [英] How do I write a code to check if the input number is happy number or not?

查看:92
本文介绍了如何编写代码来检查输入的数字是否为满意数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include   <   iostream.h  >  
#include < conio.h >
void happy_no()
{
int sum = 0 ,num,i,d,c = 0 ;
cout<< 输入一个数字:; CIN>> NUM;
for (i = 1 ; i< = 10 ; i ++)
{
d = num%10;
sum + =(d * d);
num / = 10 ;
if (sum == 1
{
c ++;
break ;
}
num = sum;
sum = 0 ;
}
如果(c == 1
cout< < HAPPY NUMBER;
else
cout<< 不是一个快乐的数字;
}
void main()
{clrscr();
happy_no();
getch();
}





我的尝试:



如果你重复这个过程,一个数字被称为一个快乐的数字,用于平方数字的总和,直到获得值1。但是,这继续求和并且检查sum = 1应该在第10次迭代之前发生。



此外,当我在visual studio上输入这段代码时,这段代码可以工作,当我在visual studio中做同样的事情时,它却没有。如果你能告诉我更正和原因,IT真的意味着很多。

解决方案

你的代码没有按照你的问题描述。正确的顺序是:

输入数字
DO
将数字拆分为其组成数字
将这些数字加在一起得到总和
计算总和
的平方如果答案等于1从循环中断
如果完成10次迭代则从循环中断
设定数等于总和
重复这个过程
END


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

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

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



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



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

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

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

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期效果时,你就接近了一个bug。

-----

建议:

- 学习一种或多种分析方法, EW Djikstra自上而下的方法是一个良好的开端。

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 [ ^

#include<iostream.h>
#include<conio.h>
void happy_no()
{
	int sum=0,num,i,d,c=0;
	cout<<"enter a number: ";	cin>>num;
	for(i=1;i<=10;i++)
	{	
		d=num%10;
		sum+=(d*d);
		num/=10;
		if(sum==1)
		{
			c++;
			break;
		}
		num=sum;
		sum=0;
	}
	if(c==1)
		cout<<"HAPPY NUMBER";
	else
		cout<<"NOT A HAPPY NUMBER";
}
void main()
{clrscr();
happy_no();
getch();
}



What I have tried:

A number is called a happy number, if you repeat the process, of squaring the sum of the digits, till the value 1 is obtained. But, this continues summing and checking for sum = 1 should happen before the 10th iteration.

Further more, when I type this code on to visual studio, this code works, and when I do the same in visual studio, it doesn't. IT will really mean alot if you could tell me the correction and the reason.

解决方案

Your code is not doing what your question describes. The correct sequence is:

Input a number
DO
    Split the number into its constituent digits
    Add those digits together to get the sum
    Calculate the square of the sum
    If the answer is equal to 1 break from the loop
    If completed 10 iterations break from the loop
    set number equal to sum
    repeat the process
END


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.
-----
Advice:
- Learn one or more analyze methods, E.W. Djikstra 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[^]


这篇关于如何编写代码来检查输入的数字是否为满意数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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