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. typedef struct {
  22. int is_valid;
  23. u64 calc_dividend;
  24. u64 calc_remainder;
  25. } VerificationResult;
  26.  
  27. VerificationResult verify_result(u64 dividend, u64 divisor, u64 quotient, u64 remainder) {
  28. VerificationResult result;
  29. result.calc_dividend = divisor * quotient + remainder;
  30. result.calc_remainder = dividend % divisor;
  31. result.is_valid = (result.calc_dividend == dividend) && (remainder < divisor);
  32. return result;
  33. }
  34.  
  35. // 測試案例結構
  36. typedef struct {
  37. u64 dividend;
  38. u64 divisor;
  39. u64 expected_quotient;
  40. u64 expected_remainder;
  41. } TestCase;
  42.  
  43. // 執行測試並打印結果
  44. void run_test_case(TestCase test) {
  45. u64 quotient, remainder;
  46. nvme_u64_div(test.dividend, test.divisor, &quotient, &remainder);
  47. VerificationResult ver_result = verify_result(test.dividend, test.divisor, quotient, remainder);
  48.  
  49. printf("Dividend: %llu\n", test.dividend);
  50. printf("Divisor: %llu\n", test.divisor);
  51. printf("Expected Quotient: %llu\n", test.expected_quotient);
  52. printf("Actual Quotient: %llu\n", quotient);
  53. printf("Expected Remainder: %llu\n", test.expected_remainder);
  54. printf("Actual Remainder: %llu\n", remainder);
  55. printf("Verification Result:\n");
  56. printf(" Calculated Dividend: %llu\n", ver_result.calc_dividend);
  57. printf(" Calculated Remainder: %llu\n", ver_result.calc_remainder);
  58. printf(" Is Valid: %s\n", ver_result.is_valid ? "Yes" : "No");
  59. printf("Test %s\n\n", (quotient == test.expected_quotient && remainder == test.expected_remainder && ver_result.is_valid) ? "PASSED" : "FAILED");
  60. }
  61.  
  62. int main() {
  63. // 測試案例數組
  64. TestCase test_cases[] = {
  65. // 正常案例
  66. {1000000000000000000ULL, 3ULL, 333333333333333333ULL, 1ULL},
  67. {9223372036854775807ULL, 2ULL, 4611686018427387903ULL, 1ULL},
  68. {18446744073709551615ULL, 9223372036854775807ULL, 2ULL, 1ULL},
  69. // 邊界案例
  70. {18446744073709551615ULL, 1ULL, 18446744073709551615ULL, 0ULL},
  71. {18446744073709551615ULL, 18446744073709551615ULL, 1ULL, 0ULL},
  72. {0ULL, 18446744073709551615ULL, 0ULL, 0ULL},
  73. // 特殊案例
  74. {18446744073709551614ULL, 2ULL, 9223372036854775807ULL, 0ULL},
  75. {18446744073709551615ULL, 2ULL, 9223372036854775807ULL, 1ULL},
  76. {18446744073709551615ULL, 3ULL, 6148914691236517205ULL, 0ULL},
  77. // 質數案例
  78. {18446744073709551557ULL, 11ULL, 1676976733973595596ULL, 1ULL},
  79. {18446744073709551557ULL, 18446744073709551557ULL, 1ULL, 0ULL},
  80. // 2的冪次方案例
  81. {9223372036854775808ULL, 2ULL, 4611686018427387904ULL, 0ULL},
  82. // 大數除以3^6案例
  83. {18446744073709551615ULL, 729ULL, 25304175686842458ULL, 603ULL},
  84. // 其他案例
  85. {18446744073709551615ULL, 10ULL, 1844674407370955161ULL, 5ULL},
  86. {18446744073709551615ULL, 9223372036854775806ULL, 2ULL, 3ULL},
  87. {12345678901234567890ULL, 98765432109876543ULL, 125ULL, 0ULL},
  88. {18446744073709551615ULL, 18446744073709551610ULL, 1ULL, 5ULL}
  89. };
  90.  
  91. int num_tests = sizeof(test_cases) / sizeof(test_cases[0]);
  92. for (int i = 0; i < num_tests; i++) {
  93. printf("Test Case %d:\n", i + 1);
  94. run_test_case(test_cases[i]);
  95. }
  96.  
  97. return 0;
  98. }
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
Verification Result:
  Calculated Dividend: 1000000000000000000
  Calculated Remainder: 1
  Is Valid: Yes
Test PASSED

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

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

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

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

Test Case 6:
Dividend: 0
Divisor: 18446744073709551615
Expected Quotient: 0
Actual Quotient: 0
Expected Remainder: 0
Actual Remainder: 0
Verification Result:
  Calculated Dividend: 0
  Calculated Remainder: 0
  Is Valid: Yes
Test PASSED

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

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

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

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

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

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

Test Case 13:
Dividend: 18446744073709551615
Divisor: 729
Expected Quotient: 25304175686842458
Actual Quotient: 25304175684100893
Expected Remainder: 603
Actual Remainder: 618
Verification Result:
  Calculated Dividend: 18446744073709551615
  Calculated Remainder: 618
  Is Valid: Yes
Test FAILED

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

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

Test Case 16:
Dividend: 12345678901234567890
Divisor: 98765432109876543
Expected Quotient: 125
Actual Quotient: 124
Expected Remainder: 0
Actual Remainder: 98765319609876558
Verification Result:
  Calculated Dividend: 12345678901234567890
  Calculated Remainder: 98765319609876558
  Is Valid: Yes
Test FAILED

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