找到GCD的问题 [英] Problem finding the GCD

查看:107
本文介绍了找到GCD的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Console.WriteLine("This is a program that calculates the gcd of some typed number");
            int StartingNum = Convert.ToInt16(Console.ReadLine());
            int NumForCalculate = StartingNum;
            string gcd = ("");
            int gathering = 1;
            operation:
            int x = 2;
            operation2:
            if ((NumForCalculate / x) == :see below:)
            {
                if (gcd == "")
                    gcd = ("" + x);
                else
                    gcd = (gcd + "," + x);
                NumForCalculate = (NumForCalculate / x);
                gathering = gathering * x;
                if (NumForCalculate <= 1)
                    goto end;
                else
                    goto operation;
            }
            else
            {
                x++;
                goto operation2;
            }
            end:
            Console.WriteLine("gcd of integers {0} is {1}, and gathering this set of numbers is {2}", StartingNum, gcd, gathering);
            Console.ReadKey();





如果我将起始小块设为20



if((NumForCalculate / x)== 1)



它将是这样的:整数20的gcd是11,并收集这个集合数字是11



if((NumForCalculate / x)== 0)



它将是像这样:整数20的gcd是21,并且收集这组数字是21



if i set starting nub as 20

if ((NumForCalculate / x) == 1)

it will be like this: gcd of integers 20 is 11, and gathering this set of numbers is 11

if ((NumForCalculate / x) == 0)

it will be like this: gcd of integers 20 is 21, and gathering this set of numbers is 21

推荐答案

找到GCD的问题通常是通过使用模块化的,ia来完成的。

The problem of finding the GCD is usually done by using modular, i.a.
if (20 Mod 2 = 0) then 



或C#


or C#

if (20%2==0)



这可以让你检查是否他们可以通过一个没有任何指挥官的号码来设计。我在这里实现了你想要的东西:

乐趣连续分数 [ ^ ]



至于GoTo的声明,应该删除它们,因为代码完全不可兑换。我通常会将这种代码视为Spaghetti代码,因为你无法找到任何东西的开头或结尾。他们肯定会让你感到沮丧,甚至更糟糕的是,这个人会接管你的代码。


This allows you to check if they could be devisible by a number without any remander. I have implemented sort of the thing you are looking for here:
Fun with continued fractions[^]

As for the GoTo statments, they should be removed becouse the make the code completely unreadeble. I usually reffer to this kind of code as Spaghetti-code, as you cant find the start or end of anything. And they are sure to frustrate you or even worse, the person that would take over your code.


这篇关于找到GCD的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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