找到更多的这样的整数:一些相续正整数的立方和正好等于另一个整数的立方。

编写一个应用程序,验证以下等式是成立的:

提示:if分支语句、for循环语句的写法与C、C++是相似的。

如果能完成上面的验证,则达到基本要求(可得到基本分数8分)。

较高要求,可根据实际情况选做(可得到附加的2分):找到更多的这样的整数:一些相续正整数的立方和正好等于另一个整数的立方。

提交时,可将java程序或项目压缩后上传附件。如果有什么特殊之处(如算法比较复杂)可以在写一个说明性的文件一起放到压缩文件中。

import java.io.*;
import java.io.InputStreamReader;
import java.util.List;
import java.math.*;


public class Work1 {
    public static void main(String args[]){
        int start = 0;
        int end = 0;
        int ans = 0;
        int length = 0;
        int sum = 0;
        double sums = 0;

        System.out.println("Please input a start number: ");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            start = Integer.parseInt(br.readLine());
        }catch (IOException e){}

        //System.out.println(start);

        System.out.println("Please input an end number:  ");
        br = new BufferedReader(new InputStreamReader(System.in));
        try {
            end = Integer.parseInt(br.readLine());
        }catch (IOException e){}

        //System.out.println(end);
/**
 * 验证结果输入
 */
        /*System.out.println("Please input the answer to prove: ");
        br = new BufferedReader(new InputStreamReader(System.in));
        try{
            ans = Integer.parseInt(br.readLine());
        }catch (IOException e){}

        //System.out.println(ans);
        */
        length = end - start +1;
        int[] list = new int[length];
        for (int i = 0;i < length;i++){
            list[i] = start + i;
            System.out.print(list[i]+" ");
        }
        /**
         * 第二个作业,求解一定范围内立方和结果为某一个整数的立方的数有哪些
         */
        System.out.println("\n范围内结果有:");
        for (int j = 0;j < length;j++){
            sums = Math.pow(list[j],3);
            for(int i = j + 1;i < length;i++){
                sums = Math.pow(list[i],3) + sums;
                //System.out.println(Math.pow(sums,1.0/3));
                if (Math.cbrt(sums) == (int)Math.cbrt(sums)){
                    //System.out.println("成功");
                    ans = (int) Math.cbrt(sums);
                    System.out.print(ans+" ");
                }else {
                    //System.out.println("Null");
                }
            }
        }

        /**
         * 第一个作业,验证立方和等式成立
         */
        /*for (int i= 0;i < length;i++){
            list[i] = start + i;
            sum = sum + list[i]*list[i]*list[i];
        }
        //System.out.println(sum);
        //System.out.println(ans*ans*ans);
        if (sum == ans*ans*ans){
            System.out.println("符合要求");
        }else {
            System.out.println("不符合要求");
        }
*/


    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值