明天参加考试,其中一个练习题是询问这个算法在伪代码中写的是什么。有人可以帮忙吗?
Algorithm ???
Input A: Array of Integers; n: Integer;
Variables i, c: Integers;
Begin
for i:=0 to n-1 do
c:=1;
while ((i+c)<n) and (A[i]<A[i+c]) do
c:=c+1;
od
output(i,A[i],c-1);
od
End
答案 0 :(得分:2)
算法采用整数数组(已排序或未排序)并输出同一数组中的项目数,其索引高于当前位置和,其大于当前索引位置值
例如
手动排序升序整数数组:
public static void main(String[] args){
// stores an array of integers
int [] myArray = {0,1,2,3};
// assuming the length of array is n
int n = myArray.length;
// counter variables
int i,c;
// starting from array index 0 to the length of the array
for(i=0;i<(n);i++){
c = 1;
while(((i+c)<n) && (myArray[i]<myArray[i+c])){
c++;
}
System.out.println("index value..."+i+", myArray value..."+myArray[i]+", number of items in array with index greater than current with values greater than current..."+(c-1));
}
}
会给出输出
index value...0, myArray value...0, number of items in array with index greater than current with values greater than current...3 index value...1, myArray value...1, number of items in array with index greater than current with values greater than current...2 index value...2, myArray value...2, number of items in array with index greater than current with values greater than current...1 index value...3, myArray value...3, number of items in array with index greater than current with values greater than current...0
用于手动排序的降序整数数组:
int [] myArray = {10,9,8};
输出是:
index value...0, myArray value...10, number of items in array with index greater than current with values greater than current...0 index value...1, myArray value...9, number of items in array with index greater than current with values greater than current...0 index value...2, myArray value...8, number of items in array with index greater than current with values greater than current...0
对于整数数组都是一样的:
int [] myArray = {1,1,1};
输出将是
index value...0, myArray value...1, number of items in array with index greater than current with values greater than current...0 index value...1, myArray value...1, number of items in array with index greater than current with values greater than current...0 index value...2, myArray value...1, number of items in array with index greater than current with values greater than current...0
答案 1 :(得分:1)
对于数组中的每个数字,此算法查找右侧的数字计数,形成一个大于或等于此数字的连续数字序列。
答案 2 :(得分:1)
这有助于您自助并通过考试: 你觉得它做什么? 如果你传递A = [1,2,3]和n = 3,会发生什么? 如果你传递A = [3,2,1,0]和n = 3,会发生什么? 你能用Java / JavaScript / C#/ Python / Erlang编写代码,看看自己会发生什么?