HW-1 for Chen

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int floor = 1;
  7. int N;
  8. int count = 0;
  9. cout << "輸入層數N:";
  10. cin >> N;
  11. for (int i = 1; i <= N; i++)
  12. {
  13. if (i == 1)
  14. count++;
  15. else
  16. {
  17. count += i + floor;
  18. floor += i;
  19. }
  20. }
  21. cout << "當蘋果堆疊" << N << "層時,金字塔總共放了" << count << "個蘋果" << endl;
  22. system("pause");
  23. return 0;
  24. }

留言

這個網誌中的熱門文章

C# 井字遊戲