4
A
回答
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
相關問題
- 1. 在數組中找到重複數組
- 2. 在數組中找到重複的數字
- 3. 在2x2數組中找到重複的數字。 (Java)
- 4. 找到重複的兩個數組
- 5. 找到一個數字中的特定重複數字
- 6. 在數組vba中查找重複的數字
- 7. 如何從命令行中找到數組中最頻繁重複的數字?
- 8. 查找重複數組
- 9. 查找數組中的重複元素
- 10. 查找數組中的重複條目
- 11. 查找數組中的重複元素?
- 12. 在帶有邊界的整數數組中找到重複項
- 13. 在數組中尋找重複數組(多維數組)
- 14. 找到重複的數據庫的最大數字位數
- 15. 在數組中查找重複項
- 16. 找到數組中最重的數字C++
- 17. 轉換重複記錄到重複的字符串數組
- 18. 如何在MongoDB中的子文檔數組中找到重複的字段
- 19. 在數組數組中顯示重複的數字
- 20. Java - 沒有重複的數字數組
- 21. 找到長度爲n的數組中最重複的元素
- 22. 從重複數字的數組中刪除數字
- 23. 返回n個數字的數組重複到另一個數組中
- 24. 需要在java數組中找到重複的整數和它們的數量
- 25. 數組中的Ng重複數組
- 26. 如何找到並控制數組中的日誌重複值?
- 27. 如何找到數組中最重複的值?
- 28. 在兩個數組中找到重複的值
- 29. 如何找到數組中最重複的部分?
- 30. 在數組中找到非重複的項目
這是功課嗎? – 2010-12-23 09:10:44
如果你不能使用循環,你如何遍歷數組?你是否應該爲每個元素複製/粘貼相同的操作?這只是愚蠢的(聽起來像是一項不好的家庭作業)。 – 2010-12-23 09:11:47
你告訴不要使用循環,並且還要遍歷數組! – codaddict 2010-12-23 09:11:57