2016-11-09 12 views
-1

基本フィボナッチ数でベース10を変換する単純なC++プログラムの内容は何ですか? これはフィボナッチ数を使用して番号を記述する方法です:ベース10フィボナッチ数

#include <iostream> 
using namespace std; 
int n, a, b, c, i; 
int main() { 
cin >> n; 
cout << n << " " << "=" << " "; 
    while(n > 0) { 
     a = 0; 
     b = 1; 
     c = 1; 
     while(c <= n) { 
      a = b; 
      b = c; 
      c = a + b; 
     } 
     if(b < n) cout << b << " " << "+" << " "; 
     else cout << b; 
     n = n - b; 
    } 
    return 0; 
} 
+0

「ベースフィボナッチ」とは呼ばないでください。そして、何が問題なの?一見すると、あなたのプログラムはあなたが望むことをしているようです。 – deviantfan

+0

ちょうど楽しみのために、私はあなたのコードを修正し、解決策を考え出しました。おそらく最高ではないかもしれませんが、[このページ](http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibrep.html#section4)の結果と一致する簡単なテストがいくつかあります。 (コメントのない)コードはhttps://ideone.com/98wRrMです。 –

+0

これは、フィボナッチ数を使って数値を書く方法です。私はベース10の数字をベースフィボナッチ(またはそれがどのように呼び出される)に変換したいと思う[リンク] \(http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibrep。 html#section4) –

答えて

0

this websiteによると、あなたは他のフィボナッチ数の和として「ベースフィボナッチ」で数字を書くことができます。実施例:図8は、第5のFIB数であり、2は2番目である

10 = 8 + 2 

。あなたは、「ベースフィボナッチ」に書き込むときに、あなたは5と進数のようにそれを書き込み、第2の「ビット」が設定:

10010 base fib = 10 base 10 
^^
8 2 

をので、このコードは整数の各ビットは、FIBを表す整数を作成します順番に番号。そして、単に数(私は一緒に追加することができるのFIB番号を見つけるために、OPのコードを使用しました)印刷されます。

int n, a, b, c, i; 
cin >> n; 
cout << n << " " << "=" << " "; 
bitset<32> bits; // Use a bitset to store the digits 
while(n > 0) { 
    a = 0; 
    b = 1; 
    c = 1; 
    int count = 0; // "count" is the nth fib # calculated 
    while(c <= n) { 
     count += 1; 
     a = b; 
     b = c; 
     c = a + b; 
    } 
    bits.set(count - 1); // Set the bit 
    if(b < n) cout << b << " " << "+" << " "; 
    else cout << b; 
    n = n - b; 
} 
cout << endl; 

// Convert binary to string of 0s and 1s 
const string str_bits = bits.to_string(); 
const auto first_digit = str_bits.find('1') ; // locate the first '1' 

// if first_digit is NOT std::string::npos, we found the first 1 
if(first_digit != std::string::npos) { // found it; print the substring starting at the first '1' 
    std::cout << str_bits.substr(first_digit) << endl; 
} 
// Not found, so it's just 0 
else { 
    std::cout << "0" << endl ; // all the bits were zeroes 
} 

注意を、ウェブサイトによると、どの2つの連続したフィボナッチ数はで使用することはできません同額。このコードはその制限に対処しません。

+0

もう一度ありがとう!その制限を解決するには? –

関連する問題