Fibonacci-Programm
-
Hallo,brauche Hilfe beim erstellen von folgendem Programm,weiß leider garnicht wie cih anfangen soll:
Es soll Prüfen ob eine eingegebene Zahl eine Fibonacci-Zahl ist und dann den Wert true bzw. false ausgeben.
Fibonacci-Folge:
http://de.wikipedia.org/wiki/Fibonacci-FolgeVielen Dank schon mal
-
for(i=0;;++i) if fib(i)>=x return fib(i)==xund dann noch statt einer funktion fib die zahlen in der fleichen schleife hochscrauben mit += und swap.
-
bool isfib(unsigned n) { unsigned f[2] = {0, 1}, i = 0; while (n > f[i]) { f[i] += f[i^1]; i^=1; } return n == f[i]; }ungetestet
ToniKucok: Bei der Eingabe könnte man auch anfangen

-
Bashar schrieb:
Bei der Eingabe könnte man auch anfangen

Also zuerst sollte doch der "Kern" des Programmes programmiert werden und dann das Interface oder?
-
//============================================================================ // Name : FibC++.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> using namespace std; template <int N> struct Fib { enum { ret = Fib<N-1>::ret + Fib<N-2>::ret}; }; template <> struct Fib<1> { enum { ret = 1}; }; template <> struct Fib<0> { enum {ret = 0}; }; template <int A, int B=A> struct isFib { enum { ret = (( Fib<A>::ret != B ) ? isFib<A-1, B>::ret : 1) }; }; template<int B> struct isFib<-1, B> { enum {ret = 0}; }; template <> struct isFib<2, 2> { enum { ret = 1 }; }; template <> struct isFib<3, 3> { enum { ret = 1 }; }; int main() { cout << isFib<8>::ret << endl; // prints !!!Hello World!!! return 0; }
-
ToniKukoc schrieb:
Hallo,brauche Hilfe beim erstellen von folgendem Programm,weiß leider garnicht wie cih anfangen soll:
Es soll Prüfen ob eine eingegebene Zahl eine Fibonacci-Zahl ist und dann den Wert true bzw. false ausgeben.
.. komisch, es hat noch keiner gesagt, dass wir hier keine Hausaufgaben machen.

Aber man darf Lösungen posten, die Du unmöglich als Deine eigene ausgeben kannst.#include <boost/operators.hpp> // boost::input_iterator_helper<> #include <algorithm> // find_if #include <functional> // bind2nd, greater_equal #include <iostream> template< typename T > class FiboIterator : public boost::input_iterator_helper< FiboIterator< T >, T > { public: explicit FiboIterator( const T& x = T(0) ) : m_f1( 1 ), m_f2( x ) {} T operator*() const { return m_f2; } FiboIterator& operator++() { T f3 = m_f1 + m_f2; m_f1 = m_f2; m_f2 = f3; return *this; } bool operator==( const FiboIterator& b ) const { return m_f2 == b.m_f2; } private: T m_f1, m_f2; }; template< typename T > bool is_fibonacci( const T& x ) { return *std::find_if( FiboIterator< T >(), FiboIterator< T >(-1), std::bind2nd( std::greater_equal< T >(), x ) ) == x; } int main() { using namespace std; for( int zahl; cout << "Eine Zahl bitte: ", cin >> zahl; ) cout << "Die Zahl " << zahl << " ist " << (is_fibonacci( zahl )? "eine": "keine") << " Fibonaccizahl" << endl; return 0; }(Link zu boost.input_iterator_helper)
Gruß
Werner
-
#include <iostream> bool is_fibonacci(int x,int a=0,int b=1) { return x==a||x>a&&is_fibonacci(x,a+b,a); } int main() { using namespace std; for(int z;cout<<"Eine Zahl bitte: ",cin>>z;) cout<<"Die Zahl "<<z<<" ist "<<"keine " "Fibonaccizahl"+is_fibonacci(z)<<endl; }sowas macht doch immer wieder spaß.
