-3
給定N個整數,編寫一個算法(以僞代碼形式)來計算具有 差異的整數對數K. 輸入 整數N,差K和列表元素。 輸出 一個整數告訴這將做的工作有K.如何搜索此數組
給定N個整數,編寫一個算法(以僞代碼形式)來計算具有 差異的整數對數K. 輸入 整數N,差K和列表元素。 輸出 一個整數告訴這將做的工作有K.如何搜索此數組
的差異對數:
START
# Variables Initialization
init Decision = 'continue';
init N = 0;
init integers_array = array();
# Getting the list of Integers
Do{
prompt "Insert an Integer";
input inserted_int;
integers_array.push(inserted_int);
prompt "Choose: Done or continue?!";
input Decision;
}while not (Decision == 'continue');
# Getting the Searched Difference
prompt "Insert the searched difference:";
input K;
# Preparing the search phase
N = count(integers_array);
init pairs_counter = 0;
# Make a double loop on the same Integers array, and increment the counter when find
pairs, every pair will be counted twice!
foreach (int i=0; i<N; i++){
foreach (int j=0; j<N; j++){
if (absolute(integers_array[i] - integers_array[j]) == K){
pairs_counter++;
}
}
}
# Divide by two to get the real number of Pairs
true_pairs_counter = pairs_counter/2;
output true_pairs_counter;
END