Skip to content

Instantly share code, notes, and snippets.

@goodjack
Created November 15, 2017 10:01
Show Gist options
  • Save goodjack/dc4fe320ec0a6a88b2374fefd98936c5 to your computer and use it in GitHub Desktop.
Save goodjack/dc4fe320ec0a6a88b2374fefd98936c5 to your computer and use it in GitHub Desktop.
費氏數列
#include <iostream>
using namespace std;
int main()
{
int numA = 1, numB = 1;
cout << numA << ", " << numB << ", ";
int oldNumA, oldNumB;
while (numA + numB < 100) {
cout << numA + numB << ", ";
// 先把改變前的數字存起來
oldNumA = numA;
oldNumB = numB;
numB = oldNumA + oldNumB; // 新的B是舊的A加上舊的B
numA = oldNumB; // 新的A是舊的B
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int numA = 1, numB = 1;
cout << numA << ", " << numB << ", ";
while (numA + numB < 100) {
cout << numA + numB << ", ";
numB = numA + numB; // 新的B是「舊的A加上舊的B」
numA = numB - numA; // 新的A是「舊的B」,舊的B是「新的B減掉舊的A」
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment