满意答案
chunhai8395
推荐于 2017.12.16
采纳率:53% 等级:12
已帮助:38018人
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[]。
00分享举报