什么是我的代码中的问题 [英] Whats the problem in my code

查看:91
本文介绍了什么是我的代码中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

int main()
{
    int n,flag=0,p;
    scanf("%d",&n);
    while(n--){
        int j=0;
        char s[10000];
        scanf("%s",&s);
        j=strlen(s);

        for(int i=1;i<=j/2;i++){

            if(s[i]==s[j-i+1])
            flag++;
            p=i;
            }
        if(flag==p)
            printf("palindrome");
            else
        printf("not");

    }
}

[edit]已添加代码块 - OriginalGriff [/ edit]



< b>我尝试了什么:



im尝试检查回文字符串。

[edit]Code block added - OriginalGriff[/edit]

What I have tried:

i m trying to check palindrome string.

推荐答案

好吧,它非常简单 - 如果你使用过调试器,你会很快发现它。



从一些提示开始:

1)数组第一个元素的索引是什么?

2)运算符优先级是什么:
Well, it's pretty simple - and if you had used the debugger, you would have spotted it pretty quickly.

Start with a couple of hints:
1) What is the index of the first element of an array?
2) What is operator precedence going to do to this:
j-i+1

它会被评估为

j-(i+1)

还是

(j-i)+1

这有关系吗?

3)(1)是否对p的值有影响?

4)flag的值是多少?第二次绕过外循环?



而不是像那样检查,添加一个名为isPalindrome的变量,并将其设置为1(这是C表示true )在内循环之外。

在循环内部,如果你找到一个单身e不匹配,将其设置为0(这是C表示假)



循环后,检查它,它会告诉你它是否是回文。 ..

Does it matter?
3) Does (1) have any effect on the value of p?
4) What is the value of flag the second time you go round the outer loop?

Instead of checking like that, add a variable called isPalindrome, and set it to 1 (this is C for "true") outside the inner loop.
Inside the loop, if you find a single non-match, set it to 0 (this is C for "false")

After the loop, check it, and it tells you if it's a palindrome...


首先,使用适当的缩进,它可以帮助您阅读代码,它可以帮助陌生人阅读您的代码。

First of all, use proper indentation, it help you reading the code, it helps strangers reading your code.
#include <stdio.h>
#include <string.h>

int main()
{
    int n,flag=0,p;
    scanf("%d",&n);
    while(n--){
        int j=0;
        char s[10000];
        scanf("%s",&s);
        j=strlen(s);

        for(int i=1;i<=j/2;i++){

            if(s[i]==s[j-i+1])
                flag++;
            p=i;
        }
        if(flag==p)
            printf("palindrome");
        else
            printf("not");

    }
}



C数组基于0,这意味着 s <中的第一个字符/ code>是 s [0] 。你开始检查第二个字符。



有一个工具可以让你看到你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



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



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

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



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

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


C arrays are 0 based, which means that the first char in s is s[0]. You start checking on second char.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger 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[^]

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 find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于什么是我的代码中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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