gpt4 book ai didi

c - 我的程序有时会返回 true,但并不总是 Codewars 问题 "Are they same"

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:39:53 24 4
gpt4 key购买 nike

我是一个新手,我被 Codewars 的这个问题困住了。问题是;

  • "Given two arrays a and b write a function comp(a, b) (compSame(a, b) in Clojure) that checks whether the two arrays have the "same" elements, with the same multiplicities. "Same" means, here, that the elements in b are the elements in a squared, regardless of the order."*

    a = [121, 144, 19, 161, 19, 144, 19, 11]
    b = [121, 14641, 20736, 361, 25921, 361, 20736, 361]

    comp(a, b) returns true because in b 121 is the square of 11, 14641 is the square of 121, 20736 the square of 144, 361 the square of 19, 25921 the square of 161, and so on.

    If we change the first number to something else, comp may not return true anymore:

    a = [121, 144, 19, 161, 19, 144, 19, 11]
    b = [132, 14641, 20736, 361, 25921, 361, 20736, 361]

    comp(a,b) returns false because in b 132 is not the square of any number of a.

我想一个检查所有数组的算法,但我不能让它成为可能。它是 C 语言。我正在 Visual Studio 上编写代码,这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
int tempVal1;
int tempVal2;

bool comp(int *a, int *b, size_t sizeArray)
{
if (sizeArray == 0 || sizeArray == 0)
return false;

for (int k = 0; k < sizeArray; k++)
{

for (int i = 0; i < sizeArray; i++)
{
if (a[k] == sqrt(b[i]))
tempVal1 = 1;
else
tempVal1 = 0;
printf("%d", tempVal1);
}
}
for (int k = 0; k < sizeArray; k++)
{
for (int i = 0; i < sizeArray; i++)
{
if (b[k] == (a[i] * a[i]))
tempVal2 = 1;
else
tempVal2 = 0;
printf("%d", tempVal2);
}
}

printf(" tempval1 : %d\n", tempVal1);
printf(" tempval2 : %d\n", tempVal2);

if (tempVal1 * tempVal2)
return true;
return false;
}

int main() {

bool result;
int a[8] = { 121, 144, 19, 161, 19, 144, 19, 11 };
int b[8] = { 121 * 121, 144 * 144, 19 * 19, 161 * 161, 19 * 19, 144 * 144, 362, 11 * 11 };
result = comp(a, b, 8);
printf("%d\n", result);
}

最佳答案

OP 的代码在功能上是错误的,因为 tempVal1tempVal2 仅依赖于最后的循环 a[k] == sqrt(b[i]) )(b[k] == (a[i] * a[i]) 比较。

相反,内部 for (i...) 循环需要找到一个匹配项。当没有找到时,返回 false

bool comp(const int *a, const int *b, size_t sizeArray) {
for (size_t k = 0; k < sizeArray; k++) {
size_t i;
for (i = 0; i < sizeArray; i++) {
long long aa = (long long) a[i] * a[i];
if (aa == b[k]) {
break;
}
}
// Since the loop did not exit early, no match was found, return fasle
if (i == sizeArray) {
return false;
}
}

for (size_t k = 0; k < sizeArray; k++) {
size_t i;
for (i = 0; i < sizeArray; i++) {
long long aa = (long long) a[k] * a[k];
if (aa == b[i]) {
break;
}
}
// Since the loop did not exit early, no match was found, return fasle
if (i == sizeArray) {
return false;
}
}

return true;
}

将 FP 数学用于整数问题会导致最好避免的精度、范围和舍入问题。

考虑一个没有这些问题的“方形”测试

bool is_aa_square_of_a(int aa, int a) {
if (aa < 0) return false;
if (a == 0) {
return aa == 0;
}
int q = aa/a;
int r = aa%a;
return q == a && r == 0;
}

注意:使用排序数组可以使代码更高效。

关于c - 我的程序有时会返回 true,但并不总是 Codewars 问题 "Are they same",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52630930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com