1
1

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.

開成中学校の算数の入試問題を力技で解いてみた

Last updated at Posted at 2023-02-18

前置き

一部の雑誌記事(下記)で開成中学校の算数の問題が話題になっています。
こういった話題を見ると力技で全数アタックしてみたくなりますよね?ならないですか。そうですか。
PRESIDENT Online

やってみた

ソースコードは下記に置きました。Pythonを使って解きました。
kaisei_2023_math_5

問題

開成中学校2023年入試問題、算数、大問5 (4)
1,2,3,4,5,6,7の7種類の数字のみを用いてつくられた整数A,Bについて
A+B=9723になるA,Bの組は何通りか。

解き方

整数Aを0から9723まで愚直に一つずつ確認します。
関数meet_cond()で整数AもしくはBが条件を満たすか検査します。

cnt = 0
print('#', 'a', 'b')
for a in range(0, 9724):  # aは9723以下
    b = 9723 - a
    if meet_cond(a) is False:
        continue
    if meet_cond(b) is False:
        continue
    cnt += 1
    print(cnt, a, b)
print('条件を満たすa,bの組が' + str(cnt) + '個見つかりました')

検査方法です。0,8,9を含むか確認しています。

def meet_cond(num: int) -> bool:
    """条件に当てはまる数かを検査する(0,8,9を含まない)
    Args:
        num (int): 検査する整数
    Returns:
        bool: Trueなら条件に当てはまる。Falseなら条件に当てはまらない
    """
    for char in str(num):
        if char in '089':
            return False  # 禁止された数(0,8,9)を含む
    return True  # 条件に当てはまる

結論

やったね。これであなたも4月から開成中学生です。試験会場でPythonが使えればね!

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?