模拟计算机病毒传播的简单递归问题
A simple recursion problem to simulate the spread of a computer virus
我尝试解决的问题是:
- 一种电脑病毒会在第0天感染100台电脑
- 每70%的受感染计算机都会感染一台以上的计算机。
- 有 2 位计算机科学家解决了这个问题。他们每个人都可以在第 1 天修复 1 台计算机。
- 随后的每一天,由于他们增加了经验,他们可以修复比前一天多一倍的计算机。
- 那么,n 天后还有多少台计算机被感染?
从第 0 天到第 20 天的数字如何?
为了解决这个问题,我写了下面的代码:
#include<iostream>
#include<cmath>
using namespace std;
int computervirus(int n){ //n:Tage
int nr_virus= 100;
int nr_fixed;
int rest = 0;
if(n == 0) return 100;
if(n <= 20){
nr_virus += computervirus(n-1)*0.7;
nr_fixed = pow(2,n);
rest = nr_virus - nr_fixed;
}
return rest;
}
int main(){
cout << endl;
for(int i=0; i <= 20; i++){
cout << "at the "<< i << " .day are still "<< computervirus(i) << " infected Computers\n" <<endl;
}
return 0;
}
数字(受感染的计算机)的输出是不正确的,因为至少前 9 天感染速度肯定比修复快。
我不确定 prrblem 在哪里。你能帮忙吗?
您忘记在 for 循环主体周围使用大括号
int main(){
for(int i=0; i <= 20; i++)
{
cout << endl;
cout << "at the "<< i << " .day are still "<< computervirus(i) << " infected Computers\n" <<endl;
}
return 0;
}
在循环中的第一个语句之前放置一个左大括号 {
,在最后一个语句之后放置一个右大括号 }
。
因为缺少大括号,只有第一个语句 cout << endl;
在循环内。
您第 n 天的递归尝试使用第 n+1 天结果的 70%。这会导致无限递归。您必须使用第 1 天的 70%,它才会起作用。现在不是前一天的 70%,而是多了 70%,所以不是 * 0,7
,而是 * 1,7
。
并且您需要考虑到受感染的计算机数量不能为负数。
最后,您高估了维修次数:第 1 天是 2 次,第 2 天是 4 次,但是由于第 1 天的 2 次已经计算在第 1 天的递归中,所以您推导了两次。所以你应该只计算第 n 天固定的附加计算机。所以 pow(2, n-1)。
更正后的代码如下所示:
int computervirus(int n){ //n:Tage
int nr_virus= 100;
int nr_fixed = 0;
int rest = 0;
if(n>0){
nr_virus = computervirus(n-1)*1.7; // n-1 not n+1
if (n==1)
nr_fixed = 2;
else nr_fixed = pow(2,n-1);
}
rest = nr_virus - nr_fixed;
if (rest<0)
rest = 0;
return rest;
}
我尝试解决的问题是:
- 一种电脑病毒会在第0天感染100台电脑
- 每70%的受感染计算机都会感染一台以上的计算机。
- 有 2 位计算机科学家解决了这个问题。他们每个人都可以在第 1 天修复 1 台计算机。
- 随后的每一天,由于他们增加了经验,他们可以修复比前一天多一倍的计算机。
- 那么,n 天后还有多少台计算机被感染?
从第 0 天到第 20 天的数字如何?
为了解决这个问题,我写了下面的代码:
#include<iostream>
#include<cmath>
using namespace std;
int computervirus(int n){ //n:Tage
int nr_virus= 100;
int nr_fixed;
int rest = 0;
if(n == 0) return 100;
if(n <= 20){
nr_virus += computervirus(n-1)*0.7;
nr_fixed = pow(2,n);
rest = nr_virus - nr_fixed;
}
return rest;
}
int main(){
cout << endl;
for(int i=0; i <= 20; i++){
cout << "at the "<< i << " .day are still "<< computervirus(i) << " infected Computers\n" <<endl;
}
return 0;
}
数字(受感染的计算机)的输出是不正确的,因为至少前 9 天感染速度肯定比修复快。 我不确定 prrblem 在哪里。你能帮忙吗?
您忘记在 for 循环主体周围使用大括号
int main(){
for(int i=0; i <= 20; i++)
{
cout << endl;
cout << "at the "<< i << " .day are still "<< computervirus(i) << " infected Computers\n" <<endl;
}
return 0;
}
在循环中的第一个语句之前放置一个左大括号 {
,在最后一个语句之后放置一个右大括号 }
。
因为缺少大括号,只有第一个语句 cout << endl;
在循环内。
您第 n 天的递归尝试使用第 n+1 天结果的 70%。这会导致无限递归。您必须使用第 1 天的 70%,它才会起作用。现在不是前一天的 70%,而是多了 70%,所以不是 * 0,7
,而是 * 1,7
。
并且您需要考虑到受感染的计算机数量不能为负数。
最后,您高估了维修次数:第 1 天是 2 次,第 2 天是 4 次,但是由于第 1 天的 2 次已经计算在第 1 天的递归中,所以您推导了两次。所以你应该只计算第 n 天固定的附加计算机。所以 pow(2, n-1)。
更正后的代码如下所示:
int computervirus(int n){ //n:Tage
int nr_virus= 100;
int nr_fixed = 0;
int rest = 0;
if(n>0){
nr_virus = computervirus(n-1)*1.7; // n-1 not n+1
if (n==1)
nr_fixed = 2;
else nr_fixed = pow(2,n-1);
}
rest = nr_virus - nr_fixed;
if (rest<0)
rest = 0;
return rest;
}