如何让我的代码通过测试用例

如何让我的代码通过测试用例

本文介绍了如何让我的代码通过测试用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们

我有问题通过我老师提供的测试用例

这是二进制减法,因为我已经编写了算法

请帮我解决我的错误

Hey guys
I am having an issue passing my test cases provided by the teacher
This is for binary subtraction as i have written the algorithm allready
please help me with what i am doing wrong

#include <stdio.h>

int main(void)
{
    int borrow, i, n, j, f;
    int x[100]= {0}, y[100]= {0}, difference[100]= {0};
    scanf("%d", &x[i]);
    scanf("%d", &y[i]);

    borrow = 0;
    for (i == 0; n - 1; i++ )
    if(y[i] <= x[i])
    {
    difference[i] = x[i]- y[i];
     }
    else if (i = n - 1)
    {
        borrow = 1;
        x[i] = x[i] + 10;
        difference[i] = x[i] - y[i];
    }
    else (j = i + 1);
     while (x[j] == 0 && j < n)
    {
        j = j + 1;
        j = j - 1;

    }
    while (j > i)
    {
        x[j] = x[j] + 10 - 1;
        j = j - 1;
        difference[i] = x[i] - y[i];

    }

         printf("%d",difference[i]);


    return 0;
}





我的尝试:



我试图用提供的测试用例运行代码,但它说代码无法编译或运行



What I have tried:

I have tried to run the code with the test cases provided, but it saying that the code could not compile or run

推荐答案

引用:

scanf(%d,& x [i]);

scanf("%d", &x[i]);

这里(和以下行) i 变量未初始化。

您应该在循环中收集用户输入,以初始化数组。 br />
你在程序的其余部分做了类似的错误。

Here (and in the following line) the i variable is NOT initialized.
You should gather the user input in a loop, in order to initialize the arrays.
You did similar errors in the remaining part of the program.


int main(void)
{
    int borrow, i, n, j, f;
    int x[100]= {0}, y[100]= {0}, difference[100]= {0};
    scanf("%d", &x[i]);
    scanf("%d", &y[i]);

    borrow = 0;
    for (i == 0; n - 1; i++ )
    {
        if(y[i] <= x[i])
        {
            difference[i] = x[i]- y[i];
        }
        else if (i = n - 1)
        {
            borrow = 1;
            x[i] = x[i] + 10;
            difference[i] = x[i] - y[i];
        }
        else
        {
            j = i + 1;
        }
    }
    while (x[j] == 0 && j < n)
    {
        j = j + 1;
        j = j - 1;
    }
    while (j > i)
    {
        x[j] = x[j] + 10 - 1;
        j = j - 1;
        difference[i] = x[i] - y[i];
    }
    printf("%d",difference[i]);
    return 0;
}

你能看出判断 循环结束的地方有多容易吗?



2)停止使用单个字符变量名称:它可以快速输入,但这使得阅读和理解变得更加困难。使用描述性名称告诉您变量的用途有助于您的代码自我记录,而现代IDE几乎不需要任何额外的时间使用!确定 - 借用差异具有描述性命名,但 x y i n j f 用于?你甚至不用其中一个!



3)你想告诉我这段代码是做什么的吗?

Can you see how much easier it is to tell where your for loop ends?

2) Stop using single character variable names: it's quick to type, but it makes it harder to read and understand. Using "descriptive" names that tell you what the variable is used for helps your code become self documenting, and with modern IDE's takes hardly any extra time to use! OK - borrow and difference are descriptively named, but what are x, y, i, n, j, and f for? You don't even use one of them!

3) Do you want to tell me what this code does?

while (x[j] == 0 && j < n)
{
    j = j + 1;
    j = j - 1;
}


这篇关于如何让我的代码通过测试用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 15:03