2016-12-02 32 views
-1

我把這作爲我的IT作業 「一隻猴子打開N個儲物櫃。然後,他從儲物櫃1開始,並從2入2關閉儲物櫃(例如:1關閉,2是開放的,3是封閉的,4是開放的),然後他再次做同樣的事情,但在3中是3。如果他碰到一個封閉的儲物櫃,他會打開它,他這樣做了N次。問題想知道哪些儲物櫃保持打開狀態。從2中2然後3中計數

有一個N = 10的例子,結果是2,5,10。 我做錯了什麼?

//NOTE : 1-OPEN, 0-Closed 
    int a[10],n,i,j,k=2; 
    cout<<"Cate colivii exista? ";cin>>n; 
    for(i=0; i<n; i++) 
    { 
     a[i]=1; 
    } 
    for(i=0; i<n; i=i+k) 
     { 
      if(a[i]==1) 
       a[i]=0; 
      else if(a[i]==0) 
       a[i]=1; 
      k++; 
     } 
    for(i=0; i<n; i++) 
    { 
     if(a[i]==1) 
      cout<<i<<" "; 
    } 
//K should have been a number but i changed it. 
    return 0; 
} 
+0

您目前的結果是什麼,或者您希望我們構建和運行您的程序以查看此結果?另外,如果你在紙上編寫中間步驟,然後調試你的程序,你應該自己找到問題。 – grek40

+2

你在for循環中增加'k' ... for循環的第一次迭代將有k = 2,下一次迭代將有k = 3等等。你繼續增加你的'k',所以你的'i'會變成'> n'非常快,循環將退出。 – Rorschach

+0

@Rorschach是對的。你在循環內增加你的'k',所以你的2合2,3合3,4合4等將在單次迭代中發生。您應該嘗試在紙上手動查看代碼,以瞭解您做錯了什麼。 – Yankee

回答

0

我重寫它,以改善一些東西放在一起,交換空調風格的C++風格

#include<iostream> 
#include<vector> 
using std::cout; //not using namespace std since that one is too big 
using std::endl; 
using std::vector; 

//declaring but not implementing here, want only to describe what it does here, not how 
//at this place, you might want to write a comment that describes what the function does 
void locker_monkey(const unsigned int amount_lockers); 

//keeping the main function simple 
int main() { 

    //initializing every variable if possible, 
    //also using unsigned int since it should never be negative 
    unsigned int amount_lockers = 0; 
    cout << "Cate colivii exista? "; 
    cin >> amount_lockers; 

    locker_monkey(amount_lockers); 

    return 0; 
} 

enum LockerStatus{ closed, open }; //using enum to make values descriptive 

void locker_monkey(const unsigned int amount_lockers){ 

    //use std::vector instead of array 
    //if created with two arguments, the first is the size of the vector, 
    //the second the default value 
    vector<LockerStatus> lockers_status(amount_lockers, open); 

    //here comes the important part regarding your question, two loops: 
    for(unsigned int k=2; k<amount_lockers; k++){ //outer one increases k 
     for(unsigned int i=0; i<amount_lockers; i+=k){ //inner one increases i 
      if(lockers_status[i] == open){ //more descriptive that a[i] == 1 
       lockers_status[i] = closed; 
      }else{ 
       lockers_status[i] = open; 
      } 
     } 
    } 

    cout << "open lockers: "; 
    for(unsigned int i=0; i<amount_lockers; i++){ 
     if(lockers_status[i]==open) 
     cout<<i<<" "; 
    } 
    cout << endl; //you forgot that part in your code - you want to finish your line 
} 

的重要組成部分,是建立在雙循環,即

for(unsigned int k=2; k<amount_lockers; k++){ 
     for(unsigned int i=0; i<amount_lockers; i+=k){ 

在至少如果我正確理解問題。

但是既然你是一個初學者,我認爲它有點改進你的代碼不會有什麼壞處。

+0

'locker_monkey'絕對不應該是內聯函數(在這種情況下沒有意義)。我肯定會分開代碼來從代碼中讀取輸入來進行計算。 (我可能會把前者留在'main'中。)'void locker_monkey(unsigned locker_count)'是一個非常簡單的函數來測試和調試。 –

+0

@MartinBonner你是對的,改變了。如果您看到其他內容,請隨時編輯。 – Aziuth

+0

@Aziuth嘿男人,非常感謝程序的重寫。我用關於雙循環的建議從0重寫了我的意思。我編譯了你的和我的,如果amount_lockers是10,他們的結果是0,1,4,9。雖然問題只是堅持2,5,10 –

相關問題