計算: 總合=1+2+4+7+11...
#include <iostream> #include <stdio.h> using std::cout; using std::cin; using std::endl; main () { long sum=0,num=1; //設定暫存數字和總合 long times; //讀入要跑的次數 cout << "N="; //輸出字串 cin >> times; //讀入字數 for (long i=0;i<times;i++) { num = i + num; //1+2+4+7+11... // cout << num << "+"; //顯示過程 sum += num; //加總 } cout << "Sum=" << sum << endl;//印出總合 system("pause"); //暫停程式 return 0; }
http://pastie.caboo.se/170514