2013-09-21 5 views
0

그래서 수업 과제에 문제가 있습니다. 목표는 길이가 인 벡터 n 만 이진 정수 (0 또는 1)로 채 웁니다. 전의. [1,1,0,1] 여기서 v [0] = 1, v [1] = 1, v [2] = 0, v [3] = 1이다. 함수 ItBin2Dec의 출력은 같은 정수 그래서 [1,1,0,1] => [1,3] (13). 나는 훌륭한 프로그래머가 아니기 때문에 우리에게 주어진 알고리즘을 따라 가려고 노력했다. 모든 조언을 크게 주시면 감사하겠습니다.이진수의 벡터를 숫자로 된 벡터로 변환

/* Algorithm we are given 

function ItBin2Dec(v) 
Input: An n-bit integer v >= 0 (binary digits) 
Output: The vector w of decimal digits of v 

Initialize w as empty vector 
if v=0: return w 
if v=1: w=push(w,1); return w 
for i=size(v) - 2 downto 0: 
    w=By2inDec(w) 
    if v[i] is odd: w[0] = w[0] + 1 
return w 

*/ 

#include <vector> 
#include <iostream> 

using namespace std; 

vector<int> ItBin2Dec(vector<int> v) { 
    vector<int> w; // initialize vector w 
    if (v.size() == 0) { // if empty vector, return w 
     return w; 
    } 
    if (v.size() == 1) { // if 1 binary number, return w with that number 
     if (v[0] == 0) { 
      w.push_back(0); 
      return w; 
     } 
     else { 
      w.push_back(1); 
      return w; 
     } 
    } 
    else { // if v larger than 1 number 
     for (int i = v.size() - 2; i >= 0; i--) { 
      w = By2InDec(w); // this supposedly will multiply the vector by 2 
      if (v[i] == 1) { // if v is odd 
       w[0] = w[0] + 1; 
      } 
     } 
    } 
    return w; 
} 

vector<int> By2InDec(vector<int> y) { 
    vector<int> z; 
    // not sure how this one works exactly 
    return z; 
} 

int main() { 
    vector<int> binVect; // init binary vect 
    vector<int> decVect; // init decimal vect 

    decVect = ItBin2Dec(binVect); // calls ItBin2Dec and converts bin vect to dec vect 
    for (int i = decVect.size(); i >= 0; i--) { // prints out decimal value 
     cout << decVect[i] << " "; 
    } 
    cout << endl; 



    return 0; 
} 

나는 약간 녹슬어 졌으므로 코드를 작성해야하기 때문에 오래되었습니다. 분명히 실제 입력으로 설정하지 않았으며, 먼저 뼈대를 먼저 얻으려고합니다. 실제 할당은 2 진수 벡터의 곱셈을 요구 한 다음 결과 숫자 벡터를 출력하지만,이 처음부터 시작하여 거기에서부터 작업 할 것이라고 생각했습니다. 감사! 진수로 진수의 숫자를 변환

답변

1

쉽고, 그래서 당신이 먼저 진수를 계산하고, 나중에 수의 자리를 얻기 위해 제안 :

int binary_to_decimal(const std::vector<int>& bits) 
{ 
    int result = 0; 
    int base = 1; 

    //Supposing the MSB is at the begin of the bits vector: 
    for(unsigned int i = bits.size()-1 ; i >= 0 ; --i) 
    { 
     result += bits[i]*base; 
     base *= 2; 
    } 

    return result; 
} 

std::vector<int> get_decimal_digits(int number) 
{ 
    //Allocate the vector with the number of digits of the number: 
    std::vector<int> digits(std::log10(number) - 1); 

    while(number/10 > 0) 
    { 
     digits.insert(digits.begin() , number % 10); 
     number /= 10; 
    } 

    return digits; 
} 


std::vector<int> binary_digits_to_decimal_digits(const std::vector<int>& bits) 
{ 
    return get_decimal_digits(binary_to_decimal(bits)); 
} 
+0

나는이 방법을 좋아한다. 그러나 과제의 요구 사항을 충족시키지 못합니다. 우리는 300 1보다 큰 벡터 (1600까지)를 전달할 수 있다고 가정하고 있습니다. 그리고 이것은 실제로 32 1을 입력 할 수 있기 때문에 결국 오버플로되어 오류를 일으 킵니다. 우리 교수가 우리가 피하기를 원하는 것입니다. 이 질문은 올바른 답으로 받아 들여지기에 좋은 곳이기 때문에 올바른 답으로 생각합니다. 감사! – Scott

관련 문제