如何在c++;(初学者)。n+;nx2&x2B;nx2x2+; 我试图编写一个C++程序,它从int A—5项开始,每秒钟INT- B都会变大2X倍。例如,1秒打印-5、2秒打印-10、3秒打印-20。然后打印一个总数。(35). 因为我是一个非常初学者,所以我被它困住了 int main() { int a,b; cout << " Enter a and b: " << endl; cin >> a >> b; for (int i=1; i<=b; i++) { cout << i << endl; } return 0; } intmain() { INTA,b; cout a>>b; 对于(int i=1;i
您需要创建一个新变量如何在c++;(初学者)。n+;nx2&x2B;nx2x2+; 我试图编写一个C++程序,它从int A—5项开始,每秒钟INT- B都会变大2X倍。例如,1秒打印-5、2秒打印-10、3秒打印-20。然后打印一个总数。(35). 因为我是一个非常初学者,所以我被它困住了 int main() { int a,b; cout << " Enter a and b: " << endl; cin >> a >> b; for (int i=1; i<=b; i++) { cout << i << endl; } return 0; } intmain() { INTA,b; cout a>>b; 对于(int i=1;i,c++,for-loop,C++,For Loop,您需要创建一个新变量result。然后根据您的逻辑修改其值 int result = 0; for (int i=1; i<=b; i++) { result = result + a; cout << a << endl; a = a * 2; } cout << result << endl; int结果=0; 对于(int i=1;i现在,你只需要打印出它循环的次数,因为你从i=1到i=3。因此,当b=3时,你
result
。然后根据您的逻辑修改其值
int result = 0;
for (int i=1; i<=b; i++) {
result = result + a;
cout << a << endl;
a = a * 2;
}
cout << result << endl;
int结果=0;
对于(int i=1;i现在,你只需要打印出它循环的次数,因为你从i=1
到i=3
。因此,当b=3
时,你的i
的值是:1,2,3。那么这与算法有什么关系呢:
a+at+at*t….+n
找到该模式并使用a
值。\include
#include <iostream>
using namespace std;
int main()
{
int a,b,i,result;
cout << " Enter a and b: " << endl;
cin >> a >> b;
result = a; // At 0 sec result is a
for (i=1; i<=b; i++)
{
result = result*2; // Doubles the result at every second
cout <<result<< endl;
}
return 0;
}
使用名称空间std;
int main()
{
int a,b,i,结果;
cout a>>b;
结果=a;//在0秒时,结果为a
对于(i=1;我想你会如何用笔和纸来做这件事。我在学校的时候,我们会画出两页大的算法运行图。呸。你很幸运有图。在我的时代,我们还在一起敲打石头来编写代码。我会写一个+at+at*t…+n.a是一个每秒钟变大2倍的数字。Rocks?ROCKS?呸,我们希望我们有石头。当我们想找出一个算法时,我们必须使用幼儿园的学生。