- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这里是问题所在:https://leetcode.com/problems/happy-number/
我的解决方案:
static int count = 0;
public static void Main(string[] args)
{
Console.WriteLine(happyNumber(19));
Console.ReadLine();
}
public static bool happyNumber(int a)
{
double result = 0;
Stack<int> stapel = new Stack<int>();
//Split the integer into single digits and save them in a stack
while (a.ToString().Count() > 1)
{
stapel.Push(a % 10);
a = a / 10;
}
if (a.ToString().Count() == 1)
{
stapel.Push(a);
}
// Add the square of the digits to get the result
foreach (var item in stapel)
{
result += Math.Pow((double)item, 2);
}
// Check if it's a happy number
if(result == 1.0)
{
return true;
}
// counter to stop if it is a endless loop
else if(count < 100)
{
count++;
happyNumber((int)result);
}
return false;
}
所以输入 19 是一个快乐的数字,并且 if 子句在第 4 次运行中为真。您可以在 if(result == 1.0)
设置断点来检查它。那么为什么我的函数返回 false 呢?
最佳答案
您不必要地转换为替身。将 result
设为 int
而不是 double
(或者如果您担心结果会变成 long
对于 int
来说太大了)。将对 Math.Pow
的调用替换为手动平方 item
,如下所示:
result += item * item;
控制流不进入 if(result == 1.0)
block 的原因是浮点值在内部表示的方式。测试 double
之间的相等性是有问题的,因此(在这种情况下)您应该完全避免使用它们,因为它们是不需要的。
这里还有一个递归调用:
happyNumber((int)result);
但是,该调用没有任何作用,因为您实际上并未对返回值执行任何操作。考虑将该行替换为:
return happyNumber((int)result);
这将返回递归调用的值,而不是仅仅丢弃它。
关于c# - 即使 if 语句为真,函数也返回假,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30443517/
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Why can't Python handle true/false values as I expect?
我是不是遗漏了什么或者这是 ruby 中的错误? a = %w(foo bar baz) a.include? "foo" # => true a.size == 3
从 Modelica 文档来看,注释 Evaluate 似乎只对参数有影响: https://build.openmodelica.org/Documentation/ModelicaReferenc
为了避免嵌套的 if 语句并提高可读性,我想创建一个switch(true){ ... } Coldfusion 中的声明。我在 php 中经常使用这个,但是当我在 Coldfusion 中尝试这个时
嗨,我正在尝试处理 ajax json 响应 这是我的代码 success: function (j) { switch(true) { case (j.cho
我之前在我的 TF 代码中使用过这个: count = "${var.whatever == "true" ? 1 : 0}" 这非常适合我想要使用的东西。但是,我正在考虑如何最好地使用类似于说的
我之前在我的 TF 代码中使用过这个: count = "${var.whatever == "true" ? 1 : 0}" 这非常适合我想要使用的东西。但是,我正在考虑如何最好地使用类似于说的
这个问题在这里已经有了答案: How can I return pivot table output in MySQL? (10 个答案) 关闭 5 年前。 我正在尝试构建一个以唯一列值作为列名的表
我制作了一个简单的 JDialog,其中包含一个标签和一个按钮,它基本上相当于信息对话框。所以在对话框中,有一个方法 display() 我在其中调用了 setVisible(true) 五次。 据我
在 bash 4.2.8(1)-release (x86_64-pc-linux-gnu) 在 Ubuntu 11.04 上这个命令 [ $(wc -l /var/www/some.log|cut -
我正在使用 c 语言进行并发处理,我有一个进程池。为此,我让每个 child 都在一个 While (True) 循环中。为了杀死 child ,我正在使用一个全局变量和一个信号处理程序来修改它来打破
我正在尝试选择填写了字段的数据库条目。数据库有两种插入数据的方式,一种输入评论,一种不输入,我希望只选择填写了评论的行。 $requete = "SELECT * FROM daysoff WHER
如何在 JavaMail session 中setDebug(true) 捕获流并在我的日志记录框架中使用它? (缺少下载源代码,更改接受流作为参数的方法,重新编译它,...) 更一般地说,Java
我是 JavaScript 的新手,我刚刚发现了我无法理解的奇怪行为: var magicVar = Math.sin; magicVar == true; // it returns false m
对此感到困惑。 在两台服务器上运行相同版本的 MySQL。 (从完全相同的 rpm 构建)- 沿线的某个地方,一些开发人员改变了一些东西...... 服务器 1: mysql> select ( no
我在查看 OpenSSL 中使用的一些预处理器宏时,从 crypto/stack/safestack.h 中发现了以下内容: #define CHECKED_STACK_OF(type, p) \
所以我遇到了一个问题,我的正则表达式看起来像这样:/true|false/。 当我检查单词 falsee 时,我从这个正则表达式中得到一个 true,有没有办法将它限制为确切的 true 或 fals
我正在对这个恶意 JavaScript 行进行一些试验:var undefined = true; JavaScript 中每个未初始化的变量都有 undefined 的值,这只是一个保存特殊值 'u
我想将 PHP 的微时间存储为我在 MySQL 中的时间戳。 我去过told最好用 DECIMAL 存储它,但我找不到理想的大小。 有谁知道 microtime(true) 返回的最大大小是多少,所以
在 PHP 中,当您在 URL 中包含诸如“var=true”之类的内容时,URL 中的“true”和“false”是否被转换为 boolean 变量,或者它们是否等于文本“true”还是“假”?例如
我是一名优秀的程序员,十分优秀!