1106. 2019 Number Sequence (15 minutes)

Take the numbers 2, 0, 1, and 9 in each digit of 2019 as the first 4 items of a sequence, and use them to construct an infinite sequence, where the nth (>4) item is the unit digit of the sum of the first 4 items. For example, the fifth item is 2, because 2+0+1+9=12, the unit digit is 2.

For this question, you are asked to write a program to list the first n items of this sequence.

Input format:

The input gives a positive integer n (≤1000).

Output format:

Print the first n items of a sequence on one line, without spaces between numbers.

Input sample:

10

Sample output:

2019224758

 Pit point: N may be less than 4

#include<cstdio>
#include<set>
#include<map>
#include<cmath> 
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;

int main(){
	char s[5] = "2019";
	queue<int> nums;
	nums.push(2);
	nums.push(0);
	nums.push(1);
	nums.push(9);

	int N,sum=12,num;
	cin>>N;

	if(N<=4) {
		for(int i=0;i<N;i++){
			cout<<s[i];
		}
	}else{
		cout<<"2019";
	}
	N=N-4; 
	for(int i=1;i<=N;i++){
		num = sum%10;
		sum = sum - nums.front() + num;
		nums.pop(); 
		nums.push(num);
		cout<<num;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_16382227/article/details/125746500