列表

详情


NC25051. [USACO 2007 Feb L]Digit Sums

描述

Farmer John has recently become quite bored working on the farm. Since he has nothing better to do, he starts writing down the integers 1, 2, 3, ... . Eventually, Farmer John realizes that he will never finish, so instead he starts looking at some of the properties of the numbers. He often likes to take a number and find its digit sum as follows:
Step 1: Add all of the digits of the current number together to form a new number.
Step 2: If the new number has exactly one digit, then that is the digit sum of the number. Otherwise, go back to step 1.
For example, the digit sum of 12345 is evaluated as follows:
1) 1 + 2 + 3 + 4 + 5 = 15.
2) 15 has two digits, so we continue:
1) 1 + 5 = 6.
2) 6 has only one digit, so the digit sum of 12345 is 6.
Write a program that, when given two numbers A and B (1 <= A <= B <= 10,000,000; B - A <= 1,000,000), and a single-digit number D, finds how many numbers between A and B, inclusive, have a digit sum of D.

输入描述

Line 1: Three space-separated integers, respectively: A, B, and D

输出描述

Line 1: A single integer representing the number of integers in between A and B that have a digit sum of D.

示例1

输入:

1 20 2

输出:

3

说明:

How many numbers between 1 and 20 have a digit sum of 2?
The numbers 2, 11, and 20 are in the range and contain a digit sum
of 2. No other numbers in the range have a digit sum of 2.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Python3 解法, 执行用时: 43ms, 内存消耗: 7604K, 提交时间: 2023-08-18 11:32:29

a, b, d = map(int, input().split())

ans = 0
if d == 9:
    if a % 9 == 0: ans += 1
    if b % 9 == 0: ans += 1
else:
    if a % 9 == d: ans += 1
    if b % 9 == d: ans += 1

ans += (b-a) // 9
print(0 if d == 0 else ans)

上一题