1年前 (2018-08-17)  编程语言 |   抢沙发  15 
文章评分 0 次,平均分 0.0

1.

A number n is triangular if n == 1 + 2 +...+j for some j. Write a function
int isTriangular(int n)
that returns 1 if n is a triangular number, otherwise it returns 0. The first 4 triangular numbers are 1 (j=1), 3 (j=2), 6, (j=3), 10 (j=4).

 

package com.zzy;

/**
 * 更多请关注: http://hadoopall.com
 * Created by hadoopall on 17/08/2018.
 */
public class triangular {
    public static void main(String[] args) {
        System.out.println(istriangular(10));

    }
    public  static int istriangular(int n){

        int sum = 0;

        for (int i = 0; i <= n; i++) {
            sum = sum+ i;
            System.out.println("sum"+sum);
            if(sum == n){
                System.out.println("sum"+sum);
                return 1;
            }else if(sum > n ){
                return 0;
            }
        }

        return 1;
    }
}
 

除特别注明外,本站所有文章均为HadoopAll原创,转载请注明出处来自http://hadoopall.com/626.html

发表评论

表情 格式

暂无评论

登录

忘记密码 ?

切换登录

注册

扫一扫二维码分享