想知道我犯的错误 [英] Would like to know the error I've made

查看:70
本文介绍了想知道我犯的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉C,所以想知道我在编写程序时犯的错误。问题如下。



目标是使用九个给定的单个数字中的任意六个来形成HH:MM:SS格式的最大可能时间(不一定另外)



给定一组九个单(不一定是不同的)数字,比如0,0,1,3,4,6,7,8,9,通过仅使用每个数字,可以在24小时时间格式HH:MM:SS中形成许多不同的时间,例如17:36:40或10:30:41。目标是找到最大可能的有效时间(00:00:01到24:00:00),这些时间可以使用九个数字中的六个完全一次形成。在这种情况下,它是19:48:37。



所以我尝试编写代码,但它总是导致运行时错误。任何帮助将不胜感激



我尝试过:



I'm not well versed with C so would like to know the error i've made while writing a program. The question is as follows.

The objective is to form the maximum possible time in the HH:MM:SS format using any six of nine given single digits (not necessarily distinct)

Given a set of nine single (not necessarily distinct) digits, say 0, 0, 1, 3, 4, 6, 7, 8, 9, it is possible to form many distinct times in a 24 hour time format HH:MM:SS, such as 17:36:40 or 10:30:41 by using each of the digits only once. The objective is to find the maximum possible valid time (00:00:01 to 24:00:00) that can be formed using some six of the nine digits exactly once. In this case, it is 19:48:37.

So ive tried writing a code but it always results to Runtime error. Any help would be appreciated

What I have tried:

#include<stdio.h>
int main()
{
    int a[8],b[5],i,c=0,d;
    for(i=0;i<9;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]==0)
        c++;
    }
    for(i=8;i>=0;i++)
    {
        if(a[i]<3)
        {
            b[0]=a[i];
            if(b[0]==1||b[0]==0)
            {
                b[1]=a[8];
                a[8]=-1;
            }
            else
            {
                i=8;
                while(a[i]>=5)
                {
                    i--;
                }
                if(a[i]==4&&c==4)
                {
                    b[1]=4;
                    b[2]=0;
                    b[3]=0;
                    b[4]=0;
                    b[5]=0;
                    a[0]=-1;a[1]=-1;a[2]=-1;a[3]=-1;
                }
                else
                {
                    while(a[i]==4)
                    {
                        i--;
                    }
                    b[1]=a[i];
                    a[i]=-1;
                }
                
            }
            
        }
        else
        printf("Impossible");
    }
    for(i=8;i>=0;i++)
    {
        if(a[i]<6)
        {
            d=i;
        }
        else
        printf("Impossible");
    }
    for(i=d;i>=0;i--)
    {
        if(a[i]!=-1)
        {
            b[2]=a[i];
            a[i]=-1;
            break;
        }
    }
    for(i=d;i>=0;i--)
    {
        if(a[i]!=-1)
        {
            b[4]=a[i];
            a[i]=-1;
            break;
        }
    }
    for(i=8;i>=0;i--)
    {
        if(a[i]!=-1)
        {
            b[3]=a[i];
            a[i]=-1;
            break;
        }
    }
    for(i=8;i>=0;i--)
    {
        if(a[i]!=-1)
        {
            b[5]=a[i];
            a[i]=-1;
            break;
        }
    }
    printf("%d%d:%d%d:%d%d",b[0],b[1],b[2],b[3],b[4],b[5]);
    
    
}

推荐答案

我不是在涉及这段代码:这是令人讨厌的。

帮自己一个忙,首先编写一个旨在阅读的代码。

这意味着:

1)适用时的评论解释代码正在做什么。这不是解释代码行本身,而是告诉我为什么它正在做它正在做的事情。
I'm not wading through that code: it's nasty to read.
Do yourself a favour, and start by writing code that is designed to be read.
That means:
1) Comments where applicable to explain what code is doing. That's not explain the line of code itself, but tell me why it's doing what it's doing.
if(a[i]<3)

为什么我感兴趣值是否小于3?那怎么回事?你打算用它做什么?好吧,我猜你正在尝试 - 正确地 - 分出最高的小时数......但你需要告诉别人!

2)停止使用单字母变量名。使用描述变量用途的名称,代码开始自我记录。键入可能会更长,但它更安全,更容易理解 - b应该是a并不明显,但如果你是从输出而不是输入读取,那么它显然是错误的!

3)一致地缩进,并且 - 作为初学者 - 总是使用{和}对,即使它们不需要。

Why am I interested in a value being less than three? So what if it is? What are you going to do with it? OK, I can guess that you are trying - rightly - to separate out the highest hour digit ... but you need to tell people!
2) Stop using single letter variable names. Use names that describe the purpose of the variable and the code starts to self-document. It may be longer to type, but it's a lot safer and easier to understand - it's not obvious that "b" should have been "a" but if you are reading from "output" instead of "input" then it's clearly wrong!
3) Indent consistently, and - as a beginner - always use "{" and "}" pairs, even if they aren't needed.

int a[8],b[5],i,c=0,d;
for(i=0;i<9;i++)
{
    scanf("%d",&a[i]);
    if(a[i]==0)
    c++;
}

缩进意味着无论 a的值是什么, c ++; 都会被执行[我] 而事实并非如此。当我们在这里时,为什么 只有当用户输入零时才会递增 c

4)将代码分解为函数,以便您可以单独测试它们并确信每个函数都是孤立地完成您的预期 - 然后您可以从主代码中调用函数,并且所有函数都可以 - 再次 - 变得更具可读性。并且更容易修改!



完成后,开始研究它失败的原因 - 这是你的功课,所以你的任务是你的任务的一部分工作,而不是我的。 :笑:

幸运的是,你有一个工具可以帮助你找到正在发生的事情:调试器。你如何使用它取决于你的编译器系统,但是一个快速的谷歌用于你的IDE名称和调试器应该给你你需要的信息。



放一个断点在函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但是我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:debugging!

The indentation implies that c++; is executed regardless of the value of a[i] and that isn't the case. And while we are here, why are you incrementing c only when the user enters a zero?
4) Break the code up into functions, so that you can test them in isolation and be confident that each function does what you intended it to in isolation - you can then call the functions from your main code, and it all - again - becomes more readable. And a lot easier to modify!

When you've done that, start working on why it's failing - it's your homework, so it's part of your task to get it working, not mine. :laugh:
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


你有超出范围的数组访问权限数组 a b



您的数组 a 的大小为8.然后您可以使用0到7的索引访问它的项目(长度减一)。类似于 b ,其中允许的索引是0到4.



另一个超出界限的访问可能发生在使用可能具有未定义值的变量 d (您正在打印不可能但继续执行)。



所以使用这样的东西:

You have out of bound array accesses for your arrays a and b.

Your array a has a size of 8. Then you can access it's items using indexes from 0 to 7 (length minus one). Similar for b where the allowed indexes are from 0 to 4.

Another out of bound access may occur when using the variable d which might have an undefined value (you are printing "Impossible" but continue execution).

So use something like this:
int a[9], b[6];

// ...

if(a[i]<6)
    d=i;
else
{
    printf("Impossible");
    return 1;
}





我没有进一步检查你的代码。这意味着由于逻辑错误,在修复上述内容之后,您的代码可能仍无法按预期工作。



I did not checked your code further. That means your code might still not work as expected after fixing the above due to logical errors.


错误消息可能是因为 a 并且 b 的大小错误:

The error message can be because a and b are wrong size:
int a[8],b[5],i,c=0,d;



否则,你的算法错误,整个logc都是错误的,使用调试器查看发生了什么并付费注意用过的数字。



你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是比较它应该做什么。

调试器中没有魔法,它不知道你应该做什么,它没有找到bu gs,它只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]

调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。



PS:这段代码对我来说很熟悉,您是否已将其发布在较旧的问题中?


Otherwise, your algorithm is just wrong, the whole logc is wrong, use the debugger to see what is going on and pay attention to used digits.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

PS: this code look familiar to me, have you already posted it in an older question ?


这篇关于想知道我犯的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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