Java遞歸方法求5!的實(shí)現(xiàn)代碼
更新時間:2017年02月21日 23:45:11 投稿:mdxy-dxy
這篇文章主要介紹了Java遞歸方法求5!的實(shí)現(xiàn)代碼,需要的朋友可以參考下
題目:利用遞歸方法求5!。
程序分析:遞歸公式:fn=fn_1*4!
程序設(shè)計(jì):
import java.util.Scanner; public class Ex22 { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); Ex22 tfr = new Ex22(); System.out.println(tfr.recursion(n)); } public long recursion(int n) { long value = 0 ; if(n ==1 || n == 0) { value = 1; } else if(n > 1) { value = n * recursion(n-1); } return value; } }
方法二利用遞歸方法求5!。
public class lianxi22 { public static void main(String[] args) { int n = 5; rec fr = new rec(); System.out.println(n+"! = "+fr.rec(n)); } } class rec{ public long rec(int n) { long value = 0 ; if(n ==1 ) { value = 1; } else { value = n * rec(n-1); } return value; } }
相關(guān)文章
Mybatis動態(tài)SQL?foreach批量操作方法
這篇文章主要介紹了Mybatis動態(tài)SQL?foreach批量操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03spring @Profiles和@PropertySource實(shí)現(xiàn)根據(jù)環(huán)境切換配置文件
這篇文章主要介紹了spring @Profiles和@PropertySource根據(jù)環(huán)境切換配置文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11java階乘計(jì)算獲得結(jié)果末尾0的個數(shù)代碼實(shí)現(xiàn)
今天偶然看到一個要求,求1000~10000之間的數(shù)n的階乘并計(jì)算所得的數(shù)n!末尾有多少個0?要求: 不計(jì)算 只要得到末尾有多少個0就可以了,看下面的代碼吧2013-12-12spring boot(三)之Spring Boot中Redis的使用
這篇文章主要介紹了spring boot(三)之Spring Boot中Redis的使用,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-05-05JAVA動態(tài)代理模式(從現(xiàn)實(shí)生活角度理解代碼原理)
本文主要介紹了JAVA動態(tài)代理模式(從現(xiàn)實(shí)生活角度理解代碼原理)的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03springboot使用Thymeleaf報錯常見的幾種解決方案
這篇文章主要介紹了springboot使用Thymeleaf報錯常見的幾種解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11一文秒懂IDEA中每天都在用的Project Structure知識
這篇文章主要介紹了一文秒懂IDEA中每天都在用的Project Structure知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10