免責宣告這不是我的代碼,此代碼來自Remove Duplicate Elements from an Array in C - Javatpoint
我想知道的是示例 2 編碼部分。(我為我編輯了一些代碼,或者您可以清楚地看到代碼。)
/* program to delete the duplicate elements from sorted array in C. */
#include <stdio.h>
int duplicate_element ( int arr[], int num)
{
// check num is equal to 0 and num == 1
if (num == 0 || num == 1)
{
return num;
}
// create temp array to store same number
int temp [num];
// declare variable
int i, j = 0;
// use for loop to check duplicate element
for (i = 0; i < num - 1; i )
{
// check the element of i is not equal to (i 1) next element
if (arr [i] != arr[i 1])
{
temp[j ] = arr[i];
}
}
temp[j ] = arr[ num - 1];
// check the original array's elements with temporary array's elements
for (i = 0; i < j; i )
{
arr[i] = temp[i];
}
return j;
}
int main ()
{
int num;
printf (" Define the no. of elements of the array: ");
scanf (" %d", &num);
int arr[num], i;
printf (" Enter the elements: ");
// use loop to read elements one by one
for ( i = 0; i < num; i )
{
scanf (" %d", &arr[i]);
}
printf (" \n Elements before removing duplicates: ");
for ( i = 0; i < num; i )
{
printf (" %d", arr[i]);
}
num = duplicate_element (arr, num);
// print array after removing duplicates elements
printf (" \n Display array's elements after removing duplicates: ");
for ( i = 0; i < num; i )
{
printf (" %d", arr[i]);
}
return 0;
}
問題來了,函式 duplicate_element 中的所有 j 是做什么的?(如果可能的話,我想知道代碼在做什么,因為 // 使用 for 回圈檢查重復元素,直到回傳之前。這部分我只是好奇我是否知道正確。)
這是我的理解(j 是 arr[] 的最終大小)。在第一個問題中,執行時
j 現在是 0
溫度[j ]
是先將 j 的值加 1,然后將值 arr[i] 賦給 temp[1]。(這對嗎?)
第二個問題,在第一個 for 回圈中檢查 arr[i] 中的值何時不等于 arr[i 1] 中的值,然后將 temp[j ] 中的值與 arr[i] 中的值分配,直到 for 回圈然后用 arr[num - 1] 分配 temp[j ]
(j 現在取決于 if 條件,例如,當所有值不等于 j 的值 == num - 1 的值且 num - 1 等于 arr 的最后一個值時)
在最后一個 for 回圈中,它將陣列 arr 中的每個值分配給陣列 temp。(這對嗎?)
uj5u.com熱心網友回復:
簡而言之,宣告
temp[j ] = arr[i];
相當于
int old_value_of_j = j;
j = j 1;
temp[old_value_of_j] = arr[i];
uj5u.com熱心網友回復:
對于初學者來說,代碼非常糟糕。
首先,函式應該宣告為
size_t duplicate_element ( int arr[], size_t num );
也就是說,傳遞陣列的大小應該使用無符號整數型別size_t而不是有符號int型別 int 來指定。否則這個變長陣列的宣告
// create temp array to store same number
int temp [num];
連同這份宣告
temp[j ] = arr[ num - 1];
如果用戶將作為第二個引數傳遞一個負數并且根據函式規范它允許傳遞一個負數,則將呼叫未定義的行為。
其次,使用變長陣列temp
// create temp array to store same number
int temp [num];
使函式不安全。可能會出現程式無法定義此可變長度陣列的情況。
該方法過于復雜、混亂且效率低下。
至于你關于后綴運算子的問題, 那么根據 C 標準(6.5.2.4 后綴遞增和遞減運算子)
2 后綴 運算子的結果是運算元的值。作為副作用,運算元物件的值會遞增(即,將相應型別的值 1 添加到其中)。
所以其實這個說法
temp[j ] = arr[i];
可以等效地重寫為
temp[j] = arr[i];
j = 1;
隨著函式將陣列temp中一系列重復元素中的最后一個元素添加到陣列中,arr然后在此主回圈之后
// use for loop to check duplicate element
for (i = 0; i < num - 1; i )
{
// check the element of i is not equal to (i 1) next element
if (arr [i] != arr[i 1])
{
temp[j ] = arr[i];
}
}
您需要將陣列的最后一個元素添加arr到陣列中temp
temp[j ] = arr[ num - 1];
這是一個演示程式,展示了如何重寫該函式并使其看起來更簡單。
#include <stdio.h>
size_t duplicate_element( int a[], size_t n )
{
size_t m = 0;
for (size_t i = 0; i < n; i )
{
if (i == 0 || a[i] != a[m-1])
{
if (i != m) a[m] = a[i];
m;
}
}
return m;
}
int main( void )
{
int a[] = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5 };
const size_t N = sizeof( a ) / sizeof( *a );
for (size_t i = 0; i < N; i )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
size_t m = duplicate_element( a, N );
for (size_t i = 0; i < m; i )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
}
程式輸出為
1 2 2 3 3 3 4 4 4 4 5 5 5 5
1 2 3 4 5
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/523813.html
