简体   繁体   English

运行我的程序时出现运行时错误

[英]Runtime error when running my program

My code is to get the num of digits for any integer up to 1000000 at most.我的代码是获取最多 1000000 的任何整数的位数。 This is my code either using while or for or if :这是我使用whileforif代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h> 
int main() {
  int t,num_of_seven_segment=0;

  printf("Enter test case: ");

  scanf("%d", &t);`

 /* for(;t!=0;t=t/10)
  {
        if(t>=1000001)
break; 
      num_of_seven_segment++;
  }*/

  while(t!=0)
{
    t=t/10;
    num_of_seven_segment++;

  }
   printf(" %d",num_of_seven_segment);
   getch();

}
#include <stdio.h>

#include <stdlib.h>

#include <math.h>

int main()


{

int t;



int num_of_sevensegment=0;



printf("enter test cases");



scanf("%d",&t);




if(t<1000000)



num_of_sevensegment+=6;




 if (t<100000)



num_of_sevensegment--;




 if (t<10000)



num_of_sevensegment--;



 if (t<1000)



num_of_sevensegment--;


 if (t<100)


num_of_sevensegment--;



 if (t<10)



num_of_sevensegment--;



else if(t==1000000)



num_of_sevensegment=7;




printf("%d",num_of_sevensegment);




return 0;


}

Since you are using an online judge, I would say that adding a return 0;既然你用的是在线判断,那我就说加个return 0; statement at the end of the first code should fix it.第一个代码末尾的语句应该修复它。 getch() doesn't usually work. getch()通常不起作用。

Additionally (not really related to the runtime error), I don't think explicitly prompting the user for input is required in online judges.此外(实际上与运行时错误无关),我认为在线评委不需要明确提示用户输入。

Also, in the statement - printf(" %d",num_of_seven_segment);另外,在语句中 - printf(" %d",num_of_seven_segment); there seems to be an extra space before %d - a possible source of wrong answer. %d之前似乎有一个额外的空格 - 可能是错误答案的来源。

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

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