如果它是正确的,我如何检查我的代码? [英] How do I check my code if it's right ?

查看:81
本文介绍了如果它是正确的,我如何检查我的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C程序由一个主函数和一个名为FindFact的函数组成。在主读取N中,一次整数一个。通过传递读取的数字来调用FindFact然后从函数中打印返回的值,函数FindFact接收数字,查找和每次我的代码运行时返回它的阶乘。

给我超出时间限制!有什么问题?



我尝试过的事情:



  #include   <   stdio.h  >  
int FindFact( int x);
int main( void ){
int N,i,num,y;
scanf( %d,& N);
for (i = 1; i< = N; ++ i){
scanf( %d,& num);
y = FindFact(num);
printf( factorial of%d =%d \ n,y,num );
}
return 0 ;
}
int FindFact( int x){
int F = 1,j;
for (j = x; j> = 1; - j){
F * = j;
}
返回 F;
}< /stdio.h>

解决方案

您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

在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 [ ^ ]



你应该交换 y num

 printf(   factorial of%d =%d \ n,num,y); 


首先简化它并测试FindFact函数:

  int  main( void 
{
int num,y ;
num = 6 ;
y = FindFact(num);
printf( factorial of%d =%d \ n,y,num );
return 0 ;
}

如果可行,那么你知道可能不是导致问题的FindFact函数。如果没有,那么你知道它是FindFact并且可以专注于它。

因此,恢复原始代码,并开始通过调试器运行它。仔细查看正在发生的事情,并观察变量包含的内容。尝试预测在执行一行代码之前会发生什么。会发生吗?如果确实如此,继续前进。如果没有,那么为什么不呢?发生了什么?为什么你没想到呢?

这是编码后的第一个阶段:测试和调试 - 这是一项技能,需要使用所有技能才能改进。对于像这样的琐碎项目来说,学习和练习比面对20,000行代码更好! :笑:


C program consists of a main and a function named FindFact .In main read N integers numbers one at a time .Call FindFact by passing the read number then print the returned value from the function , the function FindFact receive number, finds and returns its factorial .
every time my code run it gives me "time limit exceeded" ! what's the problem ?

What I have tried:

#include <stdio.h>
int FindFact (int x);
int main(void) {
int N,i,num,y;
scanf("%d",&N);
for(i=1;i<=N;++i){
	scanf("%d",&num);
	y=FindFact(num);
	printf("factorial of %d=%d\n",y,num);
}
	return 0;
}
int FindFact(int x){
	int F=1,j;
	for(j=x;j>=1;--j){
		F*=j;
	}
	return F;
}</stdio.h>

解决方案

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, 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[^]

You should swap y and num

printf("factorial of %d=%d\n",num,y);


Start by simplifying it and testing the FindFact function:

int main(void) 
    {
    int num, y;
    num = 6;
    y=FindFact(num);
    printf("factorial of %d=%d\n",y,num);
    return 0;
    }

If that works, then you know that it's probably not the FindFact function that causes the problem. If it doesn't, then you know it is FindFact and can focus on that.
So restore your original code, and start running it through the debugger. Look at exactly what is happening, and watch what variables contain. Try to predict what is going to happen before you execute a line of code. Does it happen? If it does, move on. If it didn't, then why not? What did happen? why didn't you expect that?
This is the first stage after coding: test and debugging - and it's a skill that like all skills needs to be used in order to improve. And it's better to learn and practice on a trivial project like this one than when faced with 20,000 lines of code! :laugh:


这篇关于如果它是正确的,我如何检查我的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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