c ++动态数值系列

Jos*_*ama 1 c++ visual-c++ c++11

我正在尝试创建一个程序,按照特定的数字系列条件(1-1-3-6-8-8-10-20)打印前200个元素,但不显示只有200个元素显示802.我假设是因为"for"循环中的代码,但我花了很多时间考虑如何减少代码到工作,我想不出别的.我很沮丧,需要你的帮助.

练习是在代码评论上

//Print the following numerical series 1-1-3-6-8-8-10-20  until 200
#include <stdafx.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
    int Num1=200, z = 0, x = 1, y = 1;

    cout << "\n\n1,";
    cout << " 1,";

    for (int i = 1; i <= Num1; i++)
    {
        z = y + 2;
        cout << " " << z << ","; //It will print 3
        z = z * 2;
        cout << " " << z << ",";//It will print 6
        z = z + 2;
        cout << " " << z << ",";//It will print 8
        z = z;
        cout << " " << z << ",";//It will print 8

        y = z;

    }

    cout << "\n\n";
    system("pause");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

pla*_*m95 5

你循环200次,每次循环,你打印出4个不同的数字.您还在开始时打印两次,以便2 + 4*200 = 802,这是您的802号输出来自的地方.