我该如何解决这个问题. [英] How do I proceed in this problem.

查看:248
本文介绍了我该如何解决这个问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <string.h>

void init(char a[])
{
    int i;
    for(i=0;i<26;i++)
    {
        a[i]='a'+i;
        printf("%c",a[i]);
    }
}

int fin(char c,char a[])
{
    int i;
    for(i=0;i<26;i++)
    {
        if(c==a[i])
            return i;
    }
}

int main()
{
    int t,i,l,temp1,temp2,coun=0;
    char a[26],s[1000];
    init(a);
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",s);
        l=strlen(s);
        printf("DEBUGGING LINE 1\n");
        for(i=0;i<l;i++)>
        {
            temp2=fin(s[i],a);
            if(i=0)
                temp1=temp2;
            if(temp2>temp1)
                coun+=(temp2-temp1);
            if(temp2<temp1)>
                coun+=(25-temp2+temp1);
            temp1=temp2;
            printf("DEB %d\t",i);
        }
        if(coun<=l*11)
            printf("YES\n");
        else
            printf("NO\n");
    }
}



我尝试过的事情:

当我运行此代码时,for循环中的i值表现异常.
我找不到问题.请帮助我.



What I have tried:

when i run this code the i value in for loop is behaving unexpectedly.
I can''t find the problem. Please help me.

推荐答案

也可能有其他错误,但是函数fin()中的一个错误并不总是返回值:
There may be other errors too but one is in the function fin() which does not always return a value:
int fin(char c,char a[])
{
    int i;
    for(i=0;i<26;i++)
    {
        if(c==a[i])
            return i;
    }
    // Insert this line and handle the special return value when calling this function
    return -1;
}


如果找不到匹配项,您的代码将返回一个随机值.编译器应在此处发出警告,并非所有路径都返回值.如果不是,则应使用更高的警告级别进行编译(大多数编译器使用-W选项).

另一个错误是变量temp1尚未初始化,但可以在分配值之前使用.这也应该引发编译器警告.


Your code would return a random value when no match has been found. The compiler should throw a warning here that not all pathes return a value. If not, you should compile with a higher warning level (-W option with most compilers).

Another error is that the variable temp1 is not initialised but may be used before a value is assigned. This should also throw a compiler warning.


我认为现在是时候停止猜测代码在做什么了.现在是时候查看您的代码执行并确保其执行您期望的操作了.

调试器是您的朋友.它将向您显示代码的实际作用.
逐步执行,检查变量,您会发现它会停止执行您期望的操作.
Visual Studio 2010中的精通调试-入门指南 [ ^ ]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]
https://www.jetbrains.com/idea/help/debugging-your- first-java-application.html [ ^ ]
I think it is time for you to stop guessing what your code is doing. It is time to see your code executing and ensuring that it does what you expect.

The debugger is your friend. It will show you what your code is really doing.
Follow the execution step by step, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner''s Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]


1)该代码包含语法错误(可能由于复制/粘贴错误):a)在主
中 方法中,有一个>" "for(i = 0; i
2)您不会说"for循环中的i值表现异常"是什么意思.修复语法错误后的结果是:"abcdefghijklmnopqrstuvwxyz".这是您所期望的吗?

3)注意"if"块.不使用"{...}"意味着仅执行一条指令,也就是下一条.

4)"fin"方法仅在"c == a [i]"时才返回值.从逻辑上讲这是错误的,因为您将fin方法定义为返回int,所以无论如何它都应该返回一个值.可能您可以在此方法的最后一行添加一个返回"some int value",但是请确保这取决于预期的程序逻辑.
1) The code contains syntax errors (possibly because of copy/paste typos): a) in the main
method, there is a ">" sign after "for(i=0;i<l;i+b)"; b) in the main method, there is a ">" sign after "if(temp2<temp1)". As someone else already pointed out: is time to compile and run your code and stop guessing what it should happen.

2) You don''t say what means "the i value in for loop is behaving unexpectedly". The result after fixing the syntax errors is: "abcdefghijklmnopqrstuvwxyz". Is this what you expect?

3) Pay attention to the "if" blocks. Not using "{...}" means that only one instruction, that being exactly the next one, is executed.

4) The "fin" method does return a value ONLY when "c==a[i]". That is logically wrong, since you define the fin method as returning int, so it should return a value in any case. Possibly you can add a return "some int value" as the last line of this method, but sure this depends on the expected program logic.


这篇关于我该如何解决这个问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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