fork download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. typedef uint64_t u64;
  5.  
  6. // 修改後的除法函數,返回商和餘數
  7. void nvme_u64_div(u64 dividend, u64 divisor, u64* quotient, u64* remainder) {
  8. *quotient = 0;
  9. *remainder = 0;
  10. int i;
  11. for (i = 63; i >= 0; i--) {
  12. *remainder = (*remainder << 1) | ((dividend >> i) & 1);
  13. if (*remainder >= divisor) {
  14. *remainder -= divisor;
  15. *quotient |= (1ULL << i);
  16. }
  17. }
  18. }
  19.  
  20. // 驗證函數
  21. int verify_result(u64 dividend, u64 divisor, u64 quotient, u64 remainder) {
  22. return (divisor * quotient + remainder == dividend) && (remainder < divisor);
  23. }
  24.  
  25. // 測試案例結構
  26. typedef struct {
  27. u64 dividend;
  28. u64 divisor;
  29. u64 expected_quotient;
  30. u64 expected_remainder;
  31. } TestCase;
  32.  
  33. // 執行測試並打印結果
  34. void run_test_case(TestCase test) {
  35. u64 quotient, remainder;
  36. nvme_u64_div(test.dividend, test.divisor, &quotient, &remainder);
  37. int verification_result = verify_result(test.dividend, test.divisor, quotient, remainder);
  38.  
  39. printf("Dividend: %llu\n", test.dividend);
  40. printf("Divisor: %llu\n", test.divisor);
  41. printf("Expected Quotient: %llu\n", test.expected_quotient);
  42. printf("Actual Quotient: %llu\n", quotient);
  43. printf("Expected Remainder: %llu\n", test.expected_remainder);
  44. printf("Actual Remainder: %llu\n", remainder);
  45. printf("Verification Result: %s\n", verification_result ? "PASSED" : "FAILED");
  46. printf("Test %s\n\n", (quotient == test.expected_quotient && remainder == test.expected_remainder && verification_result) ? "PASSED" : "FAILED");
  47. }
  48.  
  49. int main() {
  50. // 測試案例數組
  51. TestCase test_cases[] = {
  52. // 正常案例
  53. {1000000000000000000ULL, 3ULL, 333333333333333333ULL, 1ULL},
  54. {9223372036854775807ULL, 2ULL, 4611686018427387903ULL, 1ULL},
  55. {18446744073709551615ULL, 9223372036854775807ULL, 2ULL, 1ULL},
  56. // 邊界案例
  57. {18446744073709551615ULL, 1ULL, 18446744073709551615ULL, 0ULL},
  58. {18446744073709551615ULL, 18446744073709551615ULL, 1ULL, 0ULL},
  59. {0ULL, 18446744073709551615ULL, 0ULL, 0ULL},
  60. // 特殊案例
  61. {18446744073709551614ULL, 2ULL, 9223372036854775807ULL, 0ULL},
  62. {18446744073709551615ULL, 2ULL, 9223372036854775807ULL, 1ULL},
  63. {18446744073709551615ULL, 3ULL, 6148914691236517205ULL, 0ULL},
  64. // 質數案例
  65. {18446744073709551557ULL, 11ULL, 1676976733973595596ULL, 1ULL},
  66. {18446744073709551557ULL, 18446744073709551557ULL, 1ULL, 0ULL},
  67. // 2的冪次方案例
  68. {9223372036854775808ULL, 2ULL, 4611686018427387904ULL, 0ULL},
  69. // 大數除以3^6案例
  70. {18446744073709551615ULL, 729ULL, 25304175686842458ULL, 603ULL},
  71. // 其他案例
  72. {18446744073709551615ULL, 10ULL, 1844674407370955161ULL, 5ULL},
  73. {18446744073709551615ULL, 9223372036854775806ULL, 2ULL, 3ULL},
  74. {12345678901234567890ULL, 98765432109876543ULL, 125ULL, 0ULL},
  75. {18446744073709551615ULL, 18446744073709551610ULL, 1ULL, 5ULL}
  76. };
  77.  
  78. int num_tests = sizeof(test_cases) / sizeof(test_cases[0]);
  79. for (int i = 0; i < num_tests; i++) {
  80. printf("Test Case %d:\n", i + 1);
  81. run_test_case(test_cases[i]);
  82. }
  83.  
  84. return 0;
  85. }
Success #stdin #stdout 0.01s 5272KB
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
Verification Result: PASSED
Test PASSED

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

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

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

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

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

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

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

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

Test Case 10:
Dividend: 18446744073709551557
Divisor: 11
Expected Quotient: 1676976733973595596
Actual Quotient: 1676976733973595596
Expected Remainder: 1
Actual Remainder: 1
Verification Result: PASSED
Test PASSED

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

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

Test Case 13:
Dividend: 18446744073709551615
Divisor: 729
Expected Quotient: 25304175686842458
Actual Quotient: 25304175684100893
Expected Remainder: 603
Actual Remainder: 618
Verification Result: PASSED
Test FAILED

Test Case 14:
Dividend: 18446744073709551615
Divisor: 10
Expected Quotient: 1844674407370955161
Actual Quotient: 1844674407370955161
Expected Remainder: 5
Actual Remainder: 5
Verification Result: PASSED
Test PASSED

Test Case 15:
Dividend: 18446744073709551615
Divisor: 9223372036854775806
Expected Quotient: 2
Actual Quotient: 2
Expected Remainder: 3
Actual Remainder: 3
Verification Result: PASSED
Test PASSED

Test Case 16:
Dividend: 12345678901234567890
Divisor: 98765432109876543
Expected Quotient: 125
Actual Quotient: 124
Expected Remainder: 0
Actual Remainder: 98765319609876558
Verification Result: PASSED
Test FAILED

Test Case 17:
Dividend: 18446744073709551615
Divisor: 18446744073709551610
Expected Quotient: 1
Actual Quotient: 1
Expected Remainder: 5
Actual Remainder: 5
Verification Result: PASSED
Test PASSED