获取连续因子,c#
我需要解决这个问题,但我一直在获取因子,但我需要做的是...
正数 n 是连续因子的当且仅当它有因子 i 和 j ,其中 i > 。 1、j> 1 且 j = i + 1。编写一个名为 isConsecutiveFactored 的函数,如果其参数是连续因子分解的,则返回 1,否则返回 0。 函数签名是 int isConsectiveFactored(int n)
函数签名是 int isConceptiveFactored(int n) 示例
如果 n 为 24,则返回 1,因为 24 = 2*3*4 且 3 = 2 + 1
如果 n 为 105,则返回 0,因为 105 = 3*5 *7 和 5 != 3+1 和 7 != 5+1
如果 n 是 90,则返回 1,因为 90 的因数包括 2 和 3,并且 3 = 2 + 1
到目前为止能够得到因子,即如果数字是 24 那么我能够得到 2 和 12,但我卡在那里,一片空白......
using System;
using System.Collections.Generic;
using System.Text;
namespace isConsecutiveFactored
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(isConsecutiveFactored(24));
}
private static int isConsecutiveFactored(int p)
{
foreach (int a1 in getFactor(24))
{
Console.WriteLine(a1);
}
return 0;
}
private static List<int> getFactor(int p)
{
List<int> factor = new List<int>();
int max = (int)Math.Sqrt(p);
for (int i = 1; i <= max; i++)
{
if (i != 0)
{
if ((p % i) == 0)
{
if (i != max)
{
if ((p / i) != 1 && (p / i) != p)
{
factor.Add(i);
factor.Add(p / i);
//Console.WriteLine((p / i) + " " + "this is the factor");
}
}
}
}
//
}
List<int> fac = factor.GetRange(0, 2);
return fac;
}
}
}
任何人都可以帮我解决这个问题......
i need to solve this question but im stuck at getting the factors , but what i need to do is...
A positive number n is consecutive-factored if and only if it has factors, i and j where i > 1, j > 1 and j = i + 1. Write a function named isConsecutiveFactored that returns 1 if its argument is consecutive-factored, otherwise it returns 0.
the function signature is
int isConsectiveFactored(int n)
the function signature is
int isConsectiveFactored(int n)
Example
If n is 24 return 1 because 24 = 2*3*4 and 3 = 2 + 1
If n is 105 return 0 because 105 = 3*5*7 and 5 != 3+1 and 7 != 5+1
If n is 90 return 1 because factors of 90 include 2 and 3 and 3 = 2 + 1
so far ive been able to get the factor i.e if the number is 24 then ive been able to get
2 and 12 , but im stuck there and blanked....
using System;
using System.Collections.Generic;
using System.Text;
namespace isConsecutiveFactored
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(isConsecutiveFactored(24));
}
private static int isConsecutiveFactored(int p)
{
foreach (int a1 in getFactor(24))
{
Console.WriteLine(a1);
}
return 0;
}
private static List<int> getFactor(int p)
{
List<int> factor = new List<int>();
int max = (int)Math.Sqrt(p);
for (int i = 1; i <= max; i++)
{
if (i != 0)
{
if ((p % i) == 0)
{
if (i != max)
{
if ((p / i) != 1 && (p / i) != p)
{
factor.Add(i);
factor.Add(p / i);
//Console.WriteLine((p / i) + " " + "this is the factor");
}
}
}
}
//
}
List<int> fac = factor.GetRange(0, 2);
return fac;
}
}
}
can anybody help me with this .....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试以下操作
Try the following
但你真的应该自己做作业,而我无法让自己返回一个 int。
But you should really do your homework yourself, and I couldn't bring myself to return an int.
尝试这个
Try this