2013-02-23 89 views
-1

所以即時通訊工具與遞歸函數工作,我的問題是,我有一個數組代表金幣這是一個[]這個數組有硬幣,需要與兩個用戶共享一種方式從長遠來看,每個用戶都會獲得金相同量或最佳的解決方案......這樣C蠻力遞歸函數

Gold 
10 6 5 2 
User A : gets 10 2 
User B : gets 6 5 

用戶A和用戶B之間的絕對值給我性差異。 在這種情況下,將1

爲了解決這個問題,我需要通過所有可能的組合,爲什麼我使用暴力破解和遞歸函數...這就是繳費要做到這一點讓我不得不運行的每一個組合,看看兩者之間的絕對差異,如果它比以前的更好,我把它保存在全球變量最好...

問題是功能不是很好的工作..如果你幫我我將不勝感激...

代碼如下:

#include <stdlib.h> 
#include <stdio.h> 
#include <math.h> 

int best = 0; // keeps the best option 



int share_friends_recursive(int nelems,int a[],int friend_a,int friend_b,int i){ 
    int sub = 0; 
    friend_a += a[i]; 
    friend_b += a[i+1]; 
    if(i+1 == nelems){ 

     return 0; 
    }else{ 
     sub = abs(friend_a - friend_b); 
     if(sub < best){ 
      best = sub; 
     } 
     i++; 
     share_friends_recursive(nelems,a,friend_a,friend_b,i); 
    } 

} 

int main(int argc, char *argv[]) { 
    // 
    int nelems = 4; 
    int a[] = {10,6,5,2}; 



    //friend A can get the first value 
    // friend B gets the second one ... 
    share_friends_recursive(nelems,a,0,1,0); 
    printf("%d \n",best); 

    return 0; 
} 
+3

請舉一個例子或descri 「什麼都行不通」的意思。 – 2013-02-23 00:33:31

+0

'int a [] = {'10','6','5','2'};'您是否指定了ASCII值? – 2013-02-23 00:34:40

+0

我整數...不ascii值。當我發現它沒有工作,它沒有得到正確的解決方案,它應該是1不是0 ...也有一些問題,我似乎不明白在遞歸的方式。在這個問題,即時通訊有點阻止... – DestPT 2013-02-23 00:36:26

回答

2

你需要考慮你在做什麼。例如:

int share_friends_recursive(int nelems,int a[],int friend_a,int friend_b,int i){ 
    int sub = 0; 
    friend_a += a[i]; 
    friend_b += a[i+1]; 

所以你給了第一根樁到的友情A和第二個朋友B.但你以後要與i加1遞歸,所以你去給第二堆到A,即使你已經把它交給B.這沒有任何意義。

你想要做的是嘗試給A的第一個樁,然後遞歸給出其餘的樁,然後重新開始,嘗試給B的第一個樁,並遞歸給出其餘的樁,並比較這兩個案例看哪個最好。

編輯

好吧,讓我們去通過這個一步一步來。你有你的遞歸函數:

int share_friends_recursive(int nelems,int a[],int friend_a,int friend_b,int i){ 

這裏nelems是成堆的總數,a是每根樁的尺寸,i是多少樁已被分配,friend_afriend_b是多少已經給出了給每個朋友。首先,如果我們已經分發了所有的樁子呢?這個解決方案有多好?來電者需要知道,所以我們會將其退回。

if (i == nelems) { 
     return abs(friend_a - friend_b); 

告訴我們在完成任務後我們做了多好(或多麼糟糕)。如果我們沒有完成呢?那麼,那麼我們需要嘗試給a和b兩個堆i,看看哪個更好。

} else { 
     int toA = share_friends_recursive(nelems, a, friend_a + a[i], friend_b, i+1); 
     int toB = share_friends_recursive(nelems, a, friend_a, friend_b + a[i], i+1); 

那麼哪個更好?好吧,我們只是比較,並返回較低的一個:

 if (toA <= toB) 
      return toA; 
     else 
      return toB; 
    } 
} 

..並告訴我們最好的分裂是多麼好。

+0

謝謝你的答案,它可以幫助我一點點..仍然現在我無法找到如何做到這一點,因爲如果我以遞歸方式爲每個朋友撥打兩次電話,我將如何跟蹤已經分配的元素......如果你能幫助我,我真的很沮喪.. – DestPT 2013-02-23 01:05:49

+0

can任何人都可以幫助我,我真的不能找到解決方案...謝謝 – DestPT 2013-02-23 01:34:13

+0

感謝您的時間...即時支持它現在感謝您...我會嘗試做更多的問題,這樣我就可以更好地理解它... – DestPT 2013-02-23 16:27:35

0

新的代碼如下:

#include <stdlib.h> 
#include <stdio.h> 
#include <math.h> 

int best = 10; // keeps the best option 



int share_friends_recursive(int nelems,int a[],int friend_a,int friend_b,int i){ 
    if(i == nelems){ 

     return abs(friend_a - friend_b); 
    }else{ 
    int n = abs(friend_a - friend_b); 
     int toA = share_friends_recursive(nelems, a, friend_a + a[i],friend_b,i+1); 
     int toB = share_friends_recursive(nelems, a, friend_a, friend_b + a[i],i+1); 
     printf("[%d]: value A: %d, Value B: %d \n",i,toA,toB); 
     if (toA <= toB) 
      return toA; 
     else 
      return toB; 
    } 

} 

int main(int argc, char *argv[]) { 
    // 
    int nelems = 4; 
    int a[] = {10,6,5,2}; 



    //friend A can get the first value 
    // friend B gets the second one ... 
    best = share_friends_recursive(nelems,a,0,1,0); 
    printf("%d \n",best); 

    return 0; 
} 

在這種情況下,而不是給1它給0這是不對的像我在上面的例子已經證明......我似乎無法找到問題本情況......如果有人能幫助我,我將apreciated ...

是給這個項目目前的錯誤輸出是:

[3]: value A: 22, Value B: 18 
[3]: value A: 12, Value B: 8 
[2]: value A: 18, Value B: 8 
[3]: value A: 10, Value B: 6 
[3]: value A: 0, Value B: 4 
[2]: value A: 6, Value B: 0 
[1]: value A: 8, Value B: 0 
[3]: value A: 2, Value B: 2 
[3]: value A: 8, Value B: 12 
[2]: value A: 2, Value B: 8 
[3]: value A: 10, Value B: 14 
[3]: value A: 20, Value B: 24 
[2]: value A: 10, Value B: 20 
[1]: value A: 2, Value B: 10 
[0]: value A: 0, Value B: 2 
0 
+0

你開始給B的初始餘額爲1(你的調用'best = share_friends_recursive(nelems,a,0,1,0);',它通過給出10和2到A和6和5到B,這兩個總數都是12,所以答案是0(等於總數)。這是怎麼回事? – 2013-02-23 22:10:44

+0

感謝您的回答,但5 + 6使11這就是爲什麼它應該是1,而不是0 ... – DestPT 2013-02-24 02:28:58

+0

K我已修復它...感謝您的時間 – DestPT 2013-02-24 16:07:39