int count_1; int count_2; voidlucky(int& num, int luck) { if (num<luck*4) return; if (num == luck * 4 || num == luck * 7) { num = 0; count_1++; return; }
lucky(num, luck*4); if (num == 0) return; lucky(num, luck*7); }
voidcomparator(int num) { if (num == 1) return; while (num!=0) { if (num == 1) { count_2++; break; } if (num % 4 != 0 && num % 7 != 0) break; if (num % 4 == 0) num /= 4; if (num % 7 == 0) num /= 7; } }
int* generateRandomArr(int max, int len){ int* arr = newint[len]; for (int i = 0; i < len; i++) arr[i] = rand() % max; return arr; }
int* cpyArr(int* src, int len){ int* des = newint[len]; memcpy(des, src, len * sizeof(int)); return des; }
intmain() { srand(time(NULL)); int testTimes = 10000000;//测试次数 int arrMaxLen = 1000;//数组最大长度 int max = 100000;//最大数据
for (int i = 0; i < testTimes; i++) { int arrLen = rand() % arrMaxLen; int* arr_1 = generateRandomArr(max, arrLen); int* arr_2 = cpyArr(arr_1, arrLen); count_1 = 0; count_2 = 0;
for (int i = 0; i < arrLen; i++) { lucky(arr_1[i], 1); comparator(arr_2[i]); } if (count_1 != count_2) { std::cout << "go wrong" << std::endl; for (int i = 0; i < arrLen; i++) std::cout << arr_2[i] << " "; std::cout << std::endl; std::cout << count_1 << std::endl; std::cout << count_2 << std::endl; break; } else std::cout << "success" << std::endl; }