fork download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. typedef uint64_t u64;
  4.  
  5. // 修改後的除法函數,返回商和餘數
  6. void nvme_u64_div(u64 dividend, u64 divisor, u64* quotient, u64* remainder) {
  7. *quotient = 0;
  8. *remainder = 0;
  9. int i;
  10. for (i = 63; i >= 0; i--) {
  11. *remainder = (*remainder << 1) | ((dividend >> i) & 1);
  12. if (*remainder >= divisor) {
  13. *remainder -= divisor;
  14. *quotient |= (1ULL << i);
  15. }
  16. }
  17. }
  18.  
  19. // 測試案例結構
  20. typedef struct {
  21. u64 dividend;
  22. u64 divisor;
  23. u64 expected_quotient;
  24. u64 expected_remainder;
  25. } TestCase;
  26.  
  27. // 執行測試並打印結果
  28. void run_test_case(TestCase test) {
  29. u64 quotient, remainder;
  30. nvme_u64_div(test.dividend, test.divisor, &quotient, &remainder);
  31.  
  32. printf("Dividend: %llu\n", test.dividend);
  33. printf("Divisor: %llu\n", test.divisor);
  34. printf("Expected Quotient: %llu\n", test.expected_quotient);
  35. printf("Actual Quotient: %llu\n", quotient);
  36. printf("Expected Remainder: %llu\n", test.expected_remainder);
  37. printf("Actual Remainder: %llu\n", remainder);
  38. printf("Test %s\n\n",
  39. (quotient == test.expected_quotient && remainder == test.expected_remainder)
  40. ? "PASSED" : "FAILED");
  41. }
  42.  
  43. int main() {
  44. // 擴展的測試案例數組
  45. TestCase test_cases[] = {
  46. // 原有的測試案例
  47. {1000000000000000000ULL, 3ULL, 333333333333333333ULL, 1ULL},
  48. {9223372036854775807ULL, 2ULL, 4611686018427387903ULL, 1ULL},
  49. {18446744073709551615ULL, 9223372036854775807ULL, 2ULL, 1ULL},
  50. {18446744073709551615ULL, 1ULL, 18446744073709551615ULL, 0ULL},
  51. {18446744073709551615ULL, 18446744073709551615ULL, 1ULL, 0ULL},
  52. {0ULL, 18446744073709551615ULL, 0ULL, 0ULL},
  53. {18446744073709551614ULL, 2ULL, 9223372036854775807ULL, 0ULL},
  54. {18446744073709551615ULL, 2ULL, 9223372036854775807ULL, 1ULL},
  55. {18446744073709551615ULL, 3ULL, 6148914691236517205ULL, 0ULL},
  56. {18446744073709551557ULL, 11ULL, 1676976734155413778ULL, 9ULL},
  57. {18446744073709551557ULL, 18446744073709551557ULL, 1ULL, 0ULL},
  58. {9223372036854775808ULL, 2ULL, 4611686018427387904ULL, 0ULL},
  59.  
  60. // 新增的測試案例
  61. // 大數除以小數
  62. {18446744073709551615ULL, 10ULL, 1844674407370955161ULL, 5ULL},
  63.  
  64. // 接近 2^64 的數除以接近 2^63 的數
  65. {18446744073709551615ULL, 9223372036854775806ULL, 2ULL, 3ULL},
  66.  
  67. // 質數除法
  68. {18446744073709551557ULL, 17ULL, 1085102592571150091ULL, 10ULL},
  69.  
  70. // 連續數字除法
  71. {12345678901234567890ULL, 98765432109876543ULL, 124ULL, 98765319609876558ULL},
  72.  
  73. // 大數除以 1
  74. {18446744073709551615ULL, 1ULL, 18446744073709551615ULL, 0ULL},
  75.  
  76. // 0 除以任何數
  77. {0ULL, 12345ULL, 0ULL, 0ULL},
  78.  
  79. // 任何數除以自己
  80. {12345678901234567890ULL, 12345678901234567890ULL, 1ULL, 0ULL},
  81.  
  82. // 2 的冪次方除法
  83. {1ULL << 63, 1ULL << 31, 1ULL << 32, 0ULL},
  84.  
  85. // 大數除以大數,結果為小數
  86. {18446744073709551615ULL, 18446744073709551614ULL, 1ULL, 1ULL},
  87.  
  88. // Fibonacci 數列相鄰項的除法
  89. {17711ULL, 10946ULL, 1ULL, 6765ULL}, // F(22) / F(21)
  90.  
  91. // 大數除以 3 的冪次方
  92. {18446744073709551615ULL, 729ULL, 25304175686842458ULL, 603ULL}, // (2^64 - 1) / 3^6
  93.  
  94. // 接近 2^64 的數除以接近自身的數
  95. {18446744073709551615ULL, 18446744073709551610ULL, 1ULL, 5ULL}
  96. };
  97.  
  98. int num_tests = sizeof(test_cases) / sizeof(test_cases[0]);
  99. for (int i = 0; i < num_tests; i++) {
  100. printf("Test Case %d:\n", i + 1);
  101. run_test_case(test_cases[i]);
  102. #if 0
  103. if (test.dividend != test.divisor * test.expected_quotient + test.expected_remainder) {
  104. printf("Warning: Test case may be incorrect!\n");
  105. }
  106. #endif
  107. }
  108. return 0;
  109. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Test Case 1:
Dividend: 1000000000000000000
Divisor: 3
Expected Quotient: 333333333333333333
Actual Quotient: 333333333333333333
Expected Remainder: 1
Actual Remainder: 1
Test PASSED

Test Case 2:
Dividend: 9223372036854775807
Divisor: 2
Expected Quotient: 4611686018427387903
Actual Quotient: 4611686018427387903
Expected Remainder: 1
Actual Remainder: 1
Test PASSED

Test Case 3:
Dividend: 18446744073709551615
Divisor: 9223372036854775807
Expected Quotient: 2
Actual Quotient: 2
Expected Remainder: 1
Actual Remainder: 1
Test PASSED

Test Case 4:
Dividend: 18446744073709551615
Divisor: 1
Expected Quotient: 18446744073709551615
Actual Quotient: 18446744073709551615
Expected Remainder: 0
Actual Remainder: 0
Test PASSED

Test Case 5:
Dividend: 18446744073709551615
Divisor: 18446744073709551615
Expected Quotient: 1
Actual Quotient: 1
Expected Remainder: 0
Actual Remainder: 0
Test PASSED

Test Case 6:
Dividend: 0
Divisor: 18446744073709551615
Expected Quotient: 0
Actual Quotient: 0
Expected Remainder: 0
Actual Remainder: 0
Test PASSED

Test Case 7:
Dividend: 18446744073709551614
Divisor: 2
Expected Quotient: 9223372036854775807
Actual Quotient: 9223372036854775807
Expected Remainder: 0
Actual Remainder: 0
Test PASSED

Test Case 8:
Dividend: 18446744073709551615
Divisor: 2
Expected Quotient: 9223372036854775807
Actual Quotient: 9223372036854775807
Expected Remainder: 1
Actual Remainder: 1
Test PASSED

Test Case 9:
Dividend: 18446744073709551615
Divisor: 3
Expected Quotient: 6148914691236517205
Actual Quotient: 6148914691236517205
Expected Remainder: 0
Actual Remainder: 0
Test PASSED

Test Case 10:
Dividend: 18446744073709551557
Divisor: 11
Expected Quotient: 1676976734155413778
Actual Quotient: 1676976733973595596
Expected Remainder: 9
Actual Remainder: 1
Test FAILED

Test Case 11:
Dividend: 18446744073709551557
Divisor: 18446744073709551557
Expected Quotient: 1
Actual Quotient: 1
Expected Remainder: 0
Actual Remainder: 0
Test PASSED

Test Case 12:
Dividend: 9223372036854775808
Divisor: 2
Expected Quotient: 4611686018427387904
Actual Quotient: 4611686018427387904
Expected Remainder: 0
Actual Remainder: 0
Test PASSED

Test Case 13:
Dividend: 18446744073709551615
Divisor: 10
Expected Quotient: 1844674407370955161
Actual Quotient: 1844674407370955161
Expected Remainder: 5
Actual Remainder: 5
Test PASSED

Test Case 14:
Dividend: 18446744073709551615
Divisor: 9223372036854775806
Expected Quotient: 2
Actual Quotient: 2
Expected Remainder: 3
Actual Remainder: 3
Test PASSED

Test Case 15:
Dividend: 18446744073709551557
Divisor: 17
Expected Quotient: 1085102592571150091
Actual Quotient: 1085102592571150091
Expected Remainder: 10
Actual Remainder: 10
Test PASSED

Test Case 16:
Dividend: 12345678901234567890
Divisor: 98765432109876543
Expected Quotient: 124
Actual Quotient: 124
Expected Remainder: 98765319609876558
Actual Remainder: 98765319609876558
Test PASSED

Test Case 17:
Dividend: 18446744073709551615
Divisor: 1
Expected Quotient: 18446744073709551615
Actual Quotient: 18446744073709551615
Expected Remainder: 0
Actual Remainder: 0
Test PASSED

Test Case 18:
Dividend: 0
Divisor: 12345
Expected Quotient: 0
Actual Quotient: 0
Expected Remainder: 0
Actual Remainder: 0
Test PASSED

Test Case 19:
Dividend: 12345678901234567890
Divisor: 12345678901234567890
Expected Quotient: 1
Actual Quotient: 1
Expected Remainder: 0
Actual Remainder: 0
Test PASSED

Test Case 20:
Dividend: 9223372036854775808
Divisor: 2147483648
Expected Quotient: 4294967296
Actual Quotient: 4294967296
Expected Remainder: 0
Actual Remainder: 0
Test PASSED

Test Case 21:
Dividend: 18446744073709551615
Divisor: 18446744073709551614
Expected Quotient: 1
Actual Quotient: 1
Expected Remainder: 1
Actual Remainder: 1
Test PASSED

Test Case 22:
Dividend: 17711
Divisor: 10946
Expected Quotient: 1
Actual Quotient: 1
Expected Remainder: 6765
Actual Remainder: 6765
Test PASSED

Test Case 23:
Dividend: 18446744073709551615
Divisor: 729
Expected Quotient: 25304175686842458
Actual Quotient: 25304175684100893
Expected Remainder: 603
Actual Remainder: 618
Test FAILED

Test Case 24:
Dividend: 18446744073709551615
Divisor: 18446744073709551610
Expected Quotient: 1
Actual Quotient: 1
Expected Remainder: 5
Actual Remainder: 5
Test PASSED