总理的因素在C# [英] Prime Factors In C#
本文介绍了总理的因素在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要创建C#2005,它计算给定输入的质因数的程序。我想使用基本的,最简单的事情,没有必要为它也不阵列等事情只是简单的模创建一个方法。没有任何代码,满足了我的愿望?
这里是寻找简单要素的代码,我需要这个代码进行修改,以计算质因子
类节目
{
静态无效的主要(字串[] args)
{
int类型的,b;
Console.WriteLine(请输入您的整数:);
A = int.Parse(到Console.ReadLine());
为(B = 1; B< = A; B ++)
{
如果(A%B == 0)
{
Console.WriteLine(二+是一个因素+α);
}
}
到Console.ReadLine();
}
}
解决方案
INT A,b;
Console.WriteLine(请输入您的整数:);
A = int.Parse(到Console.ReadLine());
表示(B = 2;一个大于1; b ++)
如果(一个%B == 0)
{
INT X = 0;
,而(A%B == 0)
{
A / = B;
X ++;
}
Console.WriteLine({0}是一个主要因素{1}次!,B,X);
}
Console.WriteLine(钍-TH-钍-TH-钍-...这一切,乡亲们!);
I want to create a program in C# 2005 which calculates prime factors of a given input. i want to use the basic and simplest things, no need to create a method for it nor array things etc. just simple modulus. is there any code which fulfills what i desire?
here is the code for finding simple factors, i need this code to be modified to calculate prime factors
class Program
{
static void Main(string[] args)
{
int a, b;
Console.WriteLine("Please enter your integer: ");
a = int.Parse(Console.ReadLine());
for (b = 1; b <= a; b++)
{
if (a % b == 0)
{
Console.WriteLine(b + " is a factor of " + a);
}
}
Console.ReadLine();
}
}
解决方案
int a, b;
Console.WriteLine("Please enter your integer: ");
a = int.Parse(Console.ReadLine());
for (b = 2; a > 1; b++)
if (a % b == 0)
{
int x = 0;
while (a % b == 0)
{
a /= b;
x++;
}
Console.WriteLine("{0} is a prime factor {1} times!", b, x);
}
Console.WriteLine("Th-Th-Th-Th-Th-... That's all, folks!");
这篇关于总理的因素在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文