collatz

    2熱度

    2回答

    static void collatz(int i) { int x=0,a=0,res=0,count=0; int array[50]; array[0]=i; while(array[count]!=0) { if(array[count]%2==0) { count++; array[c

    4熱度

    2回答

    我想記住一個遞歸的collat​​z序列函數生鏽,但是我需要memoized值的hashmap來保持其內容跨單獨的函數調用。有沒有一種優雅的方式來防止這種情況發生,或者我必須在main中聲明hashmap並每次將它傳遞給函數?我相信每次我調用函數時,hashmap都會被重新聲明爲空映射。這裏是我的代碼: fn collatz(n: int) -> int { let mut map =

    -2熱度

    1回答

    我試着解決3n + 1問題(UVA 100),這裏是我的代碼,但根據UVA在線評判我的程序給出了錯誤的答案,我的代碼通過了我能想到的所有測試案例,但無法檢測到什麼是錯誤的,請幫我找到錯誤。 #include <iostream> #include <algorithm> #include <vector> using namespace std; vector<int> Num;//

    -1熱度

    2回答

    對弗吉尼亞的3N + 1個問題的鏈接是: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=36 我的代碼是: #include<iostream> using namespace std; long long l(long long n) {

    -2熱度

    2回答

    這是我解決N + 1個問題即給予錯誤的答案。自從過去5天以來,我一直在這個安靜的地方掙扎很多次。請幫我解決我的解決方案中的問題。我使用了尾遞歸,並且還存儲了一張地圖來追蹤2的權力以更快地達到答案。 這個問題的鏈接是Programming Challenges - The 3n + 1 problem #include <stdio.h> #include <map> using namespa

    0熱度

    4回答

    我怎樣才能得到一個整數作爲輸入,其輸出將是該號碼後面的Collatz sequence。該序列由以下規則來計算: 如果n是偶數,則下一個數字是n/2 如果n是奇數,則下一個數字是3n + 1。 例如11 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 開始時,這是我現在的代碼: n = int(raw_input('insert a random number'

    0熱度

    1回答

    我需要使用遞歸爲collat​​z猜想編寫python代碼,其中提示用戶輸入正整數,如果偶數乘以3,則該數字除以2並且如果奇數則加1,並且該序列繼續,直到該值等於1。我還必須提示用戶選擇如何顯示序列,無論是計算,顛倒或作爲迴文的標準方式(向前和向後,即86324895159842368)。下面是我現在所擁有的。我沒有計算序列本身的問題,但我不知道如何實現第二個參數。任何時候我試圖將方向定義爲F,B

    1熱度

    2回答

    for n in range(1,1000000): print(n) result = [] for x in range(1,3000001): if n%2==0: x=n/2 else: x=3*n+ 1 n=x result.append(n) if n==1:

    0熱度

    3回答

    ,所以我試圖迭代求解的在Collat​​z功能的計劃,但我的測試用例一直顯示爲 (define (collatz n) (define (collatz-iter n counter) (if (<= n 1) 1 (if (even? n) (collatz-iter (/ n 2) (+ counter 1)) (co

    2熱度

    1回答

    我的目標是編寫一個函數來計算低於特定數字'n'的最大Collat​​z數。 (對於那些熟悉的人來說,這是個項目歐拉問題。) 某些上下文:給定整數的Collat​​z數等於該整數的Collat​​z序列的長度。一個整數的Collat​​z序列計算如下:序列中的第一個數字(「n0」)是該整數本身;如果n0是偶數,序列中的下一個數字(「n1」)等於n/2;如果n0是奇數,那麼n1等於3 * n0 + 1