C++ 入門指南 V2.00 - 單元 22 範例及練習程式碼



time_test.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <ctime>
#include <iostream>
  
int main() {
   time_t t1 = time(NULL);
   unsigned long i = 1000000000;
   std::cout << i << std::endl;
    
   while (true) {
      if (i == 0) {
         break;
      }
       
      i--;
   }
 
   time_t t2 = time(NULL);
    
   std::cout << t2 - t1 << std::endl;
 
   return 0;
}
 
/* time_test.cpp
   Kaiching Chang
   2014-5 */

exercise2201.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <algorithm>
 
using namespace std;
 
int main(void) {
   string s = "0123456789";
   random_shuffle(s.begin(), s.end());
   cout << s.substr(0, 4) << endl;
}
 
/* 檔名: exercise2201.cpp
   作者: Kaiching Chang
   時間: 2014-5 */

exercise2202.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>
 
using namespace std;
 
int myrandom(int i) {
   return rand() % i;
}
 
int main(void) {
   srand(time(0));
 
   string s = "0123456789";
   random_shuffle(s.begin(), s.end(), myrandom);
   cout << s.substr(0, 4) << endl;
}
 
/* 檔名: exercise2202.cpp
   作者: Kaiching Chang
   時間: 2014-5 */

the end

沒有留言: