Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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++程序,它将根据用户提供的变量输入打印10个数字序列。_C++_Loops_For Loop - Fatal编程技术网

打印序列时循环出现问题 我必须编写一个C++程序,它将根据用户提供的变量输入打印10个数字序列。

打印序列时循环出现问题 我必须编写一个C++程序,它将根据用户提供的变量输入打印10个数字序列。,c++,loops,for-loop,C++,Loops,For Loop,预期产出: 输入一个数字:10 系列:11 13 16 20 25 31 38 46 55 65 以下是我目前的代码: #include <stdio.h> int main() { int j, sum = 0, b=1; for (j = 10; j <= 65; j=j+b++) { sum = sum + j; printf("%d\n",j); } } #包括 int main() {

预期产出:

输入一个数字:10

系列:11 13 16 20 25 31 38 46 55 65

以下是我目前的代码:

#include <stdio.h>

int main() 
{ 
    int j, sum = 0, b=1; 
    for (j = 10; j <= 65; j=j+b++) 
    { 
        sum = sum + j;
        printf("%d\n",j);
    } 
}
#包括
int main()
{ 
int j,和=0,b=1;

对于(j=10;j我认为这是解决方案:

#include <iostream>

using namespace std;

int main() {
    int n;
    cin >>n;
    int lastNumber=n;
    for (int i=1; i<=n; i++) {
        lastNumber+=i;
        cout <<lastNumber <<' ';
    }
    endl(cout);
}
#包括
使用名称空间std;
int main(){
int n;
cin>>n;
int lastNumber=n;

对于(inti=1;i,这里有一个几乎完成的解决方案,您的工作部分是更新一行,其中注释是
/*?*/

有关更多信息,请参见代码注释:

#include <iostream>

int main()
{
    // Starting value, ex. 10, chosen by user
    int input = 0;

    // Length of the sequence, also chosen by user
    int length = 0;

    // Sequence is incremented according to the pattern you already know
    // for more info see:
    // https://www.mathsisfun.com/algebra/sequences-sums-arithmetic.html
    int adder = 0;

    // Ask user to input starting number
    std::cout << "Please enter the starting number: ";
    std::cin >> input;

    // TODO: verify user input is number

    // Ask user for desired length of a sequence
    std::cout << "Please enter the desired length of sequence: ";
    std::cin >> length;

    std::cout << "Generating sequence..." << std::endl;

    // Generate sequence
    // TODO: your part of job is to replace the ???
    for (int counter = 0; counter < length; ++counter, input += /* ??? */)
    {
        // Show the current number in the sequence
        std::cout << input << std::endl;
    }

    return 0;
}
#包括
int main()
{
//起始值,例如10,由用户选择
int输入=0;
//序列的长度,也由用户选择
整数长度=0;
//序列根据您已经知道的模式递增
//有关更多信息,请参阅:
// https://www.mathsisfun.com/algebra/sequences-sums-arithmetic.html
整数加法器=0;
//要求用户输入起始号码
std::cout>输入;
//TODO:验证用户输入是否为数字
//询问用户所需的序列长度
std::cout>长度;

std::难道我不明白这个问题。为什么序列以11开头?下一个数字的规则是什么?@FlatAssembler+1、+2、+3、+4、+5…相当基本的谜题!@FlatAssembler我想它是从
input+1
开始的,它将10与1相加,这就是为什么11再加2,这就是为什么13@ArdentCoder实际上,它是使用geomet的
input+1
ric progression;)我还怀疑序列长度10是固定的,而不是从输入中获取的,尽管我承认这一点没有明确说明。一些建议:
adder
是一个未使用的变量,
length
始终等于10(来自问题)@ArdentCoder哈哈,我看到你和我分享了写代码的风格,多酷啊!:)至于序列长度,我看这里没有问题,在另一个答案中有一条注释,它应该包括在内。而
加法器
变量是应该放入
/*?*/
的变量。顺便说一句,该注释是在对问题进行剧烈编辑之前提出的,总之
长度
使您的代码更灵活,并且OP可以
const
10,如果他们愿意的话。我想
counter
是你谜题的答案:P