The 10th Lanqiao Cup C Language Group A-Sum of Sequences

1. Problem description:

Given a sequence of 1, 1, 1, 3, 5, 9, 17, …, starting from item 4, each item is the sum of the first three items. Find the last 4 digits of item 20190324.
Answer submission
This is a result of filling in the blanks. You only need to calculate the result and submit the output. The result of this question is a 4-digit integer (hint: the thousand digit of the answer is not 0). When submitting the answer, only output this integer. If you output extra content, you will not be able to score.
Input
No input.
Output
Output an integer.
Hint
Put the answer in the output statement, for example, printf or cout can be used in C/C++ language.
Note: What needs to be output is an integer, do not output any extra content.

2. Thinking analysis:

Analyzing the topic, we can know that we can simulate the whole process in the loop, declare four variables t, a, b, c to update the current value of the previous three items, t is used to record the current sum of the previous three items, where t = a + b + c, a = b, b = c, c = t, so that the current values ​​of the previous three items are updated in the loop, because the number 20190324 may be too large and the problem requires solving the last four digits of the 20203324th item Yes, if you reserve 4 digits for a number, you only need to take the remainder of this number to 10000 so that the last four digits are retained, and the last four digits are only the last four of the sum of a, b, and c. The digits are related and have nothing to do with other digits, so when we update a, b, and c, we take the remainder of 10000 so that the last four digits are retained, and overflow will not occur after the remainder is taken. How to determine the starting position of the loop start? At the beginning, define a, b, c, t = 1, 1, 1, 0, a count variable count. When I need to output the fourth item, then the loop should be executed once. At this time, count = 3, loop judgment The condition count <4, execute the loop t = a + b + c = 3 when the next loop count = 4 does not meet the condition to end the loop, so the initial value of count can be defined as 3, solve the first term, then the loop judgment condition is How many (a simple example can determine the starting position of the loop)

3. The code is as follows:

if __name__ == '__main__':
    a, b, c = 1, 1, 1
    count, t = 3, 0
    while count < 20190324:
        t = (a + b + c) % 10000
        a = b % 10000
        b = c % 10000
        c = t % 10000
        count += 1
    print(t)

 

Guess you like

Origin blog.csdn.net/qq_39445165/article/details/115049342