获取运行时错误。请帮忙。 [英] Getting run time error. Please help on it.

查看:85
本文介绍了获取运行时错误。请帮忙。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个公式来计算F为

F = F(n-1)+ F(n-2)+ F(n-1)* F(n-2)。

我们必须为n的值找到F.

适用于小值。公式正在运行,但是对于较大的值,我将不得不操纵它。在此代码中出现运行时错误。



我尝试过:



  #include   <   bits / stdc ++。h  >  
使用 命名空间标准;
int f( long long int F0, long long int F1, long long int n);

#def M 1000000007
int main()
{
long long int F1,F0,F,T,N;

cin>>吨;
while (T--)
{
cin>> F0>> F1>> N;

F = f(F0,F1,N);
cout<< F%M< ENDL;
}
return 0 ;
}

int f( long long int F0, long long int F1, long long int n)
{
if (n == < span class =code-digit> 0 )
return F0%M;
else if (n == 1
返回 F1%M;
else
{
return int )((( int )pow(F0,f(F0,F1,n- 1 ))%M)
*(( int )pow(F1,f(F0,F1,n)))%M )%M;
}
}

解决方案

第一个问题:

引用:

给出一个公式来计算F为

F = F(n-1)+ F(n-2)+ F (n-1)* F(n-2)。



这个公式不是程序中的公式,它们都不是Fibonacci。

所以声明,程序和Fibonacci是3种不同的东西,请记住并选择其中的1个。



---------

当你不理解你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器查看你的代码是什么这样做。它允许你逐行执行第1行并在执行时检查变量,它是一个令人难以置信的学习工具。



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

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



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做的比较。

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



建议:

  return  int )(( int )pow(F0,f(F0,F1,n-  1 ))%M)*(( int )pow(F1,f(F0,F1,n)))%M)%M; 



使用临时变量拆分此行,每行1次操作。

它可以让你看看调试器发生了什么。



问题:

< blockquote class =quote>

引用:

我在这段代码中得到运行时错误。



因为这不是标准的事情方式。你用的是什么编译器?你在电脑上运行这个吗?你得到了什么确切的信息?


Fibounachi号码变得非常休,所以某处是边界。我认为大约40个它的int大小有问题。为调试目的做一些输出。



你应该在所有情况下使用long long int数据类型。你的计算公式看起来不对。



我知道它是:



  long   long   int  fibonacci(< span class =code-keyword> long   long   int  n){
if (n == 0 return 0 ;
else if (n == 1 return 1 ;
else
return (fibonacci(n- 1 )+ fibonacci(n- 2 ));
}


好的,所以你试图通过N进行递归。但是你不应该用相同的N调用你的函数f你进去了,但只有N-1和N-2!用相同的N调用f将导致无限深度嵌入f的调用,最终会出现堆栈溢出!



另外,我看不到对pow的调用应该是有益的。如果你想实现你的描述中给出的函数f中的行应该是:

  int  fnm1,fnm2; 
...
fnm1 = f(F0,F1,n- 1 );
fnm2 = f(F0,F1,n- 2 );
return fnm1 + fnm2 + fnm1 * fnm2;



如你所见,我已将表达式拆分为两个子表达式,以避免多次调用f。



无论如何,与%M的这整个业务都是无稽之谈。只需删除它。


A formula is given to calculate F as
F=F(n-1)+F(n-2)+F(n-1)*F(n-2).
we have to find F for the values of n.
For small values . formula is working , but for large values , i will have to manipulate it.i m getting run time error in this code.

What I have tried:

#include<bits/stdc++.h>
using namespace std;
int f(long long int F0, long long int F1,long long int n);

#define M 1000000007
int main()
{
    long long int F1,F0,F, T,N;

    cin >> T;
    while(T--)
    {
        cin>>F0>>F1>>N;

        F=f(F0,F1,N);
        cout << F%M << endl;
    }
    return 0;
}

int f(long long int F0, long long int F1,long long int n)
{
     if (n==0)
         return F0%M;
     else if(n==1)
         return F1%M;
     else
     {
         return (int)(((int)pow(F0,f(F0,F1,n-1))%M)
                    * ((int)pow(F1,f(F0,F1,n)))%M)%M;
     }
}

解决方案

First problem:

Quote:

A formula is given to calculate F as
F=F(n-1)+F(n-2)+F(n-1)*F(n-2).


This formula is not the one in your program and neither of them is Fibonacci.
So statement, program and Fibonacci are 3 different things, make your mind and choose 1 of them.

---------
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. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

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.

Advice:

return (int)(((int)pow(F0,f(F0,F1,n-1))%M)*((int)pow(F1,f(F0,F1,n)))%M)%M;


Split this line using temporary variables and 1 operation per line.
It will allow you to see what is going on with the debugger.

Question:

Quote:

i m getting run time error in this code.


Since this is not standard way of things. What compiler are you using? do you run this on your PC ? What exact message do you get?


Fibounachi numbers are getting really hugh, so somewhere is a border. I think around 40 its gets problematic with int size. Make some output for debugging purposes.

You should use the data type long long int in all situations. Your formulara to calculate looks wrong.

I know it as:

 long long int fibonacci(long long int n) {
   if ( n == 0 ) return 0;
   else if ( n == 1 ) return 1;
   else
      return (fibonacci(n-1) + fibonacci(n-2));
} 


Okay, so you are trying to do a recursion via N. But then you should not call your function f with the same N as you got in, but only with N-1 and N-2! Calling f with the same N will cause an indefinitely deep nesting of calls to f and you will end up with a stack overflow!

Also, I can't see that the calls to pow should be good for. If you want to implement the function given in your description that line in function f should be:

int fnm1, fnm2;
...
fnm1 = f(F0, F1, n-1);
fnm2 = f(F0, F1, n-2);
return fnm1 + fnm2 + fnm1*fnm2;


As you see, I have split the expression up into two sub-expressions in order to avoid multiple redundant calls to f.

And this whole business with %M is nonsense anyway. Just remove that.


这篇关于获取运行时错误。请帮忙。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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