0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Python日記#2

Last updated at Posted at 2022-08-01

Pythonのアルゴリズム

下記のプログラムは1以上N以下の整数のうち、十進法での各桁の和がA以上B以下であるもの総和を求める問題です。
入力の形
N A B
例.14 2 4 が入力された場合
45が出力される。

下記がコードになります。

def Sample3():
    # 整数nの各桁の和を求める関数
    def sum(n):
        sum_1 = 0
        while n > 0:
            sum_1 += n % 10 # 10で割った余り
            n //= 10
        return sum_1
    N, A, B = map(int, input().split())
    result = 0
    for i in range(1, N + 1):
        if A <= sum(i) <= B: # A以上B以下
            result += i
    print(result)
Sample3()

以上になります。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?