33,008
社区成员
发帖
与我相关
我的任务
分享
static int[] algorithm() {
int result[] = new int[5];
int[] target = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20 };// , 21 };
int metic = target[target.length - 1] / 5 + 1;// 等差的数值
for (int j = 0; j < target.length; j++) {// 遍历数组 从第一个开始元素累加最大数值开始 是否满足要求
for (int i = metic; i > 0; i--) {// 最大的等差数值依次递减
int s = target[j];
for (int k = 0; k < 5; k++) {
if (s > target[target.length - 1]) {
break;
} else {
result[k] = s;
s += i;
if (k == 4) {// 满足5个数据时
return result;
}
}
}
}
}
return null;
}
static void getArr() {
int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 };
int FIND_COUNT = 5; // 目标数量
int lCount = 0; // 数组长度
int lStep = 0;
int i = 0;
int j = 0;
lCount = a.length;
if (lCount < FIND_COUNT) {
System.out.println("数量不足");
return;
}
lStep = (lCount - 1) / (FIND_COUNT - 1);
System.out.print("lStep:" + lStep + "\n");
j = lCount - 1;
for (int ik = lCount - 1; ik > -1; ik--) {
System.out.print(a[j] + ",");
j -= lStep;
if (j < 0)
break;
}
}
之后再修改先下,看看能不能实现任意数组取任意长度。
还请继续指点!
static void getArr(){
int FIND_COUNT = 5;
int[] a = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int lCount = 0;
int lStep = 0;
int i = 0;
int j = 0;
lCount = a.length + 1;
if( lCount < FIND_COUNT){
System.out.println("数量不足");
return;
}
lStep = (lCount - 1) / (FIND_COUNT - 1);
j = a[0];
for( int ik = 1; ik < FIND_COUNT; ik++){
System.out.print(a[j]);
if(ik < FIND_COUNT)
System.out.print(", ");
j = j + lStep;
}
}
输出 2, 5, 8, 11,
请查看下哪个地方不对[/quote]
for( int ik = 1; ik < FIND_COUNT; ik++)
=>
for( int ik = 1; ik <= FIND_COUNT; ik++)
static void getArr(){
int FIND_COUNT = 5;
int[] a = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int lCount = 0;
int lStep = 0;
int i = 0;
int j = 0;
lCount = a.length + 1;
if( lCount < FIND_COUNT){
System.out.println("数量不足");
return;
}
lStep = (lCount - 1) / (FIND_COUNT - 1);
j = a[0];
for( int ik = 1; ik < FIND_COUNT; ik++){
System.out.print(a[j]);
if(ik < FIND_COUNT)
System.out.print(", ");
j = j + lStep;
}
}
输出 2, 5, 8, 11,
请查看下哪个地方不对Option Explicit
Sub Main()
Const FIND_COUNT As Long = 5
Dim a() As Variant
Dim lCount As Long
Dim lStep As Long
Dim i As Long
Dim j As Long
a = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
lCount = UBound(a) - LBound(a) + 1
If lCount < FIND_COUNT Then
Debug.Print "数量不足"
Exit Sub
End If
lStep = Fix((lCount - 1) / (FIND_COUNT - 1))
j = LBound(a)
For i = 1 To FIND_COUNT
Debug.Print CStr(a(j));
If i < FIND_COUNT Then Debug.Print ", ";
j = j + lStep
Next
Debug.Print
End Sub
1, 4, 7, 10, 13