1. 程式人生 > >兩個工具 輸出中間結果,計時函數

兩個工具 輸出中間結果,計時函數

int time return 功能 cloc spa const actor 常數

 1 #include <stdio.h>
 2 #include <time.h>
 3 int main() {
 4     const int MOD = 1000000;
 5     int n, s = 0;
 6     //scanf("%d", &n);
 7     n =10000000;
 8     for (int i = 1; i <= n; i++) {
 9         int factorial = 1;
10         for (int j = 1; j <= i; j++)
11             factorial = (factorial*j%MOD);
12 s = (s + factorial) % MOD; 13 } 14 cout << s; 15 printf("Time used=%.2f\n", (double)clock() / CLOCKS_PER_SEC); 16 17 return 0; 18 }

clock()為計時函數,要引入<time.h>庫 其功能為返回從程序運行開始到打印出的時間 除以常數CLOCKS_PRE_SEC後得到的值以秒為單位。

兩個工具 輸出中間結果,計時函數