fork(12) download
  1. #include <ctime>
  2. #include <iostream>
  3.  
  4. // peelは入力されたbitをまるでバナナの皮をむくように移動する。
  5. // 0 bit目 -> 15 bit目
  6. // 1 bit目 -> 0 bit目
  7. // 2 bit目 -> 14 bit目
  8. // 3 bit目 -> 1 bit目
  9. // 4 bit目 -> 13 bit目
  10. // 5 bit目 -> 2 bit目
  11. // ...
  12. // n = 奇数の時, n bit目 -> (n - 1) / 2 bit目
  13. // n = 偶数の時, n bit目 -> 15 - (n / 2) bit目
  14. // (例)
  15. // a : 0000111100001111
  16. // o : 1100110000110011
  17. uint16_t peel(uint16_t a) {
  18. // ここに実装
  19. return a;
  20. }
  21.  
  22. void assert_equal(uint16_t input, uint16_t expected) {
  23. if (input != expected) {
  24. std::cout << "input = " << std::hex << input << std::endl
  25. << "expected = " << std::hex << expected << std::endl
  26. << "failed" << std::endl;
  27. exit(EXIT_FAILURE);
  28. }
  29. }
  30.  
  31. int main() {
  32. // テスト
  33. assert_equal(peel(0x0f0f), 0xcc33);
  34. assert_equal(peel(0x5555), 0xff00);
  35. assert_equal(peel(0xff00), 0x0ff0);
  36. assert_equal(peel(0x0ff0), 0x3c3c);
  37. assert_equal(peel(0x3c3c), 0x6666);
  38. assert_equal(peel(0x6666), 0x5555);
  39. assert_equal(peel(0x4567), 0xdd05);
  40. assert_equal(peel(0x23c6), 0x5859);
  41. assert_equal(peel(0x9869), 0x92a6);
  42. assert_equal(peel(0x4873), 0xb125);
  43. assert_equal(peel(0xdc51), 0xb7a0);
  44. assert_equal(peel(0x5cff), 0xf72f);
  45.  
  46. // ベンチマーク
  47. std::cout << "benchmark...";
  48. uint32_t dammy = 0;
  49. double start = clock();
  50. for (int i = 0; i < 50000000; i++) {
  51. dammy ^= peel(rand());
  52. }
  53. double end = clock();
  54. std::cout << "end! elapsed:" << (end - start) / CLOCKS_PER_SEC << "s " << dammy << std::endl;
  55. return 0;
  56. }
Runtime error #stdin #stdout 0s 4160KB
stdin
Standard input is empty
stdout
input = f0f
expected = cc33
failed