2010-12-23 93 views
4

,數組中有1到9的數字,其中一個數字被重複(重複的數字也在1和9之間)如何找到沒有重複的數字使用循環,並且數組只能從頂部到底部橫向移動一次。找到數組中重複的數字

這不是功課,這是問在採訪中

+2

這是功課嗎? – 2010-12-23 09:10:44

+1

如果你不能使用循環,你如何遍歷數組?你是否應該爲每個元素複製/粘貼相同的操作?這只是愚蠢的(聽起來像是一項不好的家庭作業)。 – 2010-12-23 09:11:47

+1

你告訴不要使用循環,並且還要遍歷數組! – codaddict 2010-12-23 09:11:57

回答

8

最短的答案必須基於弗拉基米爾的答案。沒有for循環,但它也不能擴展到可變大小的數組。它是:

int repeated_number = array[9]+array[8]+array[7]+array[6]+array[5] 
        +array[4]+array[3]+array[2]+array[1]+array[0]-45; 

甜而簡單,回答了這個問題。我認爲,問題是所有回答這個問題的人都習慣於編寫能夠處理可變情況的合理代碼,但這是一個簡單的簡單問題,所以應該有一個簡單的答案。

7
int sum = 0; 
for (int i = 9; i >= 0; --i) 
    sum+=array[i]; 
int repeatedNumber = sum - 45; // 45 = 1+...+9 

該解決方案確實使用循環,但你的條件是有爭議的 - 遍歷數組意味着循環使用

3

使10數組布爾變量。每個布爾代表它是否已經發生其各自的數量。如果你在寫真的時候發現了bool,那麼這個數字會重複。

0

我給出的解決方案來自CommanderZ,但唯一的區別是我很無聊,我不想去學習,所以我做了代碼。

int FindDuplicate(int index) 
{ 
    static bool initialized = false; 
    static bool Existance[10]; 
    if(!initialized) 
    { 
     for (int i = 0 ; i < 10 ; i++) 
      Existance[i] = false; 
     initialized = true; 
    } 

    if(index == -1) 
     return 0; 

    if(Array[index] == true) 
     return index; 
    else 
     Array[index] = true; 

    return FindDuplicate(index - 1); //edit-fix 

} 

另見這個線程可能會有點類似:Given an array with multiple repeated entries, fnd one repeated entry O(N) time and constant space

而這一個了:Array Homework Question

0

我很無聊了。下面是使用遞歸和Vladimirs減招的解決方案:

int find_dupe(int* array, int len) 
{ 
    static int sum = 0; 
    sum += array[0]; 

    if(len == 1) 
     return sum - 45; 

    return find_dupe(array+1, len-1); 
} 

int main() 
{ 
    int array[10] = { 9, 2, 5, 8, 6, 7, 1, 3, 4, 2 }; 

    printf("dupe: %i\n", find_dupe(array, 10)); 

    return 0; 
} 
0

嘗試使用布爾數組指示值是否存在。使用true來表示號碼存在。

int main(void) 
{ 
    const unsigned int array_values[10] = {1, 2, 3, 3, 4, 5, 6, 7, 8, 9}; 
    bool    is_resident[10] = {false}; 
    bool    duplicate = false; 
    unsigned int  index = 0; 
    duplicate = duplicate || is_resident[array[index]]; 
    is_resident[array[index++]] = true; 
    duplicate = duplicate || is_resident[array[index]]; 
    is_resident[array[index++]] = true; 
    duplicate = duplicate || is_resident[array[index]]; 
    is_resident[array[index++]] = true; 
    duplicate = duplicate || is_resident[array[index]]; 
    is_resident[array[index++]] = true; 
    duplicate = duplicate || is_resident[array[index]]; 
    is_resident[array[index++]] = true; 
    duplicate = duplicate || is_resident[array[index]]; 
    is_resident[array[index++]] = true; 
    duplicate = duplicate || is_resident[array[index]]; 
    is_resident[array[index++]] = true; 
    duplicate = duplicate || is_resident[array[index]]; 
    is_resident[array[index++]] = true; 
    duplicate = duplicate || is_resident[array[index]]; 
    is_resident[array[index++]] = true; 
    duplicate = duplicate || is_resident[array[index]]; 
    is_resident[array[index++]] = true; 
    std::cout << "Duplicate found: " << (char *)(duplicate ? "true" : "false") << std::endl; 
    return 0; 
} 

上面的代碼沒有經過測試和專門針對這個問題。這個代碼在生活中沒有其他目的,沒有被廣義化。

注意:在這個例子中沒有使用循環或傷害。
「總是有第三種選擇。」 - Thomas Matthews