「AP過去問 令和7年度春期 午前 問7」の版間の差分

提供:yonewiki
(ページの作成:「AP過去問 令和7年度春期 午前 問題に戻る AP過去問 令和7年度春期 午前 問6前の問題へ AP過去問 令和7年度春期 午前 問8次の問題へ   =='''問7(問題文)'''==   ア  イ  ウ  エ    =='''回答・解説'''==   AP過去問 令和7年度春期 午前 問6前の問題へ AP過去問 令和7年度春期 午前 問8次の問題へ […」)
 
編集の要約なし
 
8行目: 8行目:


=='''問7(問題文)'''==
=='''問7(問題文)'''==
 
 fact(n)は、非負の整数nに対してnの階乗を返す。fact(n)の再帰的な定義はどれか。




ア 
ア if n=0 then return 0 else return n*fact(n-1)


イ 
イ if n=0 then return 0 else return n*fact(n+1)


ウ 
ウ if n=0 then return 1 else return n*fact(n-1)


エ 
エ if n=0 then return 1 else return n*fact(n+1)


 
 


=='''回答・解説'''==
=='''回答・解説'''==
 再帰的に階乗を求める関数fact(n)において、階乗の定義は以下のようになります。
 0の階乗は1です。つまり、fact(0)=1です。
 n>0のとき、fact(n)=n×fact(n−1)となります。
 この定義に従うと、'''正しい再帰定義'''は以下の通りです。
 '''ウ if n=0 then return 1 else return n*fact(n-1)'''
 したがって
<span style = "background:linear-gradient(transparent 75%, #7fbfff 75%); font-weight:bold; ">
ウ</span>


 が答えです。


 
 

2025年4月23日 (水) 23:14時点における最新版

AP過去問 令和7年度春期 午前 問題に戻る

AP過去問 令和7年度春期 午前 問6前の問題へ

AP過去問 令和7年度春期 午前 問8次の問題へ

 

問7(問題文)

 fact(n)は、非負の整数nに対してnの階乗を返す。fact(n)の再帰的な定義はどれか。


ア if n=0 then return 0 else return n*fact(n-1)

イ if n=0 then return 0 else return n*fact(n+1)

ウ if n=0 then return 1 else return n*fact(n-1)

エ if n=0 then return 1 else return n*fact(n+1)

 

回答・解説

 再帰的に階乗を求める関数fact(n)において、階乗の定義は以下のようになります。


 0の階乗は1です。つまり、fact(0)=1です。


 n>0のとき、fact(n)=n×fact(n−1)となります。


 この定義に従うと、正しい再帰定義は以下の通りです。


 ウ if n=0 then return 1 else return n*fact(n-1)


 したがって



 が答えです。

 

AP過去問 令和7年度春期 午前 問6前の問題へ

AP過去問 令和7年度春期 午前 問8次の問題へ

AP過去問 令和7年度春期 午前 問題に戻る