Arithmetic progression 等差数列

In your class, you have started lessons about arithmetic progression. Since you are also a programmer, you have decided to write a function that will return the first n elements of the sequence with the given common difference d and first element a. Note that the difference may be zero!
The result should be a string of numbers, separated by comma and space

#include"iostream"

#include<string>

using namespace std;

std::string arithmeticSequenceElements(int a, int r, int n) 
{
  //your code here
    std::string str="";
    if(n<=0)
    {
        return str;
    }
    int currentValue=a;
    str=to_string(currentValue);
    for(int i=1;i<n;i++)
    {
        currentValue+=r;
        str+=(", "+to_string(currentValue));
    }
    return str;

}


人生苦短C++11之后有to_string(n) 这个题目就显得简单多

猜你喜欢

转载自blog.csdn.net/u014038245/article/details/80728498