简体   繁体   English

测试用例不在C程序中运行

[英]testcase not running in c program

iam new to c program and facing difficulty in debugging programs.In the below code test case 2 is not running.I have found that the error is in reading interger n in the second test case.someone please hep me with this issue.Also please recommend me with some tools that can be ued for debugging c programs using terminal.Thanks for help 我是C程序的新手,并且在调试程序时遇到困难。在下面的代码测试用例2中未运行。我发现错误是在第二个测试用例中读取整数n时出现的。请有人帮助我解决此问题。向我推荐一些可以用于使用终端调试c程序的工具。

#include <stdio.h>
#include <stdlib.h>

int read(){
   int r = 0;
   char c = getchar_unlocked();
   while(c >= '0' && c <= '9'){
      r = r*10 + c - 48 ;
      c = getchar_unlocked();
   }
   return r;
}

void main(){
   int t = 0;
   t = read();
   int rr = 0;
   for(rr = 0;rr < t;rr++){
      int i,n = 0;
      n = read();
      int *p = (int *)calloc(n,sizeof(int));
      for(i = 0;i < n;++i){
         *(p+i) = getchar_unlocked() - 48;
      }
      int no,nz = 0;
      for(i = 0;i < n;++i){
         if(*(p+i) == 0){nz += 1;}
         if(*(p+i) == 1){no += 1;}
      }
      int k = 0;
      if(((no)%2 == 0) && ((nz)%2) == 0){
         k = -1;
      }
      if(((no)%2 == 0) && ((nz)%2) == 1){
         k = 0;
      }
      if(((no)%2 == 1) && ((nz)%2) == 0){
         k = 1;
      }
      if(((no)%2 == 1) && ((nz)%2) == 1){
         k = 1;
      }
      int result = 0;printf("%d\n",5556);
      if(k == 1){
         for(i = 0;i < n;++i){
            if(*(p+i) == 1){
               result = i+1 ;
               break;
            }
         }

      }
      if(k == 0){
         for(i = 0;i < n;++i){
            if(*(p+i) == 0){
               result = i+1 ;
               break;
            }
         }
      }
      printf("%d\n",result);
   }
}

Your strategy to read an integer is flawed. 您读取整数的策略存在缺陷。 You don't have the logic to skip whitespaces. 您没有跳过空格的逻辑。 I would change the function name to read_int and change its implementation to 我将函数名称更改为read_int并将其实现更改为

int read(){
   int n;
   if ( scanf("%d", &n) != 1 )
   {
       // Deal with the error
   }
   return n;
}

Also, change 另外,改变

*(p+i) = getchar_unlocked() - 48;

to

*(p+i) = read_int();

or a more intuitive version: 或更直观的版本:

p[i] = read_int();

With those changes, I am able to read and process the numbers. 有了这些更改,我就能读取和处理数字。 But I still get the wrong output. 但是我仍然得到错误的输出。 I'll let you figure the logic error in your code. 我会让您弄清楚代码中的逻辑错误。

Additional Comments 附加评论

main is expected to return an int . main预计将返回一个int If your compiler didn't complain about that, it's time to up the warning level. 如果您的编译器没有抱怨,那就该提高警告级别了。 I use -Wall by default. 我默认使用-Wall

When you are in the process of debugging your code, it's always good to test the code that reads the input to make sure that there is no error in reading the input. 在调试代码的过程中,最好测试读取输入的代码,以确保读取输入没有错误。

Here's what I did to your code: 这是我对您的代码所做的:

#include <stdio.h>
#include <stdlib.h>

int read_int(){
   int n;
   if ( scanf("%d", &n) != 1 )
   {
      // Deal with the error.
   }
   return n;
}

int main(){
   int t = 0;
   int rr = 0;

   t = read_int();
   printf("t = %d\n", t);

   for(rr = 0;rr < t;rr++){
      int i,n = 0;

      n = read_int();
      printf("n = %d\n", n);

      int *p = (int *)calloc(n,sizeof(int));
      for(i = 0;i < n;++i){
         p[i] = read_int();
         printf("p[%d] = %d\n", i, p[i]);
      }

      int no,nz = 0;
      for(i = 0;i < n;++i){
         if(*(p+i) == 0){nz += 1;}
         if(*(p+i) == 1){no += 1;}
      }

      int k = 0;
      if(((no)%2 == 0) && ((nz)%2) == 0){
         k = -1;
      }
      if(((no)%2 == 0) && ((nz)%2) == 1){
         k = 0;
      }
      if(((no)%2 == 1) && ((nz)%2) == 0){
         k = 1;
      }
      if(((no)%2 == 1) && ((nz)%2) == 1){
         k = 1;
      }

      int result = 0;
      // printf("%d\n",5556);
      if(k == 1){
         for(i = 0;i < n;++i){
            if(*(p+i) == 1){
               result = i+1 ;
               break;
            }
         }

      }

      if(k == 0){
         for(i = 0;i < n;++i){
            if(*(p+i) == 0){
               result = i+1 ;
               break;
            }
         }
      }
      printf("%d\n",result);
   }

   return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM