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



time_test.cpp


#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


#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


#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

沒有留言: