使用C#的Collat​​z序列号 [英] Collatz sequence number using C#

查看:75
本文介绍了使用C#的Collat​​z序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要从数字中获取Collat​​z序列,如果是偶数,则将其除以2,如果是奇数,则将其乘以3并加1。继续对上一个操作的结果进行操作,直到数字变为1.



当你看到代码底部时,我试图列出一行中的数字打印行我想通过Console.ReadLine()得到另一个值;但它不符合我的意图。



你能给我一些分数或提示,以便我能解决吗?



谢谢你们。



我尝试了什么:



To get a Collatz sequence from a number, if it's even, divide it by two, and if it's odd, multiply it by three and add one. Continue the operation on the result of the previous operation until the number becomes 1.

As you see the code bottom, I tried to list the number in a line and after the line is printed I want to get another value by Console.ReadLine(); but it does not do what I intended.

Can you please give me some points or hints so I can fix it?

Thank you guys.

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercise
{
	class Program
	{
		static void Main(string[] args)
		{
			int sequenceNumber = Convert.ToInt32(Console.ReadLine());
			List<int> list = new List<int>();

			while (sequenceNumber>=1)
			{
				if (sequenceNumber == 1)
				{
					Console.WriteLine(1);
					sequenceNumber = Convert.ToInt32(Console.ReadLine());
				}

				else if(sequenceNumber>1)
				{
					while (sequenceNumber>=1)
					{
						if (sequenceNumber == 1)
						{
							list.Add(sequenceNumber);
						}

						else if (sequenceNumber % 2 == 0)
						{
							list.Add(sequenceNumber);
							sequenceNumber = sequenceNumber / 2;

						}
						else if (sequenceNumber % 2 != 0)
						{
							list.Add(sequenceNumber);
							sequenceNumber = sequenceNumber * 3 + 1;

						}
					}

					list.ForEach(Console.WriteLine);
					foreach (int i in list)
					{
						Console.Write(i + " ");

					}
				}//else

				sequenceNumber = Convert.ToInt32(Console.ReadLine());


			}    //while--sequence number

			

		}//main



	}
}

推荐答案

这是你的家庭作业......你应该自己解决它。但是,没有什么能阻止您进行Google搜索: c#Collat​​z猜想 [ ^ ]有许多你可以学习的解决方案...喜欢:



* collat​​z猜想 [ ^ ]

* GitHub - genewitch / collat​​z:collat​​z猜想简单的C#代码 [ ^ ]

* GitHub - steelethis / Collat​​z_Conjecture:在C#中实现Collat​​z猜想。 [ ^ ]

* Collat​​z猜想 - dcode.fr [ ^ ]

* 项目Euler 14:C#中最长的Collat​​z序列| MathBlog [ ^ ]

*以及更多...
This is your homework assignment... You should solve it on your own. However, there is nothing stopping you from doing a Google Search: c# Collatz conjecture[^] which has a number of solutions that you can study... Like:

* The collatz conjecture[^]
* GitHub - genewitch/collatz: collatz conjecture simple C# code[^]
* GitHub - steelethis/Collatz_Conjecture: Implementation of the Collatz Conjecture in C#.[^]
* Collatz Conjecture - dcode.fr[^]
* Project Euler 14: Longest Collatz Sequence in C# | MathBlog[^]
* and more...


您的代码过度设计,在If Then Else结构中,您到达Else部分,因为条件不满足,您不要不需要检查非条件。

Your code is over engineered, in a "If Then Else" structure, you reach the Else part because the condition is not met, you don't need to check the not condition.
if (sequenceNumber == 1)
{
}
else if(sequenceNumber>1)
{
}



可以简化为


can be simplified to

// Here, you know thar (sequenceNumber >= 1) as per previous line
if (sequenceNumber == 1)
{
	// this part is executed because (sequenceNumber == 1)
}
else if(sequenceNumber>1)
{
	// Here, you know that (sequenceNumber >= 1) and (sequenceNumber != 1)
	// which combine to (sequenceNumber > 1)
}



使用调试器查看代码在执行时的作用。





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

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

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



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

在Visual中调试C#代码工作室 - YouTube [ ^ ]

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

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


Use the debugger to see what you code does as it execute.


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[^]
Debugging C# Code in Visual Studio - 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.


这篇关于使用C#的Collat​​z序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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