Jelajahi Sumber

add chap2 code

hanojiang 1 tahun lalu
induk
melakukan
a0b3874116
1 mengubah file dengan 28 tambahan dan 1 penghapusan
  1. 28 1
      EssentialCpp/main.cpp

+ 28 - 1
EssentialCpp/main.cpp

@@ -2,6 +2,33 @@
 
 using namespace std;
 
+bool Fibon_Elem(int pos, int& elem) {
+	bool ret = false;
+	elem = 1;
+	int first = 1, second = 1;
+	
+	if (pos <= 0) {
+		return ret;
+	}
+
+	for (int ix = 3; ix <= pos; ++ix) {
+		elem = first + second;
+		first = second;
+		second = elem;
+	}
+	return true;
+}
+
+ 
 int main() {
-	cout << "hello world!" << endl;
+	int elem = 0;
+	bool ret = false;
+	for (int i = 0; i < 10; ++i) {
+		ret = Fibon_Elem(i, elem);
+		if (!ret)
+			cout << "Position " << i <<" is invalid!" << endl;
+		else
+			cout << "ret=" << ret << " " << elem << endl;
+	}
+	
 }