如何让我的代码通过测试用例? [英] How do I make my code pass the test cases?

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

问题描述

嘿伙计们

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

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

请帮我解决我的错误

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.


除了Carlo告诉你的内容之外,还要做一些好处:

1)正确缩进代码,并且总是使用大括号,即使它只是只有一个语句。特别是当你刚刚开始时,这使你的代码更容易阅读和修改。

这是你的(未修改的)代码缩进和括号:
In addition to what Carlo has told you, do yourself a couple of favours:
1) Indent your code properly, and always use curly brackets, even if it's "only one statement". Particularly when you are just starting, this makes your code easier to read and modify.
Here is your (unmodified) code indented and bracketed:
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;
}


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆