
正文
java数学题源代码 java 代码题
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
用java语言写一道数学题
//这个题难点在大数.因为1000的阶乖十在太大了.所以必须用BigInteger.
//楼上那种算法是不对的.这题看起来简单,其实要比想象中麻烦和难.
import java.math.BigInteger;
import java.util.*;
public class jiesheng{
protected static ArrayList table = new ArrayList();
static{ table.add(BigInteger.valueOf(1));}
public static synchronized BigInteger factorial(int x){
for(int size=table.size();size=x;size++){
BigInteger lastfact= (BigInteger)table.get(size-1);
BigInteger nextfact= lastfact.multiply(BigInteger.valueOf(size));
table.add(nextfact);
}
return (BigInteger) table.get(x);
}
public static void main(String[] args){
System.out.print(factorial(1000));
}
}
相关问答
Q1: 这两道题代码怎么写java?
创建一个名字为“ReportCard”的类,然后用下边的内容全部替换掉,你会成为全班最亮的仔。
import java.util.HashMap;
/**
* 学生成绩单
*/
public class ReportCard {
public static void main(String[] args) {
ReportCard reportCard = new ReportCard("张三", "070602213");
reportCard.set("语文", 80.0);
reportCard.set("数学", 59.5);
reportCard.set("英语", 66.0);
reportCard.set("java", 80, 99.0);
reportCard.set("数据库", 80, 66.0);
reportCard.set("毛概", null);
System.out.println(reportCard.getStudentName() + "语文分数:" + reportCard.get("语文"));
System.out.println(reportCard.getStudentName() + "数学考核结果:" + (reportCard.isPassed("数学") ? "合格" : "不合格"));
System.out.println(reportCard.getStudentName() + "期末是否挂科:" + (reportCard.isAllPassed() ? "否" : "是"));
}
// 学生姓名
private String studentName;
// 学生学号
private String studentNumber;
// 成绩单
private HashMapString, CourseResult cards = new HashMap();
public ReportCard() {
}
public ReportCard(String studentName, String studentNumber) {
this.studentName = studentName;
this.studentNumber = studentNumber;
}
public Double get(String courseName){
CourseResult courseResult = cards.get(courseName);
return courseResult == null ? Double.NaN : courseResult.getStudentScore();
}
public void set(String courseName, Double studentScore){
CourseResult courseResult = new CourseResult(courseName, studentScore);
cards.put(courseName, courseResult);
}
public void set(String courseName, double passMark, Double studentScore){
CourseResult courseResult = new CourseResult(courseName, passMark, studentScore);
cards.put(courseName, courseResult);
}
public boolean isPassed(String courseName){
return cards.get(courseName).isPassed();
}
public boolean isAllPassed(){
for(CourseResult cr : cards.values()){
if ( ! cr.isPassed()) {
return false;
}
}
return true;
}
public String getStudentName() {
return studentName;
}
public String getStudentNumber() {
return studentNumber;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
/**
* 课程
*/
class Course{
// 课程名称
protected String courseName;
// 及格分
protected double passMark = 60;
public Course(String courseName, Double passMark) {
this.courseName = courseName;
if ( passMark != null) {
this.passMark = passMark;
}
}
}
/**
* 课程成绩
*/
class CourseResult extends Course{
// 学生成绩
private Double studentScore;
public CourseResult(String courseName, Double studentScore) {
this(courseName, null, studentScore);
}
public CourseResult(String courseName, Double passMark, Double studentScore) {
super(courseName, passMark);
this.studentScore = studentScore == null ? Double.NaN : studentScore;
}
public boolean isPassed(){
return studentScore = passMark;
}
public String getCourseName() {
return courseName;
}
public double getPassMark() {
return passMark;
}
public Double getStudentScore() {
return studentScore;
}
}
Q2: java:杨辉三角,输入n输出它的前n行
以下是 Java 代码java数学题源代码,用于生成杨辉三角并输出前 n 行:
```java
import java.util.Scanner;
public class YangHuiTriangle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int numRows = scanner.nextInt();
for (int i = 0; i numRows; i++) {
int num = 1;
System.out.printf("%" + (numRows - i) * 2 + "s", ""); // 控制输出格式
for (int j = 0; j = i; j++) {
System.out.printf("%4d", num);
num = num * (i - j) / (j + 1); // 计算组合数
}
System.out.println();
}
}
}
```
在这个示例中java数学题源代码,java数学题源代码我们首先使用 `Scanner` 类读取用户输入java数学题源代码的行数 `numRows`。然后,java数学题源代码我们使用两个嵌套的循环来生成杨辉三角。外部循环控制行数,内部循环控制每一行的元素。
在内部循环中,我们使用了公式 `num = num * (i - j) / (j + 1)` 来计算杨辉三角中的组合数,并使用 `printf()` 方法以规定的格式输出结果。
最后,我们使用 `%n`(代表换行符)和 `printf()` 方法在控制台上输出前 n 行杨辉三角。
例如,在以上程序中输入 `6`,将会输出以下结果:
```
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
```
Q3: 请问这道java题怎么做?
Java源代码java数学题源代码:
public class Test {
public static void main(String[] args) {
Point p1 = new Point(4, 5);
System.out.printf("点p坐标为(%f,%f)\n", p1.getX(), p1.getY());
p1.setX(3);
p1.setY(4);
System.out.printf("重置后点p坐标为(%f,%f)\n", p1.getX(), p1.getY());
System.out.printf("点(%f, %f)到原点的距离的平方为%f\n", p1.getX(), p1.getY(),
p1.distance());
Point p2 = new Point(1, 2);
System.out.printf("点(%f,%f)到点(%f,%f)的距离的平方为%f\n", p1.getX(),
p1.getY(), p2.getX(), p2.getY(), p1.distance(p2));
}
}
class Point {
protected double x;
protected double y;
public Point(){
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public void setX(double x) {
this.x = x;
}
public double getX() {
return this.x;
}
public void setY(double y) {
this.y = y;
}
public double getY() {
return this.y;
}
public double distance() {
return Math.pow(x, 2) + Math.pow(y, 2);
}
public double distance(Point p) {
return Math.pow(this.x - p.x, 2) + Math.pow(this.y - p.y, 2);
}
}
运行测试java数学题源代码:
Q4: java解这道数学题(要注释)
Class SolveMathProblem
{
public int TotalCows(int year)
{
int y=year;
int total=0;
if(y4) //如果年龄小于4,没有生牛
{
return 0;
}
else
{
total=total+y-3;//如果大于4,计算这头牛一共生的数量
for(int i=y-3;i0;i--)
{
total=total+TotalCows(i);
//求上述母牛产下的每头小牛自己生的牛的数量
}
return total;
}
}
}
Class Test
{
public static void main(String args[])
{
int y;
int cows;
/*
这一段代码是从控制台读入一个年数,请自己完成。
下面的代码默认y已经有一个具体的值了。
*/
SolveMathProblem s=new SolveMathProblem();
cows=s.TotalCows(y)+1; //cows就是最后的牛的总数
}
}
用了函数递归来解决这个问题,关键就是要能理解
total=total+TotalCows(i);
如果你学过数据结构,应该能理解这种思想:我用TotalCows这个方法求每头牛自己生下的牛的数量,然后把它们加起来。
还有几个问题:
1.我设定每头牛一生下来就是1岁
2.到四岁那年就生第一头牛
java数学题源代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 代码题、java数学题源代码的信息别忘了在本站进行查找喔。







