求大神解答,要完整代码!编写一个C#控制台应用程序,要求从键盘输入一个正整数n,然后输出n的所有因子

如题所述

第1个回答  2016-10-18
public int[] GetFactors(int n){
int Count = 0;
int[] temp = new int[n];
for(int i = 1; i < n; i++){
if (n % i == 0) {
temp [Count] = i;
Count++;
}
}
int[] OutFactors = new int[Count];
for (int i = 0; i < OutFactors.Length; i++) {
OutFactors [i] = temp [i];
}
return OutFactors;
//by NWinterSS
}

使用的时候,创建一个int[]然后让它等于这个函数就可以调出了。没有优化,数比较大的话比较耗资源,凑活用吧😊。
求采纳。
P.S.我是平时用U3D做这些所以其它功能还得自己稍微链接一下。追问

我是用Visual studio2010的,然后我是渣渣,复制进去有错误,越改越多错,想哭了!

追答

估计就是调用时有问题吧。
我没用过VS,都是在Mac上做前端所以大概没法继续帮你太多。
试试:

public void GetFactors(int n, out int[] Factors){
Debug.Log ("Start");
int Count = 0;
int[] temp = new int[n];
for(int i = 1; i < n; i++){
if (n % i == 0) {
temp [Count] = i;
Count++;
}
}
int[] OutFactors = new int[Count];
for (int i = 0; i < OutFactors.Length; i++) {
Debug.Log (temp [i]);
OutFactors [i] = temp [i];
}
Factors = OutFactors;
Debug.Log ("Ended, with total of "+Count+" Factors");
}
在某个地方调用这个函数,会在Console里输出所有的数。
然后在后面两个参数分别为n是一个整数,你要找的那个,factors是一个int[]。

本回答被提问者和网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网