首页 > 其他 > 详细

递归-第N项是由第N-1项加n得到的

时间:2019-11-17 18:44:31      阅读:116      评论:0      收藏:0      [点我收藏+]

技术分享图片

package lhh.dataStructureAndAlgorithm;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @program: IdeaJava
 * @Date: 2019/11/17 16:44
 * @Author: lhh
 * @Description: 第N项是由第N-1项加n得到的,
 */
public class TrangleApp {

    static int theNumber;

    public static String getString()throws IOException{
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String s = br.readLine();
        return s;
    }

    public static int getInt() throws IOException{
        String s = getString();
        return Integer.parseInt(s);
    }


    public static int trangle(int n) {
        if(n == 1) return 1;
        else return (n+trangle(n-1));
    }

    public static void main(String[] args) throws IOException {
        System.out.print("Enter a number: ");
        theNumber = getInt();
        int theAnswer = trangle(theNumber);
        System.out.println("Triangle="+theAnswer);
    }
}

 

递归-第N项是由第N-1项加n得到的

原文:https://www.cnblogs.com/lhh666/p/11877428.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!