2015-01-15 32 views
0

我一直在研究這個問題一段時間,但每次提交時我都會得到錯誤的答案,但是當我輸入樣本案例時,我似乎產生了正確的答案。任何人都可以幫我在這裏嗎? 問題可以在這個網站上找到:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=36UVA在線評委3n + 1:錯誤的答案

代碼本身:

#include <vector> 
#include <stdio.h> 
#include <iostream> 
#include <algorithm> 
using namespace std; 

long find_cycle_length(long b) 
{ 
    // Finds the max cycle of b 
    long max_cycle = 1; 
    while (b != 1) 
    { 
     if (b % 2 != 0) 
     { 
      b = (3 * b) + 1; 
      ++max_cycle; 
     } 
     else if (b % 2 == 0) 
     { 
      b /= 2; 
      ++max_cycle; 
     } 
    } 
    return max_cycle; 
} 
long find_max_cycle(vector <long>& b) 
{ 
    vector <long> temp; 
    for (int i = 0; i < b.size(); ++i) 
    { 
     long buffer = b[i]; 
     temp.push_back(find_cycle_length(buffer)); 
    } 
    long max_cycle = *max_element(temp.begin(), temp.end()); 

    return max_cycle; 
} 

int main() 
{ 
     long i = 0; // First number 
     long j = 0; // Second number 
     long size = 0; // Determines the size of the vector buffer 
     long counter = 0; // Used to fill buffer 
     cin >> i >> j; 


     if (j > i) { 
      size = (j - i) + 1; 
      counter = i; 
     } 
     else if (i > j) { 
      size = (i - j) + 1; 
      counter = j; 
     } 
     else if (i == j) 
     { 
      size = 1; 
      counter = i; 
     } 

     vector<long> buffer(size); // Used to store all numbers i to j 
     for (int x = 0; x < buffer.size(); ++x) // fill buffer 
     { 
      buffer[x] = counter; 
      ++counter; 
     } 

     cout << i << " " << j << " " << find_max_cycle(buffer) << endl; 

     return 0; 

} 
+4

我想你忘了閱讀這部分內容:「輸入將包含一系列的整數對」。有不止一個。 – molbdnilo

+0

你的設置值得懷疑。您可以找到每個最大週期*每對線*,輸出,並移至下一行和下一對。無論如何,我都看不到「緩衝器」的用處。 – WhozCraig

回答

0

我想你可能誤解的說明。樣品輸入不是

1 10 

1 10 
100 200 
201 210 
900 1000 

你沒有一個while循環,讓用戶給你輸入的多行 - 一個後您的程序退出。你爲什麼不做這樣的改變 - 讓用戶不斷地輸入新的行,直到給你一個文件結束符(或者 - 對於編碼來說它是一樣的 - 接受來自輸入的所有行輸入文件重定向到標準輸入) - 並看看你是否得到了一個好嗎?

哦,我看到molbdnilo在第一條評論中提出了這個建議。無論如何:他是對的。