c - 无法清除测试用例: Am supposed to write code for this:

标签 c testcase

苹果和橙子的问题。

12 个测试用例中只有 3 个被清除。几个小时后就想不出其他事情了。

示例输入0

7 11
5 15
3 2
-2 2 1
5 -6

示例输出 0

1
1

问题:https://www.hackerrank.com/challenges/apple-and-orange/problem

代码

int main(){

    int s; 
    int t; 
    scanf("%d %d",&s,&t);

    int a; 
    int b; 
    scanf("%d %d",&a,&b);

    int m; 
    int n; 
    scanf("%d %d",&m,&n);

    int *apple = malloc(sizeof(int) * m);
    for(int apple_i = 0; apple_i < m; apple_i++){
       scanf("%d",&apple[apple_i]);
    }


    int *orange = malloc(sizeof(int) * n);
    for(int orange_i = 0; orange_i < n; orange_i++){
       scanf("%d",&orange[orange_i]);
    }

    int fellap=0;
    int fellor=0;
    int d;

    for(int apple_i = 0; apple_i < m; apple_i++){
        d=apple[apple_i]+a;
        f(d>=s && d<=t){
            fellap++;
        }
    }

    for(int orange_i = 0; orange_i < n; orange_i ++){
        d=orange[orange_i]+b; 
        if(d>=s&&d<=t){
            fellor++;
        }
    }

    printf("%d\n", fellor);
    printf("%d\n", fellap);

    return 0;
}

最佳答案

您的代码中的错误是一个非常微不足道的错误。您已经切换了橙子和苹果的输出。改变

printf("%d\n", fellor);
printf("%d\n", fellap);

printf("%d\n", fellap);
printf("%d\n", fellor);

我认为没有必要将所有值读入数组。您可以简单地遍历输入并同时进行计算。这是通过所有测试用例的示例:

通过所有测试用例的工作代码:

int main(){
    int s; 
    int t; 
    scanf("%d %d",&s,&t);
    int a; 
    int b; 
    scanf("%d %d",&a,&b);
    int m; 
    int n; 
    scanf("%d %d",&m,&n);

    int noApples=0;
    int noOranges=0;

    for(int apple_i = 0; apple_i < m; apple_i++){
        int apple;  
        scanf("%d",&apple);
        if (apple+a >= s && t >= apple+a) 
            noApples++;
    }

    for(int orange_i = 0; orange_i < n; orange_i++){
        int orange;
        scanf("%d",&orange);
        if (orange+b >= s && t >= orange+b)
            noOranges++;
    }
    printf("%d\n%d\n", noApples, noOranges);
    return 0;
}

关于c - 无法清除测试用例: Am supposed to write code for this:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44998081/

相关文章:

从源 wchar* 中的位置 a 复制到位置 b 从位置 c 复制到目标 wchar*,不以 null 终止

c - AIX 6.1 上的 GDB 没有 TUI 模式吗?

c - 如何打印存储在指向数组的指针中的元素数组,而指向数组的指针存储在指针数组中?

django - 也停止运行测试的抽象 django.test TestCase 类

python - 使用 PyCluster 优化 K(理想的簇数)

c - 如何使用c语言编写的cgi程序上传图片文件?

javascript - 让javascript强制Selenium测试失败

unit-testing - 设计实践 : code to create before deletion in deletion test case?

jquery - 检查输入字段的 "type"属性值

ruby-on-rails-3 - 如何在 ActionController TestCase (Rails 3.0.9) 中禁用渲染 View