编写一个应用程序,验证以下等式是成立的:
提示: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("不符合要求");
}
*/
}
}