Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么CPP代码的输出因“失败”而失败;832564823476“;作为输入?_C++_Algorithm_Data Structures - Fatal编程技术网

C++ 为什么CPP代码的输出因“失败”而失败;832564823476“;作为输入?

C++ 为什么CPP代码的输出因“失败”而失败;832564823476“;作为输入?,c++,algorithm,data-structures,C++,Algorithm,Data Structures,我目前正在Coursera上注册数据结构和算法专业。我在作业中遇到了一个问题。问题是如何找到斐波那契序列n项之和的最后一位(单位) 例如: 输入:3 输出:4 F0+F1+F2+F3=0+1+1+2=4 我提供的解决方案是: #include <iostream> using namespace std; int lastDigit=0; int fibLastDigit(unsigned long long); int main() { unsigned long lo

我目前正在Coursera上注册数据结构和算法专业。我在作业中遇到了一个问题。问题是如何找到斐波那契序列n项之和的最后一位(单位)

例如: 输入:3
输出:4
F0+F1+F2+F3=0+1+1+2=4

我提供的解决方案是:

#include <iostream>
using namespace std;

int lastDigit=0;
int fibLastDigit(unsigned long long);

int main() {
    unsigned long long terms;
    cin>>terms;         //0<=n<=10^18
    cout<<fibLastDigit(terms);
    return 0;
}

int fibLastDigit(unsigned long long nTh) { 
    int first=0, second=1, third;
    if(nTh == 0) {
        lastDigit = first;
        return lastDigit;
    }
    else if(nTh == 1) {
        lastDigit = second;
        return lastDigit;
    }
    else if(nTh > 1) { 
        lastDigit = 1;
        while(nTh>=2) {
            first = first % 10;
            second = second % 10;
            third = ((first + second)%10);
            first = second;
            second = third;
            lastDigit = (lastDigit + third) % 10;
            nTh--;

        }

    }
    return lastDigit;
}
#包括
使用名称空间std;
整数位数=0;
int fibLastDigit(无符号长);
int main(){
未签署的长期协议;

cin>>术语;//0第一个
n
斐波那契数
F(n)
的和是
F(n+2)-1
()。您对这个数的最后一位感兴趣


现在认为,“代码> f(n)< /代码>的最后一个数字只取决于<代码> f(n-1)和<代码> f(n-2)< /代码>的最后一个数字。最多有100个这样的对,所以最后一个数字序列必须重复。确实,<代码> ListDigiod(f(n))=LasDigiDigy(f(n+1))< /C>(参见),所以你应该能够找到一个比现在快得多的算法。这样你就不会让控制台“冻结”(即等待算法完成)再长一点。

你的计算机要从这个数字一个接一个地倒计时需要很长时间。即使在现代CPU上也是如此。因为这是你告诉计算机要做的,一个接一个地倒计时。你必须意识到这需要一段时间,所以也许你应该尝试在量子计算机上运行它?你在倒计时832564823476.Yekes.Debugging Pro提示:尝试一个较小的数字,看看algo是否工作。即使整个循环只需要一个周期,2.4 GHz的机器也需要将近6分钟来执行它。@nicomp是的,这适用于较小的数字。如果你查看足够多的斐波那契数的最后一个数字,你会发现有一个周期e、 (如果你仔细想想,你可能会意识到一定有一个。)这也不是一个很长的周期。