2014-03-03 2 views
0

먼저 작업을 읽어 보시기 바랍니다 : http://codeabbey.com/index/task_view/neumanns-random-generator노이만의 랜덤 생성기

내가 반복 횟수를 추적해야합니다,하지만 나는 아주 이상한 결과를 얻을 수 있습니다. 작업 후 예제에서 우리는 0001과 4100의 숫자를 가지고 있으며 2와 4 번의 반복 작업 후에 루프를 수행해야합니다. 하지만 내 결과는 1, 4 또는 내가 카운터의 위치를 ​​변경하는 경우 2 또는 5하지만 결코 2, 여기 4. 내 코드입니다 :

#include <iostream> 
#include <math.h> 
#include <stdlib.h> 
#include <vector> 
#include <algorithm> 

using namespace std; 

int main() 
{ 
    int n; 
    int value; 
    int counter; 
    int result; 
    int setvalue = 1; // use to exit the loop if setvalue == 0; 
    cin >> n; 
    vector<int> new_results(0); // use to store all the results from iterations 
    vector<int> results_vec(0); // use to store the number of iterations for each number 

    for (int i = 0; i < n ; i++) 
    { 

     cin >> value; 
     while(setvalue == 1) 
     { 
      value = value*value; 

      value = (value % 1000000)/100; 

      if(find(results_vec.begin(), results_vec.end(), value) == results_vec.end()) 
      { 
       results_vec.push_back(value); 
      } 
      else 
      { 
       counter = results_vec.size(); 
       new_results.push_back(counter); 
       setvalue = 0; 
      } 

     } 
     results_vec.clear(); 


    } 
    for (int i = 0; i < new_results.size() ; i++) 
    { 
     cout << new_results[i] << " "; 
    } 

} 
+0

0001과 4100으로 시작하면 중간 번호는 무엇입니까? – tgmath

+0

0001의 경우 나는 (0, 0) 4100의 경우 (8100, 6100, 2100, 4100) – Vallerious

+0

'while' 루프를 시작하기 전에 초기 값을'results_vec'에 넣으면 안되나요? – pjs

답변

2

당신이 가지고있는 방법은 정말 문자열의 외출 매우 추악하고 극도로 비싸다.

사용

(value % 1000000)/100;

대신 중간 네 자리 숫자를 추출합니다. 이 작업은 (1) 계수를 가져 와서 맨 앞의 두 자리를 제거한 다음 (2) 정수 나누기를 사용하여 마지막 두 자리를 제거합니다.

너무 간단하기 때문에 버그를 고칠 수도 있습니다.

+0

int가 9999^2에 비해 너무 작 으면 오래 사용해야합니다. – tgmath

+0

@tgmath 9999^2는 2,997,483,647 (2^31-1)보다 훨씬 낮은 약 99,980,001이므로 일반적인 플랫폼 (즉, int는 32 비트 이상)에서 충분합니다. 물론이 표준은 int가 적어도 32 비트를 보장하지는 않습니다 : 보장 된 하한은 16 비트입니다. – stefan

+0

와우, 고마워! 결국 문자열 변환을 사용할 필요가 없었습니다. 하지만 문제는 남아 있습니다. if 문이 벡터에 이미 있음을 발견하면 루프에 왔음을 의미합니다. 반복하고 그것을 다른 사람에게 점프하여 new_results 벡터에 추가합니다. – Vallerious

0

올바른 코드는 다음과 같습니다. 도움을 주셔서 감사합니다.

#include <iostream> 
#include <math.h> 
#include <stdlib.h> 
#include <vector> 
#include <algorithm> 

using namespace std; 

int main() 
{ 
    int n; 
    int value; 
    int counter; 
    int result; 
    cin >> n; 
    vector<int> new_results(0); // use to store all the results from iterations 
    vector<int> results_vec(0); // use to store the number of iterations for each number 

    for (int i = 0; i < n ; i++) 
    { 

     cin >> value; 
     results_vec.push_back(value); 
     while(true) 
     { 
      value = value*value; 

      value = (value % 1000000)/100; 

      if(find(results_vec.begin(), results_vec.end(), value) == results_vec.end()) 
      { 
       results_vec.push_back(value); 
      } 
      else 
      { 
       counter = results_vec.size(); 
       new_results.push_back(counter); 
       break; 
      } 

     } 
     results_vec.clear(); 


    } 
    for (int i = 0; i < new_results.size() ; i++) 
    { 
     cout << new_results[i] << " "; 
    } 

} 
관련 문제