把 2019 各个数位上的数字 2、0、1、9 作为一个数列的前 4 项,用它们去构造一个无穷数列,其中第 n(>4)项是它前 4 项之和的个位数字。例如第 5 项为 2, 因为 2+0+1+9=12,个位数是 2。
本题就请你编写程序,列出这个序列的前 n 项。
输入格式:
输入给出正整数 n(≤1000)。
输出格式:
在一行中输出数列的前 n 项,数字间不要有空格。
输入样例:
10
输出样例:
2019224758
测试点1 ,题目中虽然说了n>4但是小于等于4的也需要考虑
我的代码
#include<string>
#include <iostream>
#include<algorithm>
using namespace std;
int main()
{
string str1 = "2019";
int n;
int last;
int sum=12;
cin >> n;
int b = 0;
if (n <= 4)
{
cout << str1.substr(0, n);
return 0;
}
cout << "2019";
for (int i = 0; i < n-4; i++)
{
string str = to_string(sum);
char c = str.back();
cout << c;
str1 += c;
sum = 0;
for (int j = str1.size() - 1; j > str1.size() - 5; j--)
{
sum += str1[j] - '0';
}
}
cout << endl;
}